Sub topic 15
Testing and evaluating LLM features
This chapter assumes you can write basic Python, read JSON, and have built at least one prompt-based feature, such as the ticket classifier from the earlier chapters. It shows how to measure whether an LLM feature works and whether a change made it better or worse.
Why ordinary tests are not enough
A model's output can change between runs, and many tasks have more than one acceptable answer. You can't assert that the output equals one fixed string. Instead, you build a small set of test cases, run the feature on all of them, and compute a score. You then compare scores before and after each change to a prompt, model, or retrieval setup.
Build an evaluation set
An evaluation set is a list of inputs paired with the expected result. Start with 20 to 50 cases. Take them from real usage where you can, and include the hard ones: ambiguous tickets, empty input, and inputs that have caused failures before. Every time a bug is reported, add it as a new case.
Score the classifier
For tasks with a fixed set of labels, an exact match is a fair check. The function below takes the classify function from the earlier chapter and the complete function that wraps your model API. It returns accuracy and the list of failures.
def evaluate(cases, classify, complete):
failures = []
for ticket, expected in cases:
got = classify(ticket, complete)
if got != expected:
failures.append((ticket, expected, got))
accuracy = 1 - len(failures) / len(cases)
return accuracy, failures
cases = [
("I was charged twice this month", "billing"),
("The app crashes when I tap Save", "bug"),
("Please add a dark mode", "feature_request"),
]
def fake_complete(prompt):
return "billing"
accuracy, failures = evaluate(cases, lambda t, c: "billing", fake_complete)
print(accuracy)
print(failures)The stand-in model here always answers "billing", so it gets one of three right. Read the failures, not just the number. They show which kinds of input the prompt handles badly.
Choose a check that fits the output
| Output type | Check |
|---|---|
| A label | Exact match, plus a confusion count per label |
| JSON | Parse it and validate the fields, as in `parse_invoice` |
| Retrieved passages | Does the correct document appear in the top k results? |
| Free text | A rubric, human review, or a second model acting as a judge |
A model used as a judge is cheap and fast, but it has its own biases. Check its verdicts against human ratings on a sample before you trust it.
Run the loop
Common errors
- Tuning on the test set. If you edit the prompt until every case passes, the score no longer predicts real performance. Hold some cases back and look at them only occasionally.
- Running each case once. Outputs vary, so repeat noisy cases several times, or set the temperature low.
- Too few cases. With 10 cases, one change moves accuracy by 10 points. Treat small differences as noise.
- Only measuring averages. A 95% score can hide a label that always fails.
Exercise
Your prompt change raises accuracy from 80% to 84% on a set of 25 cases. Should you ship it? Explain.
Show answer
Not on that evidence alone. One case is 4 points, so the gain is a single extra correct answer, which could be noise. Add more cases, repeat the runs, and check that the failures that remain are not in a category that got worse.