Sub topic 11
Testing and Improving Prompts Systematically
This chapter assumes you can write a prompt with clear instructions, know what few-shot examples are, and can read short Python snippets. It covers how to replace "this seems better" with measured results.
Why guessing fails
Language models are variable. A prompt change that fixes one input can break another. If you test a new version on the one example that annoyed you, you can't tell whether the prompt improved or you got a lucky reply.
A systematic loop fixes this. You write down test cases, score a prompt against them, change one thing, and score again.
Build a small test set
A test set is a list of inputs paired with the answer you expect. For a first version, 15 to 30 cases is enough. Include three kinds:
- Typical cases that the prompt should handle easily.
- Edge cases, such as mixed feelings, empty input, or very long input.
- Past failures. Each time a prompt fails in real use, add that input to the set.
Write the expected answers before you run the prompt. If you write them after reading the model's replies, you will tend to accept whatever it said.
Keep a few cases aside and do not look at them while you tune the prompt. Check them only at the end. If the prompt scores well on cases you tuned against but poorly on the held-back ones, you have overfitted: the prompt works for your examples but not for new inputs.
Score with a simple function
The scoring function compares each reply to the expected label. This works when the task has a fixed set of answers, such as sentiment labels.
def accuracy(call_model, build_prompt, cases):
correct = 0
for text, expected in cases:
reply = call_model(build_prompt(text)).strip().lower()
if reply == expected:
correct += 1
return correct / len(cases)call_model is any function that sends a prompt to a model and returns its text. build_prompt turns one input into a full prompt. Passing both in as arguments lets you compare two prompt versions with the same code.
To try it without a real model, use a stand-in:
def fake_model(prompt):
return "Positive "
def prompt_v1(text):
return f"Classify the sentiment of this review: {text}\nSentiment:"
cases = [("Great battery.", "positive"), ("It broke.", "negative")]
print(accuracy(fake_model, prompt_v1, cases))This prints 0.5, because the fake model answers "positive" every time.
Compare versions fairly
To compare two prompts, run both on the same cases with the same settings.
- Score version 1 and record the number.
- Change one thing, such as adding the label list or a few-shot example.
- Score version 2 on the identical cases.
- Keep the change only if the score improves or the failures you care about disappear.
Changing several things at once hides which change helped. If you must change several, test them one at a time afterward.
Accuracy can hide problems, so also read the failures. Print each wrong case with the model's reply. The pattern is often obvious, for example "every wrong answer is a sentence that begins with 'Sure!'". That points to an output-format instruction, not a reasoning problem.
def show_failures(call_model, build_prompt, cases):
for text, expected in cases:
reply = call_model(build_prompt(text)).strip().lower()
if reply != expected:
print(f"INPUT: {text}\nEXPECTED: {expected}\nGOT: {reply}\n")Account for variability
If the model uses a temperature above zero (see the chapter on sampling parameters), the same prompt can score differently on each run. Two ways to handle this:
- Set the temperature to 0 or a low value while testing, so runs are closer to repeatable.
- Run the set several times and average the scores. A difference of one or two cases between runs is noise, not an improvement.
Small test sets are noisy. With 20 cases, one case is 5 percentage points. Treat small gains with suspicion.
Common errors
| Error | Result | Fix |
|---|---|---|
| Testing on one example | Fix that breaks other inputs | Use a test set |
| Tuning on all your cases | Overfitting | Hold some cases back |
| Changing many things at once | Unknown cause of improvement | Change one thing per run |
| Exact-match scoring on free text | Correct answers marked wrong | Normalize replies, or score with a rubric |
| Ignoring noise | Chasing random gains | Lower temperature, repeat runs |
For free-form output such as summaries, exact match does not work. Write a checklist instead, for example "mentions customer impact", "at most 3 bullets", and "says 'Cause unknown' when no cause is given". Many of these checks can be coded as simple rules. Others need a human reader, or a second model prompted to apply the checklist, and that second model's judgments need spot-checking too.
Exercise
Your prompt scores 17 of 20 on your test set. You add one sentence to the prompt and it now scores 18 of 20. Should you conclude the new sentence helped? What would you do next?
Show answer
Not yet. One case is within normal run-to-run noise, especially if temperature is above zero. Rerun both versions several times at low temperature and average the scores, read which cases changed, and check both versions on held-back cases. Keep the sentence only if the gain holds up or it fixes a failure pattern you care about.
Exercise
A summarization prompt is scored by exact match against a reference summary, and almost every case fails even though the summaries look good. What should you change?
Show answer
Exact match is the wrong scoring method for free text. Replace it with a checklist of requirements, such as length limit, required facts, and the "Cause unknown" rule. Check these with code where possible and by human review where not.