Skip to content

Learn evals and LLM-as-judge

A short guide to read alongside RnR phases 2, 4 and 6. The README has setup and the labs; learn-guardrails.md comes before this one. RAG itself is taught in ../rag/learn-rag.md.

1. What evals are, and why they exist

An eval is a repeatable test that scores an AI feature against answers you already know, and prices its mistakes. It turns "the outputs look fine" into numbers you can compare across prompt versions, models and settings. When an output has no single right answer (a summary, a RAG answer), a second model call grades it against a rubric: an LLM judge.

The problem they solve: a model's quality can't be seen by reading a few outputs. A prompt fix that helps the 8 reviews you looked at can break 20 you didn't. Without a score, every change is a guess and every debate is opinion.

Without evals With evals
"v2 looks better" after reading 8 reviews Honest reviews rejected 40% → 0%, on the same 50, saved and comparable
99% F1, so extraction is done Six hand-written reviews find two mistakes the 99% missed
"The summary reads well" Every number and citation checked in code; the judge scores only the meaning, after calibration

Analogy: a factory's quality lab. You don't judge a batch of TMT bars by looking at them; you bend-test a fixed sample and stop the line when a batch fails. Evals are the bend test for a prompt. Essay-style outputs need an examiner with a marking scheme (the judge), and a second examiner re-marks a sample first (calibration).

When you need them: before the first prompt goes live, and on every change after: prompt, model, temperature, thinking on or off, even an Ollama upgrade.

2. The mental model

Two eval loops: offline before you ship, online after

Four ideas carry the three phases.

  • Known answers first. The golden labels were decided before the text existed (lab 00), so the score is right by construction. No labels, no eval.
  • Price the mistakes. An honest review rejected, abuse published and a review held cost different amounts (evals/costs.py). Pick the policy with the lowest total, not the highest accuracy.
  • Facts from code, words from the model. The model extracts a label and a quote, or writes sentences from counts code already has. Code checks every quote, number and id; the model never calculates.
  • Check in code first, judge what's left, calibrate the judge. A judge is a model too: measure it on claims whose answer you know (kappa), and against your own labels, before you trust its scores.

Analogy: a newsroom. The data desk produces the numbers (code), the reporter writes the story (model), the fact-checker checks every figure (checks), and the editor reads for meaning (judge). Nobody lets the reporter make up the figures.

Facts from code, words from the model

3. The labs in three stages

Stage 1: score a classifier (05)

Script What you see Key takeaway
05_golden_set_eval.py mod-v1 rejected 40% of honest reviews; mod-v2 0%, at 50% more prompt tokens. --hard: 20 hand-written cases where the model calls blunt complaints abusive Score before and after every change, on the same reviews. Report the model's own score next to the product's: a safety net hides mistakes

Then open Ops: offline eval runs per prompt version, and the two online signals (override rate, what humans do with held reviews).

Stage 2: extract, then calculate (07–08)

Script What you see Key takeaway
07_aspect_extraction.py Six reviews: aspect, pos/neg and a quote per mention; code drops invented quotes. "koi lumps nahi" still comes out negative The LLM extracts, code checks the evidence. A small hand-written set finds what a big score hides
08_scoring_math.py Plain stars vs a Bayesian score; plain % vs the Wilson lower bound; the planted TMT defect ranked first (no model) Never ask the model for a score. Correct small samples in code

Stage 3: grade generated text (12–14)

Script What you see Key takeaway
12_sku_summary.py grounded vs stuff vs map-reduce on 4 SKUs: calls, tokens, points dropped by the checks, top complaint named or not Grounded's numbers pass because they were copied; the others count and fail. Checks catch false points, not missing ones: coverage is its own number
13_judge_calibration.py judge-v1 vs judge-v2 on 40 claims with a known answer (kappa, per error type); --label 20 and --human score the judge against you Kappa, not accuracy. judge-v1 scored 0.95 on planted errors and 0.04 on real labels: planted errors flatter a judge
14_ask_the_reviews.py Counting questions answered by code, open ones by cited RAG over one SKU, unanswerable and injection questions refused Route counting to code. Filter by SKU in the index, not the prompt. Two refusal gates: distance before the model, citation checks after

