Sub topic 10
Measuring accuracy with a labeled set
This chapter assumes you can call a chat API from Python, read JSON, and know what accuracy, precision, and recall mean. It builds on the classify function from the validation chapter. You will score the classifier against examples whose correct labels you already know.
Why a labeled set
A prompt that looks good on three hand-picked messages tells you little. A labeled set is a list of (text, expected_label) pairs, where a person decided the expected label. Running the classifier over it gives a number you can compare after every change to the prompt or labels.
Build the set from real messages, not invented ones. Include ambiguous and messy cases, and include some that belong under other. Fifty to a few hundred examples is enough to start. Keep it separate from any examples you put in the prompt as few-shot examples. If the same message appears in both, your score will be too high.
Scoring with accuracy and a confusion matrix
Accuracy is the share of examples where the predicted label equals the expected label. It is simple, but it hides which labels fail. A confusion matrix counts each (expected, predicted) pair. Pairs where the two labels differ show which labels the model mixes up.
The function below runs classify over every example and returns both.
from collections import Counter
def evaluate(examples, call_llm):
if not examples:
raise ValueError("examples must not be empty")
confusion = Counter()
correct = 0
for text, expected in examples:
predicted, _ = classify(text, call_llm)
confusion[(expected, predicted)] += 1
if predicted == expected:
correct += 1
return correct / len(examples), confusionThe Counter keys are tuples, so a count for ("billing", "account") means a billing message was labeled account. A fallback from a failed parse appears as ("billing", "other"), so parse failures lower your score and show up in the matrix.
Running it
You can test the harness without a real model by passing a fake call_llm. This one always answers billing.
examples = [
("I was charged twice this month", "billing"),
("The app crashes when I upload a file", "technical"),
("I can't reset my password", "account"),
("Refund my last invoice please", "billing"),
]
def always_billing(prompt):
return '{"label": "billing", "confidence": 0.9}'
accuracy, confusion = evaluate(examples, always_billing)
print(accuracy)
for (expected, predicted), count in sorted(confusion.items()):
print(expected, "->", predicted, count)This prints an accuracy of 0.5 and the pairs account -> billing 1, billing -> billing 2, and technical -> billing 1. The always-billing model gets half right only because half the set is billing. That is why you need to look at the class balance.
Reading the numbers
- Class balance. If 70 percent of examples are billing, a model that always says billing scores 70 percent. Compare accuracy with the share of the largest class.
- Per-label recall. For one expected label, divide the correct count by the row total in the matrix. Low recall means that label's messages go elsewhere.
- Per-label precision. For one predicted label, divide the correct count by the column total. Low precision means the label catches things that do not belong.
- Fallback rate. Count how often the prediction is
other. A high rate suggests either unclear definitions or frequent malformed output.
Routing cares about these differences. A low-precision billing label sends the wrong work to the billing team. A low-recall technical label hides bugs in human triage.
Common errors
- Small samples. With 20 examples, one change moves accuracy by 5 points. Treat small differences as noise.
- Label mistakes. If the expected labels are wrong, you will chase errors that are not real. Check the labels when the model disagrees with you often.
- Ignoring confidence.
evaluatediscards confidence. Routing uses a threshold, so also measure how many examples fall below it and how accurate the rest are. - Nondeterminism. The same prompt can give different answers across runs. Run twice, or set temperature low, before you trust a small change.
Exercise
A set has 100 examples, 80 of them billing. Your classifier scores 0.82 accuracy. Is that good, and what would you check next?
Show answer
Not necessarily. A model that always answers billing scores 0.80, so 0.82 is barely better. Check the confusion matrix and the per-label recall for the 20 non-billing examples to see whether the model handles them at all.