Skip to content
AI360XpertBeta

Automatic Prompt Optimization

You stop hand editing prompts. You score each version on a small test set, let another model propose fixes for the misses, and keep the version with the best score.

Automatic prompt optimization scores each prompt on the same 6 name cases and keeps the winner, lifting exact match from 2 of 6 to 5 of 6
Automatic prompt optimization scores each prompt on the same 6 name cases and keeps the winner, lifting exact match from 2 of 6 to 5 of 6

Why Does This Exist?

You fix one prompt case and break three others. That's the normal pain, not bad luck.

Say your prompt strips names from text. It handles "Ada Lovelace" fine. Then "Dr. John Smith" arrives and your code gets "Dr. John Smith" instead of "John Smith". You add "do not include titles like Dr." Now "Mr. Adam West" still slips through, or your fix changes phrasing enough that a case that passed last week starts failing. Hand edits chase single misses. They never tell you what they broke.

Automatic Prompt Optimization (APO) turns that guessing into a search. You keep a small scored test set, you generate prompt variants, you score every variant on the same set, and you keep the one with the best score. This page covers that loop: the score, where variants come from, and how selection works. The test harness itself lives in prompt regression testing. What you often tune inside the loop, the examples, lives in few-shot prompting.

You need one prerequisite to use any of this. A scored dev set of inputs with expected answers. No set, no optimization. If you have six to fifty cases you trust, APO pays off. If you have two cases and vibes, fix the prompt by hand.

Think of It Like This

Tasting spoons and a rewritten recipe card

You cook a soup and set out six small tasting spoons, each one checking a different thing: salt, thickness, heat, lemon, garlic, texture. Six spoons, six yes or no verdicts. Your first recipe card scores 2 of 6. Not good.

You hand the card plus the four failed spoons to a friend. Your friend tastes only the failures, spots the pattern (every miss needs acid, not more salt), and rewrites the card with one added line: "finish with lemon." You taste all six spoons again. Now it scores 5 of 6. You keep the new card because it won on the full set, not just on the misses.

Here is where the kitchen stops helping. Soup doesn't change its mind about salt when you reword the card. A language model does. One added sentence can shift answers on cases that passed before, which is why you must rescore everything each round. And six spoons never describe every dinner guest, so a card that wins your spoons can still miss in production.

How It Actually Works

Call the prompt text PP. Call your dev set DD, a list of nn pairs (xi,yi)(x_i, y_i) where xix_i is an input in plain words and yiy_i is the expected answer in plain words. Call the model output with that prompt fP(xi)f_P(x_i). Call your scoring rule mm, a small function that compares one output to one expected answer and returns 1 for a match and 0 for a miss. Exact string match is the simplest mm. A judge model or a format check can replace it when answers vary in wording.

The score you are maximizing

The mean score of a prompt is:

S(P)=1n∑i=1nm(fP(xi),yi)S(P) = \frac{1}{n} \sum_{i=1}^{n} m(f_P(x_i), y_i)

In plain words: S(P)S(P) is the fraction your prompt gets right. The symbol SS is the score, PP is the prompt, nn is the case count, and the sum adds one point per match. Your goal is the prompt with the top mean, written P∗=arg⁡max⁡PS(P)P^* = \arg\max_P S(P), which reads as the PP that makes S(P)S(P) largest. You never trust a win on the misses alone. A new prompt must beat the old one on the full DD, or you keep the old one.

Where new prompts come from

You propose, then you select. Proposing has three common flavors, and most systems mix them.

First, paraphrase. You ask a model to restate PP several ways without changing its meaning, then you score each restatement. Wording matters to models, so this cheap step often finds a point or two.

Second, error feedback. You feed the current PP plus its failed cases into an optimizer model with one job: name the missing rule, then rewrite PP to add it. The optimizer output is text, not numbers, which people sometimes call a textual gradient. It points at the flaw the way a gradient points uphill, except you can't trust its size. You still have to score the rewrite.

Third, search with memory. You keep the top few prompts, mutate each one, score the children, and drop the losers. Programmatic frameworks push this further and also choose the examples inside the prompt, so the compiled prompt carries both better wording and better demonstrations. That is the idea behind compiled prompts, and the reason APO pairs so well with in-context learning.

The trade is direct. You buy higher scores with eval calls, latency, and a test set you must maintain. Skip APO when you have almost no scored cases, when the task needs facts the model lacks (reach for retrieval or instruction tuning instead), or when the prompt already passes and your misses come from unclear requirements.

Worked example: six names, two prompts

