Files
magnus919_agent-skills/dspy/references/agent-patterns.md
T
Magnus Hedemark 95046675bc feat: add dspy — expert skill for compiled prompt programs
Greenfield SkillOpt: 3 epochs for a Stanford DSPy compiler skill.
DSPy is a fundamentally different paradigm from chain/RAG frameworks.

Epoch 1 — Prominence:
- Hard-gate blockquote: 'DSPy is NOT a chain framework'
- Core Paradigm section with runnable code example early

Epoch 2 — Decision Guidance:
- Framework Routing Guide (DSPy vs LlamaIndex vs LangChain vs LangGraph)
- Where to Start table mapping entry points
- Troubleshooting table with reference links

Epoch 3 — Pattern Expansion:
- Optimizer selection cheat sheet from official docs
- Caching, compilation cost management, save/load
- FAQ covering paradigm confusion, errors, deployment

12 files: SKILL.md, 7 references, 3 templates, 1 script.
v1.0.0 -> v1.0.3 across 3 epochs.

All API surfaces validated against dspy.ai official docs —
optimizer selection guide, caching, core modules, FAQ.
Signed-off-by: Jasper <jasper@montcastle.bitches>
2026-07-09 14:51:32 -04:00

1.2 KiB

DSPy Agent Patterns

ReAct Agent

import dspy

def search(query: str) -> str:
    """Search the knowledge base."""
    return f"Results for: {query}"

def calculate(expr: str) -> str:
    """Evaluate a mathematical expression."""
    return str(eval(expr))

agent = dspy.ReAct(tools=[search, calculate], signature="question -> answer")
result = agent(question="What is 2+2?")

AvatarOptimizer for Agent Programs

The AvatarOptimizer is specifically designed for agent-style programs with clean pass/fail metrics:

from dspy.teleprompt import AvatarOptimizer

optimizer = AvatarOptimizer(metric=metric, max_iters=10)
compiled = optimizer.compile(program, trainset=trainset)

It partitions trainset into positive and negative examples, then iteratively proposes instruction edits that explain the difference.

Tool Design Guidelines

  • Tools are plain Python functions with type hints and docstrings
  • The docstring becomes the tool description the LM sees
  • Tools should handle errors gracefully and return string results
  • For complex tools, wrap external APIs with error handling inside the function
  • ReAct loops until no more tool calls or max iterations reached