Skip to content

Training

GEPA, MIPROv2 and OPRO: automatic prompt optimization explained

How APE, OPRO, MIPROv2 and GEPA propose and select prompts, what each paper reported, how to run MIPROv2 and GEPA in DSPy, what a run costs in model calls, and how they mislead you.

By Will Schulz9 min read
A branching tree of glass sheets rising upward, each generation brighter, with the single topmost sheet glowing red

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 on

Every 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

MethodProposes fromKeepsOptimisesBest for
APEInput-output examplesTop scorersInstructionSimple single-call tasks
OPROPast prompts and scoresTop scorersInstructionTasks with a clean numeric metric
MIPROv2Data summary, program, demosBayesian surrogate's bestInstructions + few-shot demos per moduleMulti-step DSPy pipelines
GEPATraces and textual feedbackPareto frontierInstructions per moduleAgents 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.

Frequently asked questions

What is GEPA prompt optimization?
GEPA (Agrawal et al., 2025) is a prompt optimiser that reads execution traces and textual feedback to rewrite instructions, and keeps a Pareto frontier of candidates that are each best on at least one example. Across six tasks it beat GRPO by 6% on average with up to 35 times fewer rollouts, and beat MIPROv2 by over 10%.
What is the difference between MIPROv2 and GEPA?
MIPROv2 searches combinations of instructions and few-shot demonstrations with a Bayesian surrogate model over minibatch scores. GEPA rewrites instructions by reflecting on traces and feedback text, and selects candidates from a Pareto frontier rather than a single best.
What is OPRO?
OPRO (Large Language Models as Optimizers, Google DeepMind) shows an LLM previous prompts with their scores and asks for a better one. Its prompts beat human-designed ones by up to 8% on GSM8K and up to 50% on Big-Bench Hard.
How do I use GEPA in DSPy?
Write a metric that returns a score and, ideally, feedback text as dspy.Prediction(score=..., feedback=...), then call dspy.GEPA(metric=metric, ...) with a reflection model and compile your program with a training and validation set. Check the DSPy docs for current arguments.
Does automatic prompt optimization overfit?
It can. Optimisers tune to the examples they see, can leak expected answers into prompts, and can mistake minibatch noise for gains. Confirm the final prompt on held-out cases with a paired significance test.

Go deeper in the Academy

Free lessons with quizzes, citations and a hands-on exercise.

Sources

  1. [1]Large Language Models Are Human-Level Prompt Engineers. Zhou et al., arXiv (2022).
  2. [2]Large Language Models as Optimizers. Yang et al., Google DeepMind, arXiv (2023).
  3. [3]Optimizing Instructions and Demonstrations for Multi-Stage Language Model Programs. Opsahl-Ong et al., EMNLP, arXiv (2024).
  4. [4]DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines. Khattab et al., arXiv (2023).
  5. [5]GEPA: Reflective Prompt Evolution Can Outperform Reinforcement Learning. Agrawal et al., arXiv (2025).
  6. [6]dspy.GEPA overview. DSPy documentation.

Put it into practice.

Kojen gives you the Forge, LLM-judged evaluations, red-team simulations and live endpoints in one place. Free to start, no card needed, then pay as you go.

Start training free

More reading

All articles →