Sub topic 4
Protect quality with a small evaluation set
Every lever in this tutorial trades something for lower cost or latency. A smaller model, a shorter prompt, or a cached answer can each make output worse without any error message. This chapter builds a small evaluation set (an "eval set") that catches those losses before users do.
This chapter assumes you can write basic Python and have called an LLM API. You should also know what a prompt, a system message, and temperature are.
What an eval set is
An eval set is a fixed list of inputs, each paired with a way to judge the output. You run it before and after every change and compare scores. It works as a regression test for model behavior.
A set of 30 to 100 cases is enough to start. The goal is to detect large drops, not to publish a benchmark.
Choose cases that represent real traffic
Build the set from real requests, with personal data removed. Cover these groups:
- Common requests, which make up most of your traffic.
- Hard requests, such as long inputs, ambiguous questions, or multi-step tasks.
- Past failures, where the system gave a wrong or unsafe answer.
- Edge cases, such as empty input or text in another language.
Keep the set fixed between runs. If you change cases and prompts at the same time, you cannot tell which change moved the score.
Choose a check for each case
Prefer the cheapest check that works.
| Check type | Example | Strength | Weakness |
|---|---|---|---|
| Exact or rule-based | Output is valid JSON with a `category` field | Cheap, deterministic | Only suits structured output |
| Reference match | Answer contains the expected order number | Cheap, clear | Misses correct answers phrased differently |
| Model-graded | A second model scores the answer against a rubric | Handles free text | Costs tokens, can be biased or noisy |
| Human review | A person rates a sample | Most trusted | Slow and expensive |
For model-graded checks, give the judge a short rubric and ask for a pass or fail. Spot-check some judgments by hand to confirm the judge agrees with you.
A minimal runner
This code runs a set of cases through any function that calls your model and reports the pass rate.
def run_evals(cases, generate_fn):
"""cases: list of dicts with 'input' and 'check' (a function: output -> bool)."""
failures = []
for case in cases:
output = generate_fn(case["input"])
if not case["check"](output):
failures.append({"input": case["input"], "output": output})
pass_rate = 1 - len(failures) / len(cases)
return pass_rate, failures
cases = [
{"input": "2+2?", "check": lambda out: "4" in out},
{"input": "Capital of France?", "check": lambda out: "Paris" in out},
]
def fake_model(prompt):
return "The answer is 4." if "2+2" in prompt else "Paris"
rate, failures = run_evals(cases, fake_model)
print(f"pass rate: {rate:.0%}, failures: {len(failures)}")Replace fake_model with a real call. Record the pass rate together with the cost and latency numbers from your baseline, so each change shows all three.
Use the set as a gate
Set the threshold before you test, for example "no more than 2 points below the current pass rate". Deciding afterward invites you to accept whatever result you got.
Common errors
- Too few cases. With 10 cases, one failure moves the score by 10 points. Small differences are noise.
- Nondeterminism. With nonzero temperature, outputs vary between runs. Use a low temperature for evals, or run each case several times and average.
- Testing on what you tuned for. If you edit the prompt until every case passes, the set no longer predicts new traffic. Keep some cases unseen.
- Stale cases. Add new failures from production as they appear.
Exercise
You switch to a cheaper model. Cost falls 60%, and the pass rate on your 20-case eval set falls from 95% to 90%. Can you conclude the cheaper model is acceptable?
Show answer
Not yet. With 20 cases, a 5-point drop is exactly one case, which may be noise. Add more cases or repeat runs, and read the failing case to see whether it is a case type you care about. Then compare against the threshold you set beforehand.