Sub topic 11
Test prompts with a small evaluation set
This chapter assumes you can call an LLM chat API, read basic Python, and handle JSON. It also assumes you have a system prompt with a fixed output format, like the Acme Billing prompt from earlier chapters.
Why a small evaluation set
A prompt change that fixes one case often breaks another. If you only try the prompt on the example you are looking at, you will not notice the break. An evaluation set (often called an eval set) is a fixed list of test inputs with checks on the outputs. You run it after every prompt change and compare the results.
A set of 15 to 30 cases is enough to start. You can grow it later.
What goes into the set
Pick cases that cover different kinds of behavior, not 20 versions of the same question.
- Normal cases: questions the documents answer clearly.
- Missing-answer cases: questions the documents do not answer. The prompt says the model must admit this and offer a human agent.
- Rule cases: requests for legal or tax advice, which the prompt forbids.
- Injection cases: documents or questions that contain instructions such as "ignore your rules". The prompt says to treat these as data.
- Format cases: any input where the reply tends to drift, such as adding text outside the JSON.
Every time a real failure shows up in production or in testing, add it to the set as a new case.
Two kinds of checks
Some requirements can be checked by code. Others need a person or a second model to judge.
| Check type | Example | How to run it |
|---|---|---|
| Format check | Reply is valid JSON with `answer` and `sources` | Code assertion |
| Exact fact check | `sources` equals `[2]` | Code assertion |
| Behavior check | Reply declines to give tax advice | Human review or a grader model |
Start with code checks. They are cheap, repeatable, and never disagree with themselves. Add judged checks only for behavior you cannot test with code.
A small runner
The check_reply function below tests the output format. The runner applies it, plus a per-case check on sources, to every case.
import json
def check_reply(raw: str) -> list[str]:
"""Return a list of problems found in a model reply; empty means it passed."""
try:
data = json.loads(raw)
except json.JSONDecodeError:
return ["reply is not valid JSON"]
if not isinstance(data, dict):
return ["reply is not a JSON object"]
problems = []
if not isinstance(data.get("answer"), str):
problems.append("'answer' must be a string")
sources = data.get("sources")
if not isinstance(sources, list) or not all(isinstance(s, int) for s in sources):
problems.append("'sources' must be a list of integers")
return problems
CASES = [
{"name": "normal", "expected_sources": [1]},
{"name": "missing_answer", "expected_sources": []},
]
def run_eval(call_model, cases):
"""call_model(case) returns the raw reply string for one case."""
failures = {}
for case in cases:
raw = call_model(case)
problems = check_reply(raw)
if not problems:
data = json.loads(raw)
if data["sources"] != case["expected_sources"]:
problems.append(f"expected sources {case['expected_sources']}, got {data['sources']}")
if problems:
failures[case["name"]] = problems
print(f"{len(cases) - len(failures)}/{len(cases)} passed")
return failures
if __name__ == "__main__":
fake_replies = {
"normal": '{"answer": "Due on the 5th.", "sources": [1]}',
"missing_answer": "Sorry, I cannot find that.",
}
print(run_eval(lambda case: fake_replies[case["name"]], CASES))Running it prints 1/2 passed and a dictionary showing that missing_answer failed because the reply was not valid JSON. In a real run, call_model builds the messages and calls your API.
Run the loop
Change one thing at a time. If you edit three rules together and the score moves, you will not know which edit caused it.
Common mistakes
- Model output varies between runs. Run each case more than once, or set the temperature low, before you trust a single pass or fail.
- A pass rate hides which cases broke. Always list failing cases by name.
- Only testing easy cases gives a false sense of safety. Include the hard and adversarial ones.
Exercise
Your eval set has 20 cases, and your edit raises the pass count from 16 to 17. Why should you still check which cases fail before keeping the edit?
Show answer
The edit may have fixed one case while breaking another that passed before. The total only shows the net change, so compare the lists of failing cases with the previous run.
Exercise
Name one requirement from the Acme Billing prompt that you can test with a code check, and one that needs a human or grader model.
Show answer
Code check: the reply is a JSON object with a string answer and an integer list sources. Judged check: the reply declines legal or tax advice, because the wording of a refusal varies and code cannot reliably detect it.