AI from scratch · Evaluating LLMs · Stage 6: LLM engineering

Evaluating LLMs

Every lesson in this stage ended with "measure it". This one is how. Grading a classifier is easy: compare a label with a label (chapter 12). Grading a language model is hard: the right answer can be phrased a thousand ways, partly right, right but padded with a wrong claim, or a polite refusal. So LLM evaluation is mostly the craft of building tests you can trust: test sets, automatic checks, models as judges, and the statistics to tell an improvement from noise. You'll start by being the judge yourself.

1. Grade the graders

Eighteen answers to questions about this course. Some are right, some wrong, some right with padding. Judge each one. At the end you'll see how a simple automatic check and a small model judge agreed with the labels, and with you.

An eval is only as good as its grading, so check every grader, human or machine, against careful labels before trusting it.

2. Automatic checks: cheap, strict, and often enough

For questions with a short right answer, plain code grades well: normalize both texts (lowercase, drop punctuation), then check for an exact match, or that an accepted answer appears as whole words in the reply. For structured output, parse it and validate the fields (the using-LLMs lesson). These checks are free, instant and perfectly repeatable, so they run on every change.

Exact match is too strict: "ResNet-50 has 50 layers." is right and doesn't match "50". The contains-check is much better (16 of 18), and its two misses are instructive: "Despite the 50 in its name, ResNet-50 has 152 layers" and "A starts at zero; B is random" both contain the right word next to a wrong claim, and both pass. Every automatic check is a trade between being fooled and being too strict; look at its disagreements.

Beyond string matching, teams write behavioral tests (the CheckList methodology, Ribeiro et al., 2020) to unit-test capabilities across systematic variations:

Code-based checks (normalize, match, parse, validate) and CheckList behavioral tests (MFT, INV, DIR) are free and repeatable; tune them on their disagreements with careful labels.

3. A model as the judge

For open-ended answers (a summary, an explanation, an email) no string check works, so teams ask a strong model to grade with a rubric: "Given the question, the reference answer and the reply, is the reply correct? Answer yes or no." It scales, and with a capable judge it agrees with human graders about as often as two humans agree with each other. But it's a model with its own errors, and its agreement has to be measured, not assumed. In the practice notebook the judge is SmolLM2-360M, the same small model as before: it agrees with the labels on 11 of 18 answers, 61%. A "judge" that answers "correct" to everything gets 9 of 18, because half the answers are correct; so 61% is barely better than not reading at all.

\[ \kappa = \frac{p_o - p_e}{1 - p_e} \]

Cohen's kappa corrects agreement \(p_o\) for the agreement \(p_e\) two graders would reach by chance given how often each says yes: 1 is perfect, 0 is chance. The small judge's kappa is 0.22; the contains-check's is 0.78. Known biases of model judges, even strong ones:

Labels: 8 correct, 8 not. A "judge" says "correct" to everything. Its agreement is 50%. What is its kappa?

\(p_o = 0.5\). By chance: it says yes 100% of the time and the labels say yes 50%, so \(p_e = 1 \times 0.5 + 0 \times 0.5 = 0.5\). \(\kappa = (0.5 - 0.5)/(1 - 0.5) = 0\): exactly chance, however good 50% (or 90%, on a lopsided set) sounds.

A model judge scales grading to open-ended answers, but it's another model to validate: measure its agreement with human labels (kappa, not raw agreement) and test for position and length bias.

Why raw agreement flatters a lazy judge
  1. Suppose 90% of a system's answers are correct. A judge that always says "correct" agrees with the labels 90% of the time while doing no judging at all.
  2. The chance agreement for two graders who say yes with rates \(a\) and \(b\), independently, is \(p_e = ab + (1 - a)(1 - b)\): matching yeses plus matching nos.
  3. For the always-yes judge on a 90%-correct set: \(p_e = 1 \times 0.9 + 0 \times 0.1 = 0.9\), so \(\kappa = (0.9 - 0.9)/(1 - 0.9) = 0\).
  4. A judge that's genuinely good gets \(p_o\) well above \(p_e\); kappa rescales that gap to 0–1. Values above about 0.6 are usually called substantial, above 0.8 near-perfect.
  5. Same lesson as chapter 12's 99%-accurate fraud model: compare with what a do-nothing grader would get.

4. Faithfulness: is every claim in the sources?

For a RAG system, "correct" splits in two: answer correctness (matches the truth) and faithfulness (every claim is supported by the retrieved passages). The retrieval notebook's small model, asked "Why does Adam need bias correction?" from passages of the optimizers lesson, began correctly ("it uses a running average of recent gradients") and then claimed bias correction "helps to prevent the bias-variance tradeoff", which no passage says. A faithfulness check splits the answer into claims and asks, for each, whether a passage supports it: chapter 8's number check for Blip is a cheap version, a judge model a general one.

How do you automate faithfulness without relying on a slow, verbose chat model? Use a Natural Language Inference (NLI) classifier as an automated filter. NLI treats the retrieved source passage as the premise and each sentence of the model's answer as a hypothesis, classifying the relationship into three categories:

An NLI classifier is a small model, far cheaper than a chat model as judge, so it can check every sentence of every answer. Sentences it doesn't score as entailed get flagged, dropped, or sent back for another retrieval.

It has misses of its own, so treat it like the graders in section 1: measure its agreement with your labels before trusting it. NLI models learn from short sentence pairs, so they get worse when the premise is a long passage (split it and check the claim against each chunk); they're weak on numbers, dates and units ("ResNet-50 has 152 layers" can look entailed by a passage about ResNet-50); and a claim stitched together from two passages is entailed by neither one alone.

For grounded systems, grade faithfulness (every claim supported by the passages) separately from correctness; an NLI model is a cheap first filter, checked against labels like any other grader.

5. Is the new version actually better?

Change a prompt, a model or a retriever, rerun the eval, compare. That comparison is Probability 4 again: both versions answer the same questions, so use a paired test on the questions where they disagree. In the notebook, adding retrieval to SmolLM2-360M raised its score on 20 short questions from 9 to 13. All 4 changed questions went RAG's way, and still the exact test says \(p = 2 \times 0.5^4 = 0.125\): with 20 questions, even a clean 4-to-0 isn't conclusive.

How should a team catch a prompt change that quietly breaks 5% of answers?

Chapter 8's margin: with 20 cases, ±13 points; to see 5 points you need hundreds. Keep the set fixed so results compare across versions, add every real failure to it, and keep a slice that nobody tunes prompts on (the experiments lesson's worn-out test set).

Compare versions on the same fixed questions with a paired test; a few hundred cases are needed to see changes of a few points.

Where it breaks: evals that stop measuring

6. What to carry forward

All lessons: AI from scratch. The 16 graded answers and 20 questions: ai/data/llm-eval.json. Anthropic's and OpenAI's guides on building evals are good next reads.

Last updated