Code graders vs LLM-as-judge
Once you know what failure modes you care about, you need graders that detect them automatically. There are two broad families: code-based graders that check something deterministic (does the JSON parse, did it call the right tool, does the answer contain the order number), and LLM-as-judge graders that ask a model to evaluate qualities code cannot easily check (is this faithful to the source, is the tone right). Most real suites use both, and the hard part is knowing which to reach for and how to make the judge trustworthy.
Why it matters
Graders are the measuring instrument. If the instrument is wrong, every decision you make downstream is wrong too, and you will not notice because the numbers look precise. Code graders are cheap, fast and repeatable, but brittle on open-ended text. LLM judges handle nuance but cost money, add latency, are non-deterministic and carry known biases. The research on MT-Bench and Chatbot Arena (Zheng et al.) found strong judges can agree with human preferences at levels comparable to human-human agreement, but also documented position bias, verbosity bias and self-enhancement bias. So judges are useful, and they need checking.
How it works
My rule: use code whenever the property can be checked mechanically, and only fall back to a judge for what is left.
Rendering diagram…
Code graders include exact or fuzzy string match, JSON schema validation, SQL that executes and returns the right rows, unit tests on generated code, checks that a required citation ID appears, length limits, and tool-call name and argument checks. Braintrust's open-source autoevals library ships several of these (Levenshtein, JSON validity, numeric difference) alongside model-graded ones.
LLM judges take the input, the output, sometimes a reference, and a carefully written instruction, and return a label plus reasoning. Good judge prompts have a few properties:
- They ask a narrow question about one failure mode, not "rate this 1 to 10".
- They prefer binary pass or fail over Likert scales. Binary labels are easier to calibrate and harder to fudge.
- They ask for the reasoning before the verdict, so the label is conditioned on the explanation.
- They include a handful of labelled examples, ideally borderline ones.
Calibration means measuring how well the judge agrees with a human on a labelled set, then iterating on the judge prompt until agreement is acceptable. Treat this like training a classifier: split your human-labelled data into a development set (to tune the prompt) and a test set (to report final agreement). Report true positive rate and true negative rate separately, not just accuracy, because class imbalance makes accuracy misleading. Eugene Yan's survey of LLM evaluators recommends Cohen's kappa for categorical labels since it corrects for chance agreement.
Reference architecture
Rendering diagram…
Once the judge is calibrated, freeze its prompt and model version. Changing either means re-running calibration.
Code
A judge with a binary verdict and a calibration function that reports TPR, TNR and Cohen's kappa. The model client is left abstract so you can plug in any provider.
import json
from dataclasses import dataclass
JUDGE_PROMPT = """You are checking one thing: does the ANSWER state any fact
that is not supported by the CONTEXT?
CONTEXT:
{context}
ANSWER:
{answer}
Think step by step, then reply with JSON only:
{{"reasoning": "<short explanation>", "verdict": "pass" | "fail"}}
"pass" means every factual claim is supported."""
def call_judge(prompt: str) -> str:
raise NotImplementedError # your LLM client, temperature 0
def judge(context: str, answer: str) -> bool:
raw = call_judge(JUDGE_PROMPT.format(context=context, answer=answer))
return json.loads(raw)["verdict"] == "pass"
@dataclass
class Labelled:
context: str
answer: str
human_pass: bool
def calibrate(rows: list[Labelled]) -> dict[str, float]:
tp = tn = fp = fn = 0
for r in rows:
pred = judge(r.context, r.answer)
if pred and r.human_pass: tp += 1
elif not pred and not r.human_pass: tn += 1
elif pred and not r.human_pass: fp += 1
else: fn += 1
n = tp + tn + fp + fn
observed = (tp + tn) / n
p_yes = ((tp + fp) / n) * ((tp + fn) / n)
p_no = ((tn + fn) / n) * ((tn + fp) / n)
expected = p_yes + p_no
kappa = (observed - expected) / (1 - expected) if expected < 1 else 1.0
return {
"tpr": tp / (tp + fn) if tp + fn else 0.0,
"tnr": tn / (tn + fp) if tn + fp else 0.0,
"kappa": kappa,
}
Once you know TPR and TNR on a held-out test set, you can also correct the judge's raw pass rate on unlabelled production data, which is more honest than reporting it as is.
Trade-offs and pitfalls
- Likert scales. A 1 to 5 score invites the judge (and humans) to cluster around 3 and 4. The difference between a 3 and a 4 is rarely actionable. Binary per-criterion verdicts are easier to act on.
- Position and verbosity bias. Judges favour longer answers and, in pairwise setups, whichever answer appears in a given slot. Randomise order and control for length where you can.
- Self-preference. A model judging its own outputs tends to be generous. Use a different model family as the judge when possible.
- Uncalibrated judges gating CI. A judge with 70 percent agreement can easily flip a pass to a fail on noise. Know the error rate before using it as a gate.
- Criteria drift. Shreya Shankar's "Who Validates the Validators?" paper observes that people refine their criteria as they grade outputs. Expect your rubric to change during calibration and that is fine.
- Cost. Every judged example is an extra model call. Use code graders as a cheap first filter and sample for judging on large sets.
- Brittle code graders. Exact match on free text fails on harmless rephrasing. Normalise (case, whitespace, numbers) or switch to a judge.
Checklist
- Every property that can be checked with code is checked with code.
- Each judge answers one narrow question with a binary verdict and reasoning.
- You have 100 or more human-labelled examples split into dev and test sets.
- TPR, TNR and kappa are reported for each judge on the test split.
- Judge prompt and model version are pinned and recorded with each run.
- Pairwise and ordered inputs are randomised to reduce position bias.