LLM-as-a-judge
Use a strong LLM with a narrow rubric to grade outputs at scale, but treat it as a classifier: calibrate it against human labels, report agreement, and counter position, verbosity and self-preference bias.
Analogy: Like hiring a teaching assistant to grade 10,000 essays: you grade 100 yourself first, compare, and keep spot-checking, because they drift and tend to reward long essays.
How it works
- Three modes: pointwise for CI and monitoring, pairwise to compare prompts or models, reference-based against a gold answer.
- Judges favor one answer slot (position bias), longer answers and outputs from their own model family.
- Use binary, specific criteria: one judge per failure mode, not a 1–10 "quality" score.
- In pairwise, swap the order and count flips as ties; ask for reasoning before the verdict.
- Hand-label ~100–200 examples and aim for judge TPR and TNR of ~0.9 or better.
Check yourself
Answer to mark it learned; your answers aren't scored.
Comparing prompts A and B with a pairwise judge, you always show A first, and A wins 70%. You re-run with B shown first, and B wins 62%. What's going on, and what do you do?
Go deeper
Full explanation
An LLM-as-a-judge is a second model call that grades an output against criteria you write. It is the only practical way to score open-ended text (tone, helpfulness, policy compliance) over thousands of examples per run. Wherever code can check the answer, use Code-based graders & assertions first.
Three modes:
- Pointwise — one output, one rubric → PASS/FAIL or a score. Best for CI and monitoring.
- Pairwise — is A or B better? More stable than absolute scores; ideal for comparing prompts or models.
- Reference-based — compare against a gold answer; makes open grading closer to objective.
Known biases (Zheng et al., 2023):
- Position — favors the first (or second) answer shown.
- Verbosity — prefers longer answers even when they are not better.
- Self-preference — rates outputs from its own model family higher.
Mitigations that work: binary, specific criteria (one judge per failure mode, not a 1–10 "quality" score), swap the order in pairwise and count flips as ties, reasoning before the verdict, a strong judge from a different family, and few-shot examples of hard cases.
The judge is itself a classifier. Hand-label 100–200 examples, measure its agreement (TPR/TNR or Cohen's kappa) and report it next to every score: an uncalibrated judge is a vibe with a number attached.
Code Binary judge with structured JSON, plus swapped pairwise
from typing import Literal
from openai import OpenAI
from pydantic import BaseModel
client = OpenAI()
JUDGE = "gpt-4.1" # any strong chat model, ideally another family than the generator
class Verdict(BaseModel):
reasoning: str # generated first, so it conditions the verdict
verdict: Literal["PASS", "FAIL"] # binary: far easier to calibrate than 1-10
RUBRIC = """Grade ONE criterion of a support reply.
PASS: every policy, price or deadline it states appears in <policy>.
FAIL: it promises anything the policy does not allow.
Quote the policy line you relied on, then decide. Return JSON:
{"reasoning": "...", "verdict": "PASS" or "FAIL"}"""
def judge(policy: str, question: str, reply: str) -> Verdict:
resp = client.chat.completions.create(
model=JUDGE, temperature=0,
response_format={"type": "json_object"},
messages=[{"role": "system", "content": RUBRIC},
{"role": "user", "content":
f"<policy>{policy}</policy>\n<question>{question}</question>\n<reply>{reply}</reply>"}],
)
return Verdict.model_validate_json(resp.choices[0].message.content)
def pairwise(q: str, a: str, b: str) -> str:
# Ask twice with the order swapped; a flip is position bias -> call it a tie
first, second = compare(q, a, b), compare(q, b, a) # each returns "1" or "2"
if (first, second) == ("1", "2"): return "A"
if (first, second) == ("2", "1"): return "B"
return "tie"Real example
A fintech team wants to catch replies that promise refunds outside policy. They hand-label 200 traces (31 FAIL), write a binary judge for that one criterion, and measure: v1 reaches TPR 0.71 and TNR 0.97 — it misses vague promises like "we'll sort it out". They add two such cases as few-shot examples and require quoting the policy line before the verdict: TPR 0.90, TNR 0.95. It now grades 2% of production traffic daily, and they re-check 50 fresh human labels every month.
Trade-offs +3 · −4
+ You gain
- Grades open-ended qualities (tone, policy, helpfulness) that code can't check.
- Orders of magnitude cheaper and faster than expert review: thousands of grades per run.
- The same rubric runs offline in CI and online on sampled production traffic.
− You pay
- Biased (position, verbosity, self-preference) and itself nondeterministic.
- Needs human labels to calibrate, and recalibration whenever the judge model or prompt changes.
- Weak where the judge itself is weak: hard math, niche domain facts, very long contexts.
- One extra LLM call per graded output: real cost and latency if run on every request.
Use it when
Criteria that need language understanding (faithfulness, tone, policy, helpfulness), pairwise prompt or model comparisons, and production monitoring — once you have human labels to calibrate against.
Avoid it when
When code can decide (exact match, JSON schema, unit tests, SQL results), or for high-stakes domain correctness the judge can't verify itself — that needs experts.
Numbers to know 3
- Zheng et al. (2023): on MT-Bench, GPT-4 as judge agreed with human experts on >80% of non-tie votes, about the level humans reach with each other.
- Same paper: with the answer order swapped, even GPT-4 kept its verdict only ~65% of the time.
- Rule of thumb: calibrate on ~100–200 human-labeled examples; aim for judge TPR and TNR of ~0.9 or better.
Common pitfalls 4
- A 1–10 "overall quality" score: uncalibrated, drifts, and nobody knows what to fix at 6.8.
- Reporting judge scores without the judge's agreement with humans, or measuring it with raw accuracy on rare failures.
- Using the same model as generator and judge: self-preference inflates the score.
- Swapping the judge model or prompt mid-project and comparing scores across the change.
Interview questions 2
Answer out loud first, then reveal.
How do you know your LLM judge can be trusted?Show answer
I treat it like any classifier. A domain expert labels 100–200 real traces pass/fail with a short critique, and I split them into few-shot, dev and test sets so I don't tune on the test. I measure TPR and TNR on the test split, not raw accuracy, because failures are usually rare. I iterate on the rubric until both are around 0.9, report that agreement next to every score, and re-check a fresh labeled sample whenever the judge model, its prompt or the product changes.
Pairwise or pointwise judging?Show answer
It depends on the decision. To choose between two prompts or models I use pairwise, because relative judgments are more stable than absolute scores, and I run every pair twice with the order swapped, counting flips as ties to cancel position bias. For CI and production monitoring I use pointwise binary checks, one per failure mode, because I need a rate I can track and alert on. In both cases I avoid 1–10 scales.