Sub topic 9
Choose graders and metrics
This chapter assumes you can read Python and JSONL, you have a dataset with the schema from earlier chapters (id, input, reference, tags), and you know what a proportion and a sample mean are. A grader is the code or person that turns one model output into a score. A metric is how you combine those scores over the dataset.
Start from the success criteria
Pick the grader after you know what "correct" means for the task. Look at each success criterion you wrote and ask what evidence would show it was met. If the evidence is a string, a number, or valid JSON, you can check it with code. If it is tone, helpfulness, or faithfulness to a document, you need a human or a model to judge it.
Three kinds of grader
| Grader | Good for | Cost | Main risk |
|---|---|---|---|
| Code-based | Exact answers, labels, formats, tool calls | Very low, fully repeatable | Marks correct answers wrong when wording differs |
| Human | Subjective quality, final checks, building labels | High and slow | Raters disagree, and results vary between sessions |
| LLM judge | Open-ended text at scale | Moderate | Biases, drift when the judge model changes, and unverified trust |
Use the cheapest grader that can actually measure the criterion. Code graders come first, and a judge covers only what code cannot.
Code-based graders
The grader from the earlier example uses exact match after trimming and lowercasing. It works for short fixed answers, such as a class label. It fails on the refund example, because "No, returns are accepted within 30 days." is correct but does not match the reference string.
Here are some alternatives that stay in code:
- Normalized match: strip punctuation, collapse spaces, and compare.
- Contains or regex: require key facts, such as "30 days", to appear.
- Structured checks: parse the output as JSON and compare fields.
- Behavior checks: confirm the app called the right tool with the right arguments.
import json
import re
def grade_contains(output, required_phrases):
text = output.lower()
return all(p.lower() in text for p in required_phrases)
def grade_json_field(output, field, expected):
try:
data = json.loads(output)
except json.JSONDecodeError:
return False
return data.get(field) == expected
def grade_regex(output, pattern):
return re.search(pattern, output) is not None
print(grade_contains("No. Returns are accepted within 30 days.", ["30 days"]))
print(grade_json_field('{"label": "refund"}', "label", "refund"))
print(grade_regex("Order #4821 shipped", r"#\d{4}"))A contains check can pass an answer that also says something wrong, such as "Yes, and returns within 30 days are accepted." Add a forbidden-phrase check or move that criterion to a judge.
When to use an LLM judge
Use a judge when the criterion is about meaning and many wordings are valid. Give it a rubric with narrow questions, such as "Does the answer state the 30-day limit? Does it contradict the policy?" Ask for a short reason and a pass or fail label. A single 1-to-10 score is harder to keep consistent. The next chapters cover how to check a judge against human labels.
Choose the metric
Per-case scores are usually pass or fail, so the metric is accuracy, the fraction passed. Report it per tag, as run_eval does, as well as overall. A single average can hide a slice that fails badly. If failures differ in cost, add a second metric, such as the pass rate on the safety slice alone.
Exercise
A support bot must answer "Can I return a laptop after 45 days?" Reference: "No. Returns are accepted within 30 days of delivery." Exact match marks "No, returns are only accepted within 30 days." as wrong. Name two grader options and say which fits better.
Show answer
Option one is a contains check that requires "30 days" plus a negative answer, which is cheap and repeatable. Option two is an LLM judge with a rubric asking whether the answer denies the return and states the 30-day limit. The contains check fits better here, since the key facts are short and fixed. Use the judge only if wording varies too much for phrase checks.