> ## Documentation Index
> Fetch the complete documentation index at: https://langwatch.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## 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.

# Optimization Algorithms

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

Every algorithm below takes a program, a set of examples and a metric, and returns the program with better text in it. With a scenario suite as the metric, one metric call is one simulated conversation, which is why the number of metric calls is the cost that matters in the table. The guided loop is listed first because it is the one to start with: it changes code and tools, and no algorithm does that.

## The table

| 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                                                             | Through the callback in the example | 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](/docs/improve-your-agent/optimize-with-dspy).

## Which one

* **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.** A 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.** An evaluator that returns a score with no reasoning, or a dataset with expected outputs, fits MIPROv2, and LangWatch tracks every trial of it.
* **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. For an optimizer without a tracked class, the [custom optimizer](/docs/dspy-visualization/custom-optimizer) page shows how to log a step yourself.

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