Your dev set has n=6n = 6 cases. The job is to output first and last name only.

  1. "Dr. John Smith" points at "John Smith"
  2. "Mary Jenkins, PhD" points at "Mary Jenkins"
  3. "Mr. Adam West" points at "Adam West"
  4. "Ada Lovelace" points at "Ada Lovelace"
  5. "Sir Patrick Stewart" points at "Patrick Stewart"
  6. "Ursula Le Guin" points at "Ursula Le Guin"

Start with P0P_0 as "Extract the name." Run it and suppose the model copies its input word for word. It matches only cases 4 and 6, the two names that were already clean. So S(P0)=2/6≈0.33S(P_0) = 2 / 6 \approx 0.33.

Feed P0P_0 and its four misses to the optimizer. It spots the pattern: three misses carry titles before the name and one carries a degree after it. It proposes P1P_1 as "Extract the name. Strip titles such as Dr. and Mr. and degrees such as PhD. Output first and last name only."

Score P1P_1 on all six. It now matches cases 1, 2, 3, 4, and 6. It still misses case 5 because its rule lists Dr. and Mr. but never mentions Sir, so "Sir Patrick Stewart" survives. That gives S(P1)=5/6≈0.83S(P_1) = 5 / 6 \approx 0.83. The gain is 5/6−2/6=3/6=0.505/6 - 2/6 = 3/6 = 0.50. You keep P1P_1 because 0.83 beats 0.33 on the full set.

The next round teaches the honest lesson. If the optimizer just appends "and Sir" to the list, the score hits 6/6=1.006/6 = 1.00 on your six cases and you feel done. But a list of three titles is not a rule. Production will send Mrs., Ms., and Lord, and your perfect prompt will miss them. A general line such as "strip any honorific before the name" generalizes. An enumerated list memorizes. Your dev set of six can't tell the difference, which is why the assumption matters: APO assumes DD looks like production and mm measures what you care about. When that assumption fails, the score climbs and quality does not.

Code

This snippet is the selection half of the loop, with no model calls. It shows how a 2 of 6 prompt loses to a 5 of 6 prompt on the same cases.

def mean_score(matches: list[int]) -> float:    return sum(matches) / len(matches)  # fraction correct on the full set

p0 = [0, 0, 0, 1, 0, 1]  # misses, misses, miss, hit, miss, hitp1 = [1, 1, 1, 1, 0, 1]  # only the Sir case still misses
print(round(mean_score(p0), 2))  # -> 0.33print(round(mean_score(p1), 2))  # -> 0.83print(sum(p1) - sum(p0))  # -> 3

The outputs are real: 2 divided by 6 rounds to 0.33, 5 divided by 6 rounds to 0.83, and the gain is 3 cases. In a full loop you would generate P1P_1 with an optimizer model, then run this same comparison before you ship anything. If you chain tuned prompts into a pipeline, read prompt chaining next, because each step needs its own set.

Watch Out For

Your winner only wins on your six cases

Symptom: the dev score hits 1.00 and production still misses. Your set named Dr., Mr., and PhD, so the optimizer memorized those three strings. Then Mrs., Ms., and Lord arrive and fail. Small sets reward memorization over rules.

Fix: split your cases into a dev set the optimizer can see and a held out set it never sees. Require the winner to improve the held out score, not just the dev score. Add every production miss to the set with its expected answer, and force the winning prompt to state general rules ("strip any honorific") instead of enumerating the three titles you happened to test.

The score climbs while the task drifts

Symptom: S(P)S(P) rises but spot checks look wrong. The optimizer shortened answers to game an exact match rule, dropped a format your code parses, or answered a nearby easier question. You optimized the metric, not the job.

Fix: pin the output contract in the test, not just in the prompt. Check exact text plus format (first and last name only, no titles, no extra words). Keep one or two regression cases that fail when the format slips, and read the winning prompt yourself before it ships. Never ship a prompt you have not read.

The Quick Version

  • Automatic prompt optimization treats the prompt as a candidate to search, not text to polish by feel.
  • Each candidate is scored on the same dev set with S(P)S(P), the fraction of cases it gets right.
  • An optimizer model reads the misses, names the missing rule, and proposes a rewrite, which must then beat the old prompt on the full set.
  • A 6 case name cleanup run shows the pattern: a bare prompt scores 2 of 6, and one added title stripping rule moves it to 5 of 6.
  • The method assumes the dev set looks like production and the metric measures what matters. Small or narrow sets produce winners that memorize instead of generalize.