Calibrate the judge before you trust its scores

4. Decisions your team will face

Decision Start with Change when
Golden set size 50 stratified (3+ per scenario) for every change; all 500 before switching the product Differences you care about are under 5 points: grow the set
Where labels come from Generated from specs (synthetic) Real traffic exists: add every human override, then sampled reviews labelled by two people
Headline number Honest rejected, bad published, held, side by side The business agrees on costs: lead with ₹ per 1,000
Hold policy Hold below 0.7 confidence Confidence is one constant value (it is here): hold every risky call instead, or measure agreement across samples
Hard cases A small hand-written set of past failures (evals/hard_cases.jsonl) Every real failure or override adds one; the set only grows
What to check in code Every number, id and quote the model writes Free text with nothing checkable: lean on the judge, and sample for humans
Judge The same local model, one claim at a time, calibrated Kappa under ~0.6, or it favours its own writing: a different or bigger judge model
When to rerun Every prompt or model change Also after Ollama or model-file upgrades, and weekly on fresh production samples

5. What the labs don't cover

  • Confidence intervals. 3 abusive reviews in 50 give a recall of 0, 33, 67 or 100%. A bootstrap shows how much a difference could be luck.
  • Fairness slices and consistency. Rates per language and tone, counterfactual pairs, agreement across samples: see FAQ-more.md for what we tried and found.
  • A regression gate in CI. Here you run lab 05 by hand; in production a prompt change fails the build when a key number gets worse.
  • Pairwise judging and judge panels. "Which of two summaries is better?" is often more reliable than grading one alone.
  • Drift monitoring. Alerts when the override rate or the share of held reviews moves, not just a table.

Check yourself

Answer each in a sentence or two, without looking. If one is hard, reread the section in brackets.

  1. Why must the golden labels exist before the review text is written? (section 2)
  2. Two prompt versions: one has higher accuracy, the other rejects fewer honest reviews but holds more. How would you decide which to ship? (section 2, lab 05)
  3. Lab 05 reports the model's own score next to the product's score. Why, when the product's score is what buyers see? (section 3, lab 05)
  4. A judge scored well on planted errors and badly on real labels. Why can planted errors flatter a judge, and what does that mean for how you test one? (section 3, lab 13)
  5. A teammate changes the moderation prompt, reads ten outputs, says they look better and wants to ship today. What would you ask for first, and why? (section 1, lab 05)

Same pattern, other industries

Industry Use case What changes
B2C e-commerce Review highlights and product Q&A Millions of reviews: grounded summaries are the only option that scales; labels from clicks and purchases
Banking Dispute triage, credit memo summaries The cost matrix is in money and penalties; every figure in a memo comes from code and a human signs
Healthcare Patient message triage, discharge summaries A missed urgent message costs far more than a hold; lab values never come from the model
Insurance Claim document extraction Field-level precision per document type; the same document re-uploaded must give the same fields
Legal Contract Q&A Citations point to clauses; long documents need chunking and map-reduce, with a judge on the result

Going deeper: advanced eval topics

Topic What it solves Search for
Eval frameworks Running, storing and comparing eval suites "promptfoo", "DeepEval", "Inspect AI"
RAG eval frameworks Faithfulness, context precision and recall metrics "ragas", "trulens"
Statistical significance Telling a real improvement from noise "bootstrap confidence interval", "McNemar test"
Judge biases Position, length and self-preference bias "llm-as-a-judge bias", "pairwise llm judge"
Claim extraction Splitting free text into checkable claims "factscore", "atomic facts"
Annotation quality Trusting human labels "inter-annotator agreement", "Cohen's kappa"
Online evaluation Quality on live traffic "human override rate", "shadow deployment"

Try it

Plant one more error type. In evals/judge_eval.py _sku_claims(), add a claim that is true in numbers but about the wrong aspect ("17 of 24 buyers are unhappy with the packaging", using bendability's counts). Run python 13_judge_calibration.py: does judge-v2 catch it? If not, change the rubric (not the claims), re-run, and check your hand labels with --human before you keep the change.

Next: learn-operate.md: trace every step and price every call.