esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

What Are Prompt Optimizers (e.g. DSPy)?

What a prompt optimiser is, how tools like DSPy search for better prompts, and where they go wrong, explained for your AI interview.

In short

A prompt optimiser is a tool that tries many versions of a prompt, scores each one on examples with known right answers, and keeps the best. DSPy is a 2023 programming model that does this for whole AI pipelines, but a high score still needs a fair test before you trust it.

11 min readAdvancedOccasional · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

A prompt optimiser is a tool that tries many versions of a prompt and keeps the one that scores best. I give it examples with known right answers and a score that says what good looks like. DSPy is a programming model from 2023 that does this for whole AI pipelines. The catch is that a high score can flatter you, so I always check the winner on emails it never saw.

What is a prompt optimiser, in plain words?

A prompt optimiser is a tool that finds better prompts by trial and error. It tries many versions, scores each one on real examples, and keeps the winner.

Writing prompts by hand often looks like this. They change a word, run a few tests, and call it done. That can work for one prompt, but it breaks when the model changes, the data shifts, or a second person edits the text. An optimiser turns that guessing into a loop a computer can run.

Think of a triage nurse who writes a checklist for the front desk. She drafts one, sends a day of patients through it, counts the mistakes, and rewrites until the mistakes drop. An optimiser runs that same loop for a model. Our claims team does the same with emails that go to fast-track, fraud check or missing documents, and the computer does the counting.

Key words

Prompt optimiser
A tool that tries many prompt versions and keeps the one that scores best.
Metric
The score that says what good looks like, such as the share of right answers.
Dev set
Labelled examples the optimiser is allowed to tune on.
Held-out set
Labelled examples kept back, used only to judge the winning prompt.
Demonstration
A worked example placed in the prompt to show the model the task.

The DSPy paper says existing language model pipelines are typically implemented using hard-coded "prompt templates", which are lengthy strings discovered via trial and error. Its answer is to write the program once and let a compiler tune it.

How does an optimiser find a better prompt?

It runs the loop a careful engineer would run, but faster, and with a score at every step. There are five parts.

  1. 1

    Write the metric

    Pick a score that matches the job, such as the share of emails sent to the right queue.

  2. 2

    Label real examples

    Collect inputs with the right answer, then split them. Tune on one part and keep the rest held back.

  3. 3

    Propose candidates

    The optimiser writes or picks new instructions and worked examples to try.

  4. 4

    Run and score each one

    Each candidate answers the dev set, and the metric scores the answers.

  5. 5

    Keep the best, then check it

    The top candidate gets one final run on the held-out set before anyone ships it.

How an optimiser searches for a prompt

Labelled emails

Real inputs with the right answer

Your draft prompt

The starting point to beat

Optimiser

Writes new prompt versions

Try and score

Run each candidate

The model answers the dev set

Score with the metric

Share of emails routed right

Best prompt

Highest score on the dev set

  1. 1The optimiser reads the examples and their right answers.
  2. 2It also starts from your draft, which it tries to beat.
  3. 3It sends each new candidate prompt to the model.
  4. 4The model's answers are compared with the labels.
  5. 5The top score wins, then gets a held-out check.
Labelled emails and a draft prompt go into the optimiser. It sends each candidate through the model, scores the answers, and keeps the best. The winner is trusted only after a held-out check.

The loop is only as good as its inputs. A score means something only when the metric measures the real goal, and the examples look like real traffic.

What are the main kinds of optimiser?

Three families show up in papers and tools. They differ in what they change.

  • Wording

    Rewrite the instruction

    APE, a 2022 paper, treats the instruction as the program and searches over candidates an LLM proposes. OPRO, a 2023 paper, asks an LLM to write new solutions from earlier scores.

  • Examples

    Pick the demonstrations

    DSPy modules learn by creating and collecting demonstrations.

  • Reflection

    Learn from the mistakes

    GEPA, a 2025 paper, samples what the system did and reflects on it in natural language.

  • “We design a compiler that will optimize any DSPy pipeline to maximize a given metric.”

    DSPy paper

  • “we treat the instruction as the "program," optimized by searching over a pool of instruction candidates proposed by an LLM in order to maximize a chosen score function”

    APE paper

Pick the family that fits the failure. If the model has the right idea but misses the format, demonstrations often help. If it keeps failing the same way, reflection can write the rule that stops it.

What does a small optimiser run look like?

Take a claims team that gets thousands of emails a week and sorts each one into fast-track, fraud check or missing documents. The hand-written prompt is the baseline. The demo below is small and made up: ten dev emails and three candidates. A real dev set would hold a few hundred labelled emails.

Python
# A simplified optimizer step for a claims triage prompt. No real model: each list is the
# label one candidate prompt gave to the same 10 dev emails (made-up numbers).
GOLD = ["fast", "fraud", "docs", "fast", "fast", "fraud", "docs", "fast", "docs", "fraud"]
RUNS = {
    "hand-written": ["fast", "fraud", "docs", "fast", "docs", "fraud", "docs", "fast", "docs", "fast"],
    "candidate-1": ["fast", "fraud", "docs", "fast", "fast", "fraud", "fast", "fast", "docs", "fraud"],
    "candidate-2": ["fast", "fraud", "fraud", "fast", "fast", "fraud", "fast", "docs", "docs", "fast"],
}

def accuracy(pred, gold):
    # The metric: the share of emails sent to the right queue, as a whole percent.
    return 100 * sum(p == g for p, g in zip(pred, gold)) // len(gold)

scores = {name: accuracy(pred, GOLD) for name, pred in RUNS.items()}
for name, score in scores.items():
    print(f"{name}: {score}%")
best = max(scores, key=scores.get)
print("Best on dev set:", best)
# A gate, not a guess: ship only a clear gain, then check it on a held-out set.
if scores[best] - scores["hand-written"] >= 10:
    print("Ship", best, "after a held-out check")
else:
    print("Keep the hand-written prompt: the gain is too small")

Output

hand-written: 80%
candidate-1: 90%
candidate-2: 60%
Best on dev set: candidate-1
Ship candidate-1 after a held-out check

The hand-written prompt scores 80%. Candidate 1 scores 90%, a gain of ten points, so the gate says ship it after a held-out check. Candidate 2 scores 60% and drops out.

Look closer at the mistakes. The hand-written prompt sends a fraud claim to fast-track, the costliest slip. Candidate 1 fixes that, but sends one claim that needs documents to fast-track too. A real metric would weight each slip by its cost, and this demo's plain share does not.

Why can a high score still mislead?

Why a dev score can flatter you

Tuned on these emails

Dev set score: 92%

Same emails it was tuned on

Never seen while tuning

Held-out score: 81%

New emails from the same queue

Looks like a win

The real gain may be smaller

Ship or keep draft

Decided on unseen emails

  1. 1A high score on the emails it tuned on can mislead.
  2. 2The held-out score is the one that decides what ships.
Two made-up scores for one prompt. The score on tuned emails looks like a win, but the held-out score is the one that decides what ships.

An optimiser fits whatever it is shown, so a dev score is partly memory. Keep a held-out set it never sees, and never tune on it, or it stops being a test.

  • The metric

    An optimiser makes a bad metric look good. If the score ignores costly mistakes, the winner will too.

  • The data

    Ten emails teach almost nothing. A dev set that misses rare cases will miss them in the winner too.

  • The cost

    Each candidate usually needs a run over the dev set, so one search makes many model calls.

  • The model

    A prompt tuned on one model can stop working after the provider changes it, so rerun the search.

Tip

Asked about cost in an interview: each candidate usually needs a run over the dev set, so a search makes many model calls. Cap the rounds, and budget for them.

How would you decide whether to use an optimiser?

Start with a clear metric and a few hundred labelled examples. If the hand-written prompt already clears the bar on a held-out set, stop there. If it does not, and the failures repeat, run an optimiser.

Keep the winner only when it beats the draft on the held-out set, by a margin you chose before the run. Then rerun the search when the model changes.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: a tool that searches for better prompts using a score.
  • The three inputs: a metric, labelled examples and a starting prompt.
  • A dev set to tune on, and a held-out set to judge the winner.
  • The kinds of optimiser, and what each one changes.
  • The limits: metric quality, overfitting, cost and model changes.

Common traps

  • Trap"The optimiser finds the perfect prompt."

    BetterIt finds the best of the candidates it tried, on the data and metric you gave it.

  • Trap"A higher dev score means the prompt is better."

    BetterOnly the held-out score tells you that. The dev score can be memorised.

  • Trap"Optimisers are free because they run automatically."

    BetterEach candidate usually costs a run over the dev set, so the search costs real model calls.

Can you spot the weak check?

Spot the leak

Which setup would make a reported 88% score unreliable?

Quick check

An optimiser lifts dev accuracy from 80% to 92%, but the held-out score is 81%. What is the best next step?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q100How do you test prompts systematically?12 min
  2. Q098How do you pick and order few-shot examples?12 min
  3. Q102Why do prompts break when you switch models?11 min
  4. Q110Where is the line between prompting and fine-tuning?11 min

Last updated on 27 November 2026

Tuning prompts for a system that has to work on real data?

I design and build AI agents, including the prompts and the tests that show they work on real examples. Book a short call and tell me what your system needs to do.