Sub topic 6
Judge-based checks and validating the judge
This chapter assumes you have a golden dataset, deterministic checks like the ones in earlier chapters, and basic pytest and Git skills. Deterministic checks cover format, required keys, and phrases. They cannot tell you whether a summary is faithful or a reply is polite. For those qualities you can use a second model call as a grader. This is called an LLM judge.
When to use a judge
Use a judge only for qualities you cannot check with code. Keep the deterministic checks as the first gate, because they are free, fast, and repeatable. Run the judge on outputs that already passed them.
Good judge targets:
- Faithfulness: does the answer stay within the supplied context?
- Tone: is the reply professional and free of blame?
- Completeness: does the answer address every part of the question?
Write a narrow rubric
A vague question such as "Is this a good answer?" gives noisy scores. Ask one question per criterion, define the pass condition, and require a short reason before the verdict. Binary pass/fail verdicts are easier to validate than 1–10 scores.
import json
JUDGE_PROMPT = """You are grading an assistant's answer.
Criterion: the answer uses only facts found in the CONTEXT.
Fail if it states any fact that is not in the CONTEXT.
CONTEXT:
{context}
ANSWER:
{answer}
Reply with JSON only: {{"reason": "<one sentence>", "verdict": "pass" or "fail"}}"""
def judge_faithfulness(call_model, context, answer):
prompt = JUDGE_PROMPT.format(context=context, answer=answer)
raw = call_model(prompt, temperature=0)
data = json.loads(raw)
if data["verdict"] not in {"pass", "fail"}:
raise ValueError(f"Unexpected verdict: {data['verdict']!r}")
return data["verdict"] == "pass"Here call_model is your own wrapper around the API. The judge runs at temperature 0 and returns JSON, so you can parse it the same way as any other output.
Validate the judge against human labels
A judge is itself a prompt and a model, so it can be wrong. Before you trust it in CI, measure it.
- Pick 30 to 50 outputs from your dataset, including clear passes, clear failures, and borderline cases.
- Have a person label each one pass or fail using the same rubric.
- Run the judge on the same outputs.
- Compare the two sets of labels.
def judge_agreement(human_labels, judge_labels):
pairs = list(zip(human_labels, judge_labels))
agree = sum(1 for h, j in pairs if h == j)
false_pass = sum(1 for h, j in pairs if not h and j)
false_fail = sum(1 for h, j in pairs if h and not j)
return {
"agreement": agree / len(pairs),
"false_pass": false_pass,
"false_fail": false_fail,
}
human = [True, True, False, False, True, False, True, True]
judge = [True, True, False, True, True, False, True, False]
print(judge_agreement(human, judge))This prints {'agreement': 0.75, 'false_pass': 1, 'false_fail': 1}. Agreement alone hides the direction of the errors. A false pass lets a real regression through. A false fail wastes reviewer time and teaches people to ignore the suite. For a regression gate, false passes usually matter more, so look at them first.
If agreement is low, read the disagreements. They usually show a rubric that is ambiguous or a missing example. Revise the rubric and re-run. Do not tune the judge until it agrees on the same samples you will use as your only proof. Keep a few labeled cases aside as a holdout.
Common errors
| Problem | Effect | Mitigation |
|---|---|---|
| Judge is the same model as the system under test | Shared blind spots, self-preference | Use a different model or family when you can |
| Scores on a 1–10 scale | Noisy, hard to threshold | Use binary verdicts per criterion |
| Judge sees the expected answer and the output together without a rubric | Judge guesses what matters | State the criterion and pass condition |
| Judge model changes silently | Scores shift with no code change | Pin the judge model version |
| Verdict parsing is loose | Malformed replies counted as passes | Fail the case when the JSON is invalid |
Using the judge in CI
Treat the judge as part of the test infrastructure. Pin its model version and keep its prompt in version control. Re-run the human-label comparison whenever you change the judge prompt or model. Because a judge call costs money and time, run it on the scheduled suite or on a sampled subset for pull requests, and keep the deterministic checks as the fast gate.
Exercise
Your judge scores 92% agreement with human labels, but all of its errors are false passes. Why is that a problem for a regression gate, and what do you do next?
Show answer
False passes let real regressions through, so the gate misses the failures it exists to catch. Read the false-pass cases, tighten the rubric or add examples to the judge prompt, and re-measure on a holdout set of labeled cases. Aim for fewer false passes, even if that adds a few false fails.