How to build your first DSPy program and optimize it
When to use: You have a task where prompt quality matters and want a systematic way to improve it.
Prerequisites
- Python 3.10+ with dspy-ai installed — pip install dspy-ai
- Skill cloned — git clone https://github.com/OmidZamani/dspy-skills ~/.claude/skills/dspy-skills
Flow
-
Define the signatureTask: classify support tickets into {billing, technical, account}. Give me a DSPy signature and a simple Predict module.✓ Copied→ Signature + module code
-
Write an evalAdd an evaluation set of 50 labeled examples and an accuracy metric.✓ Copied→ Eval harness with metric callable
-
OptimizeRun BootstrapFewShot to compile the module against the eval set.✓ Copied→ Compiled predictor + improved score
Outcome: A DSPy-optimized predictor that beats a hand-written prompt, with reproducible code.
Pitfalls
- Eval too small — optimizer overfits — Minimum 100–200 examples; hold out a true test set
- Metric doesn't capture what you care about — Spend on metric design before on model choice