Sub topic 12
Error analysis: turning failures into improvements
This chapter assumes you can run the evaluation harness from earlier chapters and that you have per-case results with scores. A pass rate tells you how often the application fails. It does not tell you why. Error analysis is the step that turns a list of failed cases into a ranked list of fixes.
Read the failures first
Start by reading failed cases directly. Pull 30 to 50 failures, or all of them if there are fewer. For each one, look at the input, the retrieved context if any, the output, and the expected result. Write a short note on what went wrong in plain words, such as "answered from memory instead of the context" or "returned a date in the wrong format".
Do this before you choose a fix. Reading failures usually shows problems that no metric was built to detect, including labels that are wrong.
Group notes into failure categories
After a first pass, cluster the notes into a small set of categories. Aim for five to ten. Each case gets one primary category. Keep an "other" bucket and review it, because it often holds a new category.
Typical categories for a retrieval-based question answering app:
| Category | Symptom | Likely fix area |
|---|---|---|
| Retrieval miss | Relevant document not in top k | Chunking, embeddings, k |
| Unfaithful answer | Claim not in the context | Prompt, model, grounding instructions |
| Format error | Output breaks the schema | Output constraints, parsing |
| Refusal or hedge | Declines an answerable question | Prompt wording |
| Bad label | Expected answer is wrong | Dataset |
Bad labels belong in the table. Fixing the dataset is a legitimate outcome of error analysis.
Count and rank
Counts show where effort pays off. Tag each failed result with a category and tally them.
from collections import Counter
def tally_failures(results, tags):
"""results: list of dicts with "id" and "passed".
tags: dict mapping case id to a failure category."""
failed = [r for r in results if not r["passed"]]
counts = Counter(tags.get(r["id"], "untagged") for r in failed)
total = len(failed)
return [(cat, n, n / total) for cat, n in counts.most_common()]
results = [
{"id": "c1", "passed": False},
{"id": "c2", "passed": False},
{"id": "c3", "passed": False},
{"id": "c4", "passed": True},
]
tags = {"c1": "retrieval_miss", "c2": "retrieval_miss", "c3": "format_error"}
for category, n, share in tally_failures(results, tags):
print(f"{category}: {n} ({share:.0%})")This prints retrieval_miss: 2 (67%) and format_error: 1 (33%). Fix the largest category first, unless a smaller one is cheap to fix or carries higher risk.
The improvement loop
Each fix should be tested against the same dataset, so you can tell whether it helped.
Change one thing at a time. If you change the prompt and the retriever together, you cannot say which change moved the score.
Common errors
- Fixing the first failure you see. One vivid failure may be rare. Use counts.
- Tuning to the test cases. If you add the failing inputs as examples in the prompt, the pass rate rises without the application improving. Keep a held-out set that you do not inspect.
- Ignoring passes. Skim a sample of passing cases too. A lenient scorer can hide failures.
- Forgetting that small changes are noisy. Check that a gain exceeds sampling noise, as in the earlier chapter on uncertainty.
Exercise
An eval run has 40 failures. Tagging shows 22 retrieval misses, 10 format errors, 6 unfaithful answers and 2 bad labels. A teammate wants to rewrite the generation prompt to reduce unfaithful answers. What do you recommend, and what would you check afterward?
Show answer
Start with retrieval misses, since they are 55% of failures (22 of 40) and an unfaithful answer is often a downstream effect of missing context. Change the retriever settings or chunking, then rerun the same dataset and compare the retrieval miss count and the overall pass rate, with a confidence interval to rule out noise. Also fix the 2 bad labels, which is cheap. Revisit the prompt once the larger categories have shrunk.