Evaluating LLM output: golden sets, LLM-as-judge and prompt regressions
Unit tests can't assert on free text: build a golden set, layer hard checks, reference comparison and LLM-as-judge, calibrate the judge against human labels, and stop a new prompt from degrading answers in CI.
On this page
1. The problem
A team tweaks the support bot's prompt to be "friendlier". All 312 unit tests stay green because the model is mocked. A week later, 18% of refund answers say "30 days" while the policy is 14.
2. Key idea
Code around the LLM can be tested like any code. The behavior of prompt plus model can only be measured statistically over a set of cases: free text means scoring by criteria, non-determinism means measuring rates, wide-reaching changes mean comparing each slice with the old version. The merge gate compares with the baseline (what's running), not with 100%.
| Check | Use when | Don't use when |
|---|---|---|
| Hard checks (schema, regex, has a citation, length) | structured output, clear rules | judging tone or correctness of meaning |
| Reference comparison | there's a single right answer: "14 days", "SKU-889" | open questions |
| Embedding similarity | quickly flagging far-off cases | deciding pass/fail: one wrong number still looks "similar" |
| LLM-as-judge with a rubric | correct meaning, grounded in sources, right tone | not yet calibrated against humans |
| Human grading | calibrating the judge, before big releases | every PR |
3. In Java
@Tag("llm-eval") // runs on PRs that change prompts/models, and nightly
@SpringBootTest
class RefundAnswerEvalTest {
@Autowired ChatClient support; // prompt and model under test
@Autowired @Qualifier("judge") ChatClient.Builder judgeModel; // a different model family
@Test
void refundAnswersStayAboveBaseline() {
var judge = FactCheckingEvaluator.builder(judgeModel).build();
List<GoldenCase> cases = Golden.load("golden/refund.jsonl"); // 200 cases
List<String> failed = new ArrayList<>();
for (GoldenCase c : cases) {
String answer = support.prompt().user(c.question()).call().content();
boolean ok = answer.contains(c.mustCite()) // hard check first
&& judge.evaluate(new EvaluationRequest(c.policyText(), List.of(), answer)).isPass();
if (!ok) failed.add(c.id());
}
double passRate = 1 - (double) failed.size() / cases.size();
assertThat(passRate).as("failed: %s", failed).isGreaterThanOrEqualTo(0.95); // baseline 0.96
}
}Other languages: Python has promptfoo, DeepEval, Ragas; Node.js uses promptfoo.
How judges are biased, and noise with 200 cases
- Common biases: preferring longer answers, preferring their own model's style, order effects when comparing two answers, leniency with vague rubrics. Fixes: yes/no per criterion, a judge from another model family, swapping order in pairwise comparisons.
- Calibration: humans label 50–100 cases; measure how often the judge agrees. Low agreement means fixing the rubric before trusting the judge.
- Noise: with 200 cases, each case is 0.5 points, and rerunning the same prompt moves a few points. Make the threshold "no slice drops more than 3 points", and rerun when results sit near it.
- Hidden set: keep some cases out of prompt tuning so you don't "memorize" the golden set.
- Pin dated model versions; aliases like
-latestchange the model without anyone deploying.
4. Working with AI
Hand to AI: generating question variants from real cases, writing a JSONL harness, drafting rubrics, summarizing failed cases by error group.
Check yourself in AI-written code:
- Expected answers are human-reviewed, not copied from current output.
- The judge gets the question, the policy or context, and the answer; the answer never goes in the context slot.
- The judge is from another model family and its agreement with human labels was measured.
- Assertions use the pass rate versus baseline, with per-slice results and the list of failed ids.
- Tests calling a real model have their own tag and don't run on every local build.
5. Exercises
Exercise 1: Review an evaluation test
The AI says "checks answers are factually grounded with FactCheckingEvaluator". The test is 100% green even when you deliberately break the prompt. Why?
String answer = support.prompt().user(c.question()).call().content();
var verdict = FactCheckingEvaluator.builder(ChatClient.builder(chatModel)).build()
.evaluate(new EvaluationRequest(answer, List.of(), answer));
assertThat(verdict.isPass()).isTrue();Solution 1
FactCheckingEvaluator checks whether the answer is supported by the document (userText). The test passes the answer itself in both places, so every answer supports itself. A secondary bug: the judge uses the same chatModel that generated the answer.
Fix: new EvaluationRequest(c.policyText(), List.of(), answer), with a judge from another model family. Prove it: add a control case with a fixed wrong answer ("30 days") and assert isPass() == false. An evaluation harness needs a red test first too.
Exercise 2: Read a regression
Nobody changed the prompt or code, but the nightly run drops from 94% to 90%; the "no diacritics" slice falls from 91% to 64%, other slices unchanged. Traces show gen_ai.response.model changed from ...-2026-05-01 to ...-2026-09-12 two days ago. Diagnose it, say what to do in 15 minutes, fix the root cause.
Solution 2
- The config calls the model through an alias; the provider moved the alias to a new version that's worse at Vietnamese without diacritics. It's a "deploy" you didn't make.
- 15 minutes: pin the model to the dated version (
...-2026-05-01). - Root fix: always pin versions; upgrading the model is a PR that runs the full golden set; alert when
gen_ai.response.modeldiffers from the expected value.
Summary
- Unit tests with a mocked model can't measure output quality; a golden set can.
- The golden set has slices, human-reviewed answers and a hidden part.
- Hard checks first, LLM-as-judge next, human grading to calibrate the judge.
- The merge gate compares with the baseline per slice, not with 100%.
- Pin model versions; changing the model is a code change.
Further reading: Observability for LLM calls.
Practice interview questions on this topic
- LLM output is free text, so you can't `assertEquals`. How do you test a feature that uses an LLM?
- How far do you trust LLM-as-judge, and how do you know your judge is reliable?
- Nobody changed a prompt or code, yet last night's evaluation fell from 94% to 90%, with the no-diacritics slice dropping from 91% to 64%. How do you diagnose it and design the CI gate?
Comments
Community rules