LLM-as-Judge Evaluation

Use an LLM to grade answers reliably, and avoid the common judge biases.

Last Updated:

Module 16 · Lesson 4 of 8 · Intermediate

LLM-as-Judge Evaluation

Use an LLM to grade answers reliably, and avoid the common judge biases.

Human review is the gold standard for judging answers, but it is slow and expensive. LLM-as-judge uses a language model to grade outputs against a rubric, so you can evaluate hundreds of answers in minutes after every change. Done carefully, LLM judges agree with human reviewers about as often as reviewers agree with each other. Done carelessly, they produce confident, meaningless numbers. This lesson shows how to do it carefully.

Two styles of judging

  • Pointwise: grade one answer on its own against a rubric (optionally with a reference answer). Good for tracking quality over time.
  • Pairwise: show two answers to the same question and ask which is better. More sensitive to small differences, ideal for comparing two pipeline versions.

A pointwise correctness judge

judge.py
from typing import Literal
from pydantic import BaseModel
import anthropic

client = anthropic.Anthropic()

class Grade(BaseModel):
    reasoning: str                                   # written BEFORE the verdict
    verdict: Literal["correct", "partially_correct", "incorrect", "correctly_declined"]

RUBRIC = """You are grading an answer from a company policy assistant against a reference answer.

- correct: contains the key facts of the reference and nothing that contradicts it. Different
  wording, extra helpful detail and different formatting are fine.
- partially_correct: some key facts are right but an important one is missing or vague.
- incorrect: contradicts the reference, or states wrong facts.
- correctly_declined: the reference says the question cannot be answered, and the answer
  declines or says the information is unavailable.

Judge only factual content, not style or length."""

def grade(question: str, answer: str, reference: str) -> Grade:
    response = client.messages.parse(
        model="claude-opus-5-5",
        max_tokens=1500,
        output_config={"effort": "medium"},
        system=RUBRIC,
        messages=[{"role": "user", "content":
            f"<question>{question}</question>\n<reference>{reference}</reference>\n"
            f"<answer>{answer}</answer>"}],
        output_format=Grade,
    )
    return response.parsed_output

Three design choices make this judge reliable:

  • Categorical labels with definitions instead of a 1–10 scale. Fine-grained numeric scales are inconsistent; a few clearly defined categories are not.
  • Reasoning before the verdict. The schema puts reasoning first, so the verdict follows from an explanation, and you can read why a case failed.
  • A reference answer. Judging correctness without a reference asks the judge to know the right answer itself, which for private documents it cannot.

A pairwise judge with position swapping

LLM judges have a position bias: they tend to prefer whichever answer is shown first (or second). Run every comparison twice with the order swapped, and count a win only when both runs agree.

class Preference(BaseModel):
    reasoning: str
    better: Literal["A", "B", "tie"]

def compare_once(question: str, a: str, b: str, reference: str) -> str:
    response = client.messages.parse(
        model="claude-opus-5-5", max_tokens=1500, output_config={"effort": "medium"},
        messages=[{"role": "user", "content":
            f"Question: {question}\nReference answer: {reference}\n\nAnswer A:\n{a}\n\nAnswer B:\n{b}\n\n"
            "Which answer is more correct and more useful to the user? Ignore length and style "
            "unless they affect usefulness."}],
        output_format=Preference)
    return response.parsed_output.better

def pairwise(question: str, old: str, new: str, reference: str) -> str:
    first = compare_once(question, old, new, reference)      # old = A, new = B
    second = compare_once(question, new, old, reference)     # new = A, old = B
    if first == "B" and second == "A":
        return "new wins"
    if first == "A" and second == "B":
        return "old wins"
    return "tie"

Known biases and mitigations

BiasWhat happensMitigation
Position biasPrefers the first (or second) answer shownSwap order and require agreement
Verbosity biasPrefers longer answersRubric says length is not a criterion; use references
Self-preferencePrefers text written in its own styleValidate against human labels; consider a different judge model
LeniencyMarks vague answers as correctPrecise category definitions; examples of borderline cases
InconsistencyDifferent verdicts on rerunsCategorical labels, reasoning first, aggregate over many cases

Validate the judge against humans

Before trusting a judge, have people label 30 to 100 cases and compare. Measure the agreement rate, and look at the disagreements: they usually reveal an unclear rubric rather than a bad model.

def agreement(human: list[str], model: list[str]) -> float:
    return sum(h == m for h, m in zip(human, model)) / len(human)

# Cohen's kappa corrects for chance agreement (scikit-learn):
# from sklearn.metrics import cohen_kappa_score
# print(cohen_kappa_score(human, model))

Iterate on the rubric until agreement is close to the agreement between two human reviewers on the same cases. Re-check whenever you change the judge prompt or model.

Cost control

Judging can cost more than generation, because judge prompts include context and references. Judge a fixed, representative test set rather than all traffic, cache the judge's system prompt, and use lower effort for simple checks.

Key takeaways

  • LLM judges make large-scale evaluation practical; use pointwise grading for tracking and pairwise for comparisons.
  • Use categorical labels with clear definitions, reasoning before verdicts, and reference answers.
  • Counter position bias by swapping order, and watch for verbosity and self-preference bias.
  • Validate every judge against human labels before trusting its numbers.