Skip to contentSkip to page list

Docs

Optimization Algorithms

What each optimization algorithm changes, what it needs, how many suite runs it costs, and when to use it.

On this page

On this page we show which algorithm to pick for improving your agent, from the guided loop to the DSPy optimizers, when the metric is a scenario suite.

The metric is what sets the cost: every metric call is one simulated conversation, so an optimizer that plans hundreds of calls costs hundreds of conversations. The table counts those calls on the six-scenario example of Optimize with DSPy.

Algorithms side by side

AlgorithmChangesNeedsMetric calls on 6 scenariosTracked in LangWatchUse when
Guided loop (Langy or your coding agent)Prompt, tool descriptions, tool code, harness settings, one hypothesis per runThe suite and the traces6 to 12 per hypothesisEvery run in Agent TestingThe cause is in code, a tool or the harness, or you want to review each change
BootstrapFewShotAdds few-shot examples taken from passing runsA metricAbout 20YesThe instructions are right and the model needs examples of the format
BootstrapFewShotWithRandomSearchSame, and searches over sets of examplesA metric50 to 100YesFew-shot examples help and you can afford the search
COPRORewrites the instructions, breadth-first then depth-firstA metricAbout 50 with breadth=3, depth=2YesInstructions only, on a small budget
MIPROv2Proposes instructions and few-shot sets, then searches over the combinationsA metric52 zero-shot with num_candidates=4, num_trials=6: 10 to bootstrap, 42 over seven full evaluations. 42 minutes and $0.93 on the exampleYesThe default choice for instructions, with or without examples
GEPARewrites the instructions from text feedback, keeps a Pareto front of candidatesA metric that returns a score and feedback textSet max_metric_calls: 48 took 51 runs, 31 minutes and $0.64 on the exampleYesThe judge's reasoning is the feedback, which is the scenario case
SIMBARewrites instructions and examples from self-reflection over mini-batchesA metricAbout 60 with max_steps=4NoLarger example sets, where mini-batches pay off

The instruction optimizers above (COPRO, MIPROv2, GEPA, SIMBA) rewrite the instructions of every predictor in the program, and on a dspy.ReAct program the instructions contain the tool descriptions, so those optimizers change the tools as well. BootstrapFewShot and BootstrapFewShotWithRandomSearch leave the instructions alone and only add demonstrations. See Optimize with DSPy.

Which algorithm to pick

  • Start with the guided loop. Most of the turns in a long conversation come from a tool contract or a harness setting, and an algorithm cannot change either, while one hypothesis in the guided loop costs one suite run.
  • GEPA when the metric returns text feedback. The scenario judge writes why a candidate failed, and GEPA's reflection reads that text and proposes the next candidate from it, without you reviewing each step.
  • MIPROv2 when the metric is a number. Use MIPROv2 with an evaluator that returns a score and no reasoning, or with a dataset of expected outputs; LangWatch tracks every trial.
  • COPRO on a small budget, when you want the instructions rewritten in a few calls and have no examples to add.
  • BootstrapFewShot when the format is the problem, meaning the instructions are right and the model gets the shape of the answer wrong.

Tracking a run

langwatch.dspy.init(experiment=..., optimizer=optimizer) records every trial of a tracked optimizer in Experiments, with the score, the instructions of each predictor and the examples it chose. The tracked optimizers are BootstrapFewShot, BootstrapFewShotWithRandomSearch, COPRO, MIPROv2 and GEPA. For any other optimizer, the custom optimizer page shows how to log a step yourself.

Also check: Improve your agent (the loop the algorithms run inside), DSPy visualization, Prompt optimization with Langy.

Documentation