Sub topic 7
Grading outputs: code checks, human review, and model judges
A grader is the part of an evaluation that looks at one output from your AI feature and decides how good it is. This chapter assumes you have already named your quality dimensions, written testable requirements, and built an evaluation set. It covers the three kinds of grader and how to pick one for each dimension. Turning grades into metrics and calibrating a model judge in detail come in later chapters.
What a grader does
A grader takes a case from the evaluation set and the output your feature produced for it. It returns a verdict. The verdict can be pass or fail, a score on a small scale, or a label such as "supported", "partly supported", or "contradicted".
Your requirements decide what the grader must measure. A requirement like "at least 95% of refund answers make only claims supported by the policy text" needs a grader that can judge support. A requirement like "the reply is valid JSON with a status field" needs a different one. Choose the grader from the dimension, not from what is convenient to build.
There are three families:
- Code-based graders are programs that check the output against rules.
- Human graders are people who read the output and apply a rubric.
- Model-based graders (judges) are language models prompted to apply a rubric.
Code-based graders
A code grader is a function. It runs the same way every time, costs almost nothing, and can run on every case in seconds. That makes it the right default for any property you can state as a rule.
Good targets for code checks:
- Format: valid JSON, required fields present, length within limits.
- Required and forbidden content: a legal disclaimer must appear, a competitor name must not.
- Exact facts: a refund window of 30 days must appear as "30 days", or a computed total must equal the known total.
- Safety patterns: no email addresses, card numbers, or internal URLs in the output.
Here is the grader and runner from this tutorial's running example, with a stub standing in for the real feature so the code runs as shown.
def grade_case(case, output):
text = output.lower()
includes_ok = all(term.lower() in text for term in case.get("must_include", []))
excludes_ok = not any(term.lower() in text for term in case.get("must_not_include", []))
return includes_ok and excludes_ok
def run_eval(cases, generate):
results = []
for case in cases:
output = generate(case["input"])
results.append({"id": case["id"], "severity": case.get("severity_if_failed", "minor"), "passed": grade_case(case, output)})
return results
cases = [
{
"id": "refund-014",
"input": "I bought this 45 days ago and it arrived damaged. Can I still return it?",
"must_include": ["damaged"],
"must_not_include": ["guaranteed refund"],
"severity_if_failed": "critical",
}
]
def fake_generate(prompt):
return "Because the item arrived damaged, please contact support so we can review your return."
print(run_eval(cases, fake_generate))The generate argument wraps whatever you are testing: a prompt template, a retrieval pipeline, or a whole agent. The grader never needs to know how the output was produced.
A format check is a second common pattern.
import json
def grade_format(output, required_keys):
try:
data = json.loads(output)
except json.JSONDecodeError:
return False
return isinstance(data, dict) and all(key in data for key in required_keys)
print(grade_format('{"status": "ok", "reply": "Hello"}', ["status", "reply"]))
print(grade_format("Sure! Here is the JSON: {...}", ["status", "reply"]))The first call prints True and the second prints False.
Where code checks break
Code checks are precise about what they test and blind to everything else. Three failure modes are common:
- False failures from paraphrase. If
must_includerequires "30 days" and the model writes "one month", the grader fails a correct answer. Prefer several accepted variants or a regular expression, and review failures to see whether the grader or the feature was wrong. - False passes from keyword presence. An answer can contain "damaged" and still tell the customer the wrong thing. A substring check cannot see meaning, so it can pass an answer that is fluent and wrong.
- Gaming. Once a team optimizes against a keyword list, outputs may start stuffing the keywords. The metric then stops tracking quality.
A passing code check proves only that the rule held. It does not prove the answer was good.
Human review
A human grader reads the output and judges it. Humans handle what rules cannot: tone, whether an explanation is actually helpful, whether a subtle claim is misleading, and whether a new kind of failure has appeared that no rubric anticipated.
Their cost is time and money, and their judgments vary. Two reviewers often disagree, and one reviewer may drift over a long session. You control this with process:
- Write a rubric. For each dimension, define what each score means with a short description and an example. "Helpfulness: 1 to 5" is vague. "3 = answers the question but omits a next step the customer needs" is usable.
- Prefer small scales. A pass/fail or a three-level scale gives more agreement than a ten-point scale.
- Hide the version. Show reviewers outputs without saying which system version produced them, so expectations do not color the score.
- Double-grade a sample. Have two reviewers grade the same subset and measure how often they agree. Low agreement usually means the rubric is unclear, not that the reviewers are careless. Revise the rubric and try again.
- Settle disagreements. Decide in advance how ties are resolved, for example by a third reviewer.
Because humans are slow, you will rarely have them grade everything on every run. Use them for three jobs: grading a sample, labeling a reference set to check other graders, and reviewing cases where the automated graders disagree with each other.
Model-based judges
A model judge is a language model given the question, the output, any reference material, and a rubric. It returns a verdict. It sits between code and humans: it can assess meaning at scale, but it is itself nondeterministic and can be wrong.
Judges suit dimensions like groundedness, relevance, completeness, and tone, where the check needs reading comprehension but the rubric can be written clearly.
A few practices make judges more reliable:
- Ask narrow questions. "Is every claim in the answer supported by the policy text? Answer yes or no" works better than "Rate the answer's quality."
- Give the evidence. For groundedness, include the retrieved text in the judge prompt. A judge without it can only guess.
- Ask for reasoning before the verdict. A short explanation, then the label, makes failures easier to audit and often improves the verdict.
- Fix the output format. Require a label from a fixed set so your code can parse it.
- Keep settings stable. Pin the judge model version and use low randomness, so a changed score reflects a changed feature and not a changed judge.
Judges have known biases. They can prefer longer answers, prefer the first of two options in a pairwise comparison, and rate text from their own model family more favorably. Where possible, use a different model from the one being tested, and randomize the order in comparisons.
The most important rule: a judge is a measuring instrument and must be checked before you trust it. Compare its verdicts to human labels on a set of cases, and only rely on it for dimensions where it agrees with people well enough. The later chapter on calibration covers how.
Choosing a grader for each dimension
Use the cheapest grader that can actually measure the dimension. Move up only when the cheaper one cannot see the property.
The table summarizes the trade-offs.
| Grader | Strong at | Weak at | Cost per case | Repeatability |
|---|---|---|---|---|
| Code | Format, required or forbidden content, exact facts | Meaning, paraphrase, tone | Near zero | Identical every run |
| Human | Nuance, new failure types, final authority | Speed, consistency across people | High | Varies between reviewers |
| Model judge | Meaning at scale with a clear rubric | Bias, drift, unverified trust | Low to moderate | Mostly stable, not perfect |
Layering graders
Real features need more than one grader, usually stacked per dimension. For the refund-policy feature:
- Format and safety: code checks on every case.
- No contradiction of policy amounts or deadlines: a code check for exact figures, plus a judge for contradictions that are worded differently. This is the zero-tolerance requirement, so route every judge failure to a human.
- Groundedness: a calibrated judge on all cases, with humans grading a sample each release.
- Tone: a judge with a short rubric, with human spot checks.
Run the cheap checks first. If the output is not valid JSON, there is no reason to pay for a judge call.
Common errors
- Grading the wrong thing. The grader measures what is easy (output length, keyword presence) and not what the requirement names.
- Trusting a judge on faith. No comparison against human labels means you do not know whether a 95% judged pass rate is real.
- One grader for everything. A single "quality score" hides which dimension failed. Grade each dimension separately so a regression has a visible cause.
- Hiding severity. A grader that returns only pass or fail loses the severity from the case. Keep the case's
severity_if_failedattached to its result, asrun_evaldoes, so the release gate can apply zero tolerance to critical failures. - Never reading failures. Read a sample of failed and passed cases each run. Both kinds reveal grader bugs.
Exercise
Your support assistant must (a) always return valid JSON with a "reply" field, (b) never invent a refund deadline that contradicts the policy, and (c) use a friendly tone. Choose a grader type for each requirement and say why. For (b), explain what you would add because a wrong verdict is costly.
Show answer
(a) Use a code grader. Parsing the JSON and checking for the "reply" key is a rule, so it is cheap and exact. (b) Use a code check for exact deadline values plus a calibrated model judge for contradictions worded differently, because a substring check cannot see paraphrases. Since this is a critical, zero-tolerance requirement, send every case the judge fails, and a sample it passes, to human review. (c) Use a model judge with a short rubric, such as "friendly, polite, no blame", and spot-check its verdicts against a small set of human labels, because tone needs meaning but a wrong verdict is low cost.