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

AI and LLM interview question Q101, Prompt and context engineering. Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-are-prompt-optimizers-e-g-dspy

## Say it in 30 seconds

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.

## Say it in 2 minutes

A prompt optimiser does the trial and error for you. You give it a starting prompt, a score that says what good looks like, and examples with known right answers. It writes new versions of the prompt, runs each one on the examples, and keeps the best. Without it, someone tweaks the wording by hand and hopes.

DSPy takes the idea further. The 2023 paper describes it as a programming model for AI pipelines, where the parts can learn from examples. Its compiler optimises the whole pipeline to maximise a given metric. The parts can learn by creating and collecting demonstrations, rather than relying on hard-coded "prompt templates".

There are three broad kinds. Some tools have an LLM propose new instructions, as APE did in 2022 and OPRO in 2023. Some pick the demonstrations that worked. Some reflect on the failures and write rules, as GEPA does in its 2025 paper. Each one still needs a metric and labelled data.

The catch is the score. An optimiser fits the examples it sees, so a high dev score can be partly memory. So I keep a held-out set it never sees, and only ship a winner that holds up there. Each candidate also costs model calls, and a tuned prompt can stop working when the model changes.

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

- **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. **Write the metric:** Pick a score that matches the job, such as the share of emails sent to the right queue.
2. **Label real examples:** Collect inputs with the right answer, then split them. Tune on one part and keep the rest held back.
3. **Propose candidates:** The optimiser writes or picks new instructions and worked examples to try.
4. **Run and score each one:** Each candidate answers the dev set, and the metric scores the answers.
5. **Keep the best, then check it:** The top candidate gets one final run on the held-out set before anyone ships it.

*Diagram, How an optimiser searches for a prompt: 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.*
1. The optimiser reads the examples and their right answers.
2. It also starts from your draft, which it tries to beat.
3. It sends each new candidate prompt to the model.
4. The model's answers are compared with the labels.
5. The top score wins, then gets 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:

```text
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?

*Diagram, Why a dev score can flatter you: 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.*
1. A high score on the emails it tuned on can mislead.
2. 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." Better: It 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." Better: Only the held-out score tells you that. The dev score can be memorised.
- Trap: "Optimisers are free because they run automatically." Better: Each 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?

A. The dev set and the held-out set are separate, and the held-out set is run once at the end.
B. The optimiser was tuned on the same 300 emails that the final 88% score is measured on.
C. The metric counts a correctly routed email as one point, and a wrong one as zero.
D. The winning prompt is written in plain English, with a short role line at the top.

**Answer:** B. Scoring on the emails it tuned on measures memory, not skill. Keep a held-out set back and never tune on it.

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

A. Ship the 92% prompt, since the dev set is larger
B. Keep the draft, or search again and judge the winner on fresh held-out emails
C. Add more examples to the dev set and skip the held-out check
D. Ship the 92% prompt, then watch its score in production

**Answer:** B. The held-out score (81%) is far below the dev score (92%), so the dev gain is probably memory, not skill. Keep the draft unless a new run beats it on emails it never saw.

## Can you answer these from memory?

**Flash cards**

- **What is a prompt optimiser?** A tool that tries many prompt versions and keeps the one that scores best.
- **What is DSPy?** A 2023 programming model that tunes AI pipelines to maximise a given metric.
- **Dev set vs held-out set?** The dev set tunes the prompt. The held-out set judges the winner on unseen emails.
- **Three kinds of optimiser?** Rewrite the wording, pick demonstrations, or reflect on mistakes and write rules.
- **The biggest trap?** A high dev score that the optimiser has memorised from the data it tuned on.
- **The cost trap?** Each candidate usually needs a run over the dev set, so it costs many model calls.

## Likely follow-up questions

- Q100 How do you test prompts systematically? https://designervenkat.online/interview/ai/how-to-test-prompts-systematically
- Q098 How do you pick and order few-shot examples? https://designervenkat.online/interview/ai/how-do-you-pick-and-order-few-shot-examples
- Q102 Why do prompts break when you switch models? https://designervenkat.online/interview/ai/why-do-prompts-break-when-you-switch-models
- Q110 Where is the line between prompting and fine-tuning? https://designervenkat.online/interview/ai/where-is-the-line-between-prompting-and-fine-tuning
