Meta‑prompts are higher‑order templates that generate concrete prompts on‑the‑fly, while DSPy automates the search for prompt variants using differentiable programming over LLM calls.
Step‑by‑step workflow
1. Define the meta‑prompt – write a Jinja‑style or DSPy Prompt subclass that contains placeholders for context, examples, and instructions.
2. Collect a validation set – 200‑500 input‑output pairs covering the target distribution; split 80/20 for train/val.
3. Specify an evaluation metric – e.g. score = 0.7rouge_l(pred, ref) - 0.001len(pred) to balance quality and token cost.
4. Configure the DSPy optimizer – set model="gpt-4o-mini", temperature=0.0, max_tokens=256, budget=5000 LLM calls, and early_stop=0.01 improvement threshold.
5. Run the search – DSPy back‑propagates through the prompt template, mutating wording, few‑shot examples, and temperature flags to maximize the metric.
6. Deploy the best prompt – render the optimized template and optionally cache the compiled prompt for production.
Comparison table
| Aspect | Meta‑Prompt | DSPy Optimizer |
|--------|-------------|----------------|
| Control granularity | Template‑level (static slots) | Gradient‑level (continuous token embeddings) |
| Required data | Example pairs only | Example pairs + explicit metric |
| Runtime cost | Single LLM call per query | Iterative calls (≤ budget) |
| Flexibility | Easy to hand‑craft | Automated discovery of non‑intuitive phrasing |
Minimal DSPy example
import dspy
from dspy import Prompt, Optimizer, rouge_l
class QA(Prompt):
instruction = "Answer the question using only the provided context."
context = dspy.Input()
question = dspy.Input()
answer = dspy.Output()
opt = Optimizer(
model="gpt-4o-mini",
temperature=0.0,
max_tokens=256,
eval_metric=lambda pred, ref: 0.7*rouge_l(pred, ref) - 0.001*len(pred),
budget=5000,
early_stop=0.01,
)
best_prompt = opt.search(QA, train_set, val_set)
print(best_prompt.render())The optimizer will adjust the instruction string, example ordering, and optional few‑shot snippets until the weighted ROUGE‑L score plateaus within the 5 k‑call budget.