Sub topic 7
Using an LLM as a judge
This chapter assumes you can call an LLM through an SDK, write basic Python, and have a working harness like the one in the previous chapters. Some qualities have no exact-match answer. Examples are faithfulness to a source, helpfulness, and tone. An LLM judge is a second model call that grades the first model's output against a stated criterion.
When a judge is the right tool
Use deterministic checks and reference metrics first. They are cheap and repeatable. Add a judge when the property you care about is semantic and cannot be written as a rule.
| Approach | Strength | Weakness |
|---|---|---|
| Exact match or schema check | Fast, exact, free | Misses valid paraphrases and meaning |
| LLM judge | Handles meaning and open-ended text | Costs money, can be biased, needs validation |
| Human review | Highest trust | Slow and expensive |
Write one criterion per judge
A judge works best when it checks a single, explicit criterion. "Is this answer good?" is vague. "Is every claim in the answer supported by the context?" is checkable. If you care about three qualities, run three judges and report three scores.
Ask for a strict output format so you can parse it. Put the verdict on the first line and the reason on the second.
JUDGE_PROMPT = """You are grading an answer to a question.
Criterion: the answer must be fully supported by the provided context and must not add claims that the context does not contain.
Context:
{context}
Question:
{question}
Answer:
{answer}
On the first line, write only PASS or FAIL. On the second line, give a one-sentence reason."""
def build_judge_prompt(context: str, question: str, answer: str) -> str:
return JUDGE_PROMPT.format(context=context, question=question, answer=answer)
def parse_verdict(judge_output: str):
lines = judge_output.strip().splitlines()
if not lines:
return None
first = lines[0].strip().upper()
if first in ("PASS", "FAIL"):
return first == "PASS"
return None # unparseable: count and review these separatelyparse_verdict returns None for malformed replies. Do not treat None as a failure. Count these cases separately, because a rising count usually means the prompt or the model changed.
Plug the judge into the harness
The harness from the earlier chapter takes a scorer(output, expected). A judge needs more inputs, so wrap it in a function that closes over the extra fields. In the sketch below, call_llm stands for your own function that sends a prompt to a model and returns text.
The flow is simple: the application produces an answer, the judge prompt wraps it with the criterion, and the parser turns the reply into a boolean.
Practical rules
- Use a clear rubric with defined PASS and FAIL conditions. Include a short example of each if the criterion is subtle.
- Set temperature to 0 where the API allows it. This reduces, but does not remove, run-to-run variation.
- Prefer binary or small-scale verdicts. Fine scales such as 1 to 10 are noisy and hard to compare.
- Ask for the reason after the verdict, and log it. Reasons make failures easy to inspect.
- Use a judge model at least as capable as the model being graded, and avoid grading a model with itself when you can.
Common errors
- Vague criteria. The judge invents its own standard, and scores shift between runs.
- Trusting the judge blindly. A judge is a metric that must itself be checked. The next chapters cover how to compare it with human labels.
- Ignoring parse failures. Silent
Nonevalues can hide a broken prompt. - Leaking the expected answer or system details into the judge prompt in ways that change its behavior.
Exercise
Why does parse_verdict return None for an unexpected reply instead of False?
Show answer
Returning False would count a malformed judge reply as an answer failure, which mixes judge errors with application errors. Returning None lets you count unparseable replies separately and review them, so you can fix the judge prompt or model instead of misreading the pass rate.
Exercise
You want to grade an answer for both faithfulness to the context and politeness. Should you use one judge prompt or two, and why?
Show answer
Use two. Each judge checks a single criterion, so its verdicts are easier to interpret, validate against human labels, and debug. Report two separate scores.