Writing prompts by hand is a search problem done badly: you try a wording, look at a few outputs, and keep whatever felt better. Automatic prompt optimisation does the same search with a metric, a dataset and a model proposing the candidates. Four methods cover most of what's in use: APE, OPRO, MIPROv2 and GEPA. They differ mainly in two things: how they propose a new prompt, and how they decide which ones to keep.
The shared loop
candidates = [initial_prompt]
repeat until budget is spent:
propose new prompts # how is the method's main difference
score them on examples # usually a minibatch, to save calls
select which to keep # greedy, Bayesian, or Pareto
return best prompt on a validation set it was never tuned onEvery method needs the same three ingredients: a set of examples with known good outputs, a metric that scores an output (exact match, a test, or an LLM judge), and a budget. What the method gives you back is text: an instruction, sometimes with few-shot examples. Nothing about the model's weights changes, which is why this is the cheapest lever after choosing a model.
APE: generate, score, select
Automatic Prompt Engineer (Zhou et al., 2022) shows a model input-output pairs and asks it to infer the instruction that produced them, samples many candidates, scores each on held examples, and keeps the best, optionally resampling variations of the winners. Across 24 NLP tasks its instructions were better than or comparable to human-written ones on 19.[1] APE treats proposal as a one-shot guess; it doesn't learn from failures.
OPRO: the optimiser sees its own history
OPRO (Yang et al., Google DeepMind) puts the history in the prompt. Each step shows the optimiser model a meta-prompt containing previous instructions with their scores, sorted, plus a few task examples, and asks for a new instruction that scores higher. The best prompts it found beat human-designed ones by up to 8% on GSM8K and up to 50% on Big-Bench Hard tasks.[2] OPRO popularised optimised phrases such as “Take a deep breath and work on this problem step-by-step.” Its weakness is that the optimiser sees scores, not reasons: it learns that a prompt scored 71%, not why.
MIPROv2: instructions and examples together
MIPRO (Opsahl-Ong et al., 2024) was built for DSPy programs, where a pipeline has several LM calls and you only have labels for the final output.[3] It optimises each module's instruction and its few-shot demonstrations:
- Bootstrap demos by running the program and keeping traces that led to correct final answers.
- Propose instructions grounded in a summary of the dataset, the program's code and the demos.
- Search combinations of instructions and demos with a Bayesian surrogate model trained on noisy minibatch scores, so it spends evaluations on promising combinations.
With Llama-3-8B, MIPRO beat baseline optimisers on five of seven multi-stage programs, by up to 13% accuracy.[3] In DSPy it's dspy.MIPROv2.[4]
GEPA: reflect on traces, keep a Pareto frontier
GEPA (Agrawal et al., 2025) changes both halves of the loop.[5]
- Reflective proposal. Instead of a score, the proposer reads full traces of a few rollouts (inputs, intermediate steps, outputs) plus textual feedback from the metric, such as which test failed or why the answer was wrong, and writes a revised instruction that addresses those specific failures.
- Pareto selection. Rather than keeping only the single best prompt, it keeps every candidate that is best on at least one training example, and samples the next one to mutate from that frontier. That preserves prompts that handle a niche of cases well, and avoids collapsing early onto one local optimum.[6]
Across six tasks, GEPA beat the reinforcement-learning method GRPO by 6% on average and up to 20%, using up to 35 times fewer rollouts, and beat MIPROv2 by over 10%.[5] The comparison with RL is the headline: for many tasks, rewriting the prompt from feedback extracts more from each rollout than a scalar reward does.
Side by side
| Method | Proposes from | Keeps | Optimises | Best for |
|---|---|---|---|---|
| APE | Input-output examples | Top scorers | Instruction | Simple single-call tasks |
| OPRO | Past prompts and scores | Top scorers | Instruction | Tasks with a clean numeric metric |
| MIPROv2 | Data summary, program, demos | Bayesian surrogate's best | Instructions + few-shot demos per module | Multi-step DSPy pipelines |
| GEPA | Traces and textual feedback | Pareto frontier | Instructions per module | Agents and pipelines where you can explain failures |
Running them in DSPy
Both MIPROv2 and GEPA ship in DSPy.[4] GEPA's metric can return feedback text alongside the score; the richer that feedback, the better its rewrites.[6] A minimal example (check the DSPy docs for current argument names):
import dspy
dspy.configure(lm=dspy.LM("openai/gpt-4.1-mini")) # the model you'll deploy
class Route(dspy.Signature):
"""Route a support ticket to billing, bug or how-to."""
ticket: str = dspy.InputField()
label: str = dspy.OutputField()
program = dspy.Predict(Route)
def metric(gold, pred, trace=None, pred_name=None, pred_trace=None):
ok = gold.label == pred.label.strip().lower()
feedback = "Correct." if ok else f"Expected {gold.label}, got {pred.label}."
return dspy.Prediction(score=float(ok), feedback=feedback) # GEPA reads the feedback
# MIPROv2: Bayesian search over instructions + few-shot demos
mipro = dspy.MIPROv2(metric=lambda g, p, t=None: metric(g, p).score, auto="light")
tuned_a = mipro.compile(program, trainset=train)
# GEPA: reflective evolution with a Pareto frontier
gepa = dspy.GEPA(metric=metric, auto="light",
reflection_lm=dspy.LM("openai/gpt-4.1")) # a stronger model to reflect
tuned_b = gepa.compile(program, trainset=train, valset=val)Budget it before you run it
Every candidate is scored by running the model on examples and, often, a judge on each output. The arithmetic for a modest run:
Each candidate costs (cases scored) × (1 model call + 1 judge call)
4 candidates per round × 12-case minibatch × 2 calls = 96 calls per round
5 rounds = 480 calls
+ final check of 3 prompts on 50 held-out cases × 2 = 300 calls
≈ 780 calls
At 1,000 input + 300 output tokens per call, that's about 1M tokens:
a few dollars on a small model, more on a frontier judge.How these methods mislead you
- Overfitting the training examples. An optimiser that sees the same 30 cases for many rounds will find wording that suits those 30. Always confirm on a held-out set it never saw.
- Answer leakage. A proposer that sees expected outputs can paste them into the prompt. Check winning prompts for verbatim answers.
- Noise mistaken for gains. Minibatch scores of 12 cases are very noisy. Compare the final prompt with the baseline on the same held-out cases with a paired test; see our sample-size guide.
- A judge you haven't checked. The optimiser will exploit any weakness in the metric. Calibrate the judge first; see Can you trust your LLM judge?
For the bigger picture of when to tune prompts versus weights, read Prompt tuning or fine-tuning? or take the Automatic prompt optimisation lesson.



