Sub topic 11
Reading errors and improving the prompt and labels
This chapter assumes you can call an LLM from Python, read JSON, and know what accuracy means. It also assumes you have the classify and evaluate functions from the earlier chapters. Accuracy tells you how often the classifier is wrong. It does not tell you why. Error analysis answers the why, and it tells you what to change.
Start with the confusion counts
The evaluate function returns accuracy and a Counter of (expected, predicted) pairs. Look at the off-diagonal pairs, where the two values differ. These show which labels get mixed up.
from collections import Counter
def top_confusions(confusion, n=5):
errors = Counter(
{pair: count for pair, count in confusion.items() if pair[0] != pair[1]}
)
return errors.most_common(n)
confusion = Counter({
("billing", "billing"): 18,
("technical", "technical"): 15,
("account", "account"): 12,
("billing", "account"): 6,
("technical", "other"): 4,
("account", "technical"): 1,
})
for (expected, predicted), count in top_confusions(confusion):
print(f"{expected} -> {predicted}: {count}")This prints billing -> account: 6, technical -> other: 4, and account -> technical: 1. One pair, billing mistaken for account, accounts for more than half of the errors. Fix that pair first.
Read the actual messages
Counts point to a problem. The messages explain it. For the top confusion pair, print every failing example and read it.
def failures(examples, call_llm, expected_label, predicted_label):
found = []
for text, expected in examples:
if expected != expected_label:
continue
predicted, confidence = classify(text, call_llm)
if predicted == predicted_label:
found.append((text, confidence))
return foundReading about 10 failures is usually enough to see a pattern. Keep the confidence next to each message. Wrong answers with high confidence suggest the model misreads your definitions. Wrong answers with low confidence suggest the case is truly ambiguous.
Sort errors by cause
Different causes need different fixes.
| Cause | What you see | Fix |
|---|---|---|
| Overlapping labels | Two labels both fit the message | Sharpen the definitions or merge the labels |
| Vague definition | The model guesses differently on similar messages | Add concrete wording to the definition |
| Missing label | Many items land in "other" | Add a label for that group |
| Ambiguous case | Humans disagree too | Add a few-shot example or a tie-break rule |
| Wrong expected label | The model is right and your data is wrong | Correct the labeled set |
Check the last row first. In many projects, a share of the "errors" are labeling mistakes in the test set.
Change one thing at a time
Suppose the billing-to-account errors are messages like "I was charged after I closed my account." Both labels fit. You could edit the definitions so the tie is broken explicitly:
LABELS = {
"billing": "Charges, invoices, refunds, and payment failures. "
"Use this when money is involved, even if the account is also mentioned.",
"technical": "Bugs, errors, outages, and how-to questions about the product",
"account": "Login, password, profile, and permission changes",
"other": "Anything that does not clearly fit the labels above",
}Then rerun evaluate on the same set and compare. If accuracy rises and the billing-to-account count falls, keep the change. If another pair gets worse, you have moved the problem, so revert or adjust. Changing several things at once makes it impossible to know which change helped.
Know when to stop
Some errors will remain. If the remaining errors are rare or are cases that people also disagree about, route low-confidence results to human triage instead of adding more rules. Stop when changes no longer move the confusion counts in a meaningful way.
Exercise
Your confusion counts show "technical -> other: 9" and no other large errors. Reading the failures, you find most are questions about exporting data to CSV. What is the most likely cause, and what are two possible fixes?
Show answer
The cause is a missing or vague definition: the "technical" description does not clearly cover how-to questions about features such as export, so the model falls back to "other". One fix is to extend the technical definition to name feature usage and data export explicitly. Another is to add a few-shot example of an export question labeled "technical". Rerun the evaluation afterward and check that "technical -> other" falls without new errors appearing elsewhere.