# Optimization Algorithms

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

> Page: https://langwatch.ai/docs/improve-your-agent/algorithms
> Source: https://github.com/langwatch/langwatch/blob/main/docs/improve-your-agent/algorithms.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

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](https://langwatch.ai/docs/improve-your-agent/optimize-with-dspy).

## Algorithms side by side

| Algorithm | Changes | Needs | Metric calls on 6 scenarios | Tracked in LangWatch | Use when |
|---|---|---|---|---|---|
| Guided loop (Langy or your coding agent) | Prompt, tool descriptions, tool code, harness settings, one hypothesis per run | The suite and the traces | 6 to 12 per hypothesis | Every run in Agent Testing | The cause is in code, a tool or the harness, or you want to review each change |
| BootstrapFewShot | Adds few-shot examples taken from passing runs | A metric | About 20 | Yes | The instructions are right and the model needs examples of the format |
| BootstrapFewShotWithRandomSearch | Same, and searches over sets of examples | A metric | 50 to 100 | Yes | Few-shot examples help and you can afford the search |
| COPRO | Rewrites the instructions, breadth-first then depth-first | A metric | About 50 with `breadth=3, depth=2` | Yes | Instructions only, on a small budget |
| MIPROv2 | Proposes instructions and few-shot sets, then searches over the combinations | A metric | 52 zero-shot with `num_candidates=4, num_trials=6`: 10 to bootstrap, 42 over seven full evaluations. 42 minutes and $0.93 on the example | Yes | The default choice for instructions, with or without examples |
| GEPA | Rewrites the instructions from text feedback, keeps a Pareto front of candidates | A metric that returns a score and feedback text | Set `max_metric_calls`: 48 took 51 runs, 31 minutes and $0.64 on the example | Yes | The judge's reasoning is the feedback, which is the scenario case |
| SIMBA | Rewrites instructions and examples from self-reflection over mini-batches | A metric | About 60 with `max_steps=4` | No | Larger 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](https://langwatch.ai/docs/improve-your-agent/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](https://langwatch.ai/docs/dspy-visualization/custom-optimizer) page shows how to log a step yourself.

> **Info:** **Also check:** [Improve your agent](https://langwatch.ai/docs/improve-your-agent/overview) (the loop the algorithms run inside), [DSPy visualization](https://langwatch.ai/docs/dspy-visualization/quickstart), [Prompt optimization with Langy](https://langwatch.ai/docs/langy/prompt-optimization).

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.
