Chapter 29: LLM Evaluation
Evaluating a generative model is hard because there is no single right answer to compare against. So evaluation splits into two fundamentally different questions. The first is intrinsic: how well does the model predict text? That has a clean, information-theoretic answer (cross-entropy, perplexity, bits-per-byte), and it is literally the training objective, which is why “scaling means lower loss” works. The second is extrinsic: is the output actually good, meaning correct, helpful, grounded, and safe? That has no closed form, so we approximate it with benchmarks, model judges, and humans, each a noisy proxy. Mature evaluation is knowing which question you are asking and never mistaking a low perplexity for a good product.
Concepts from other sections (25)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- Perplexity: , the geometric mean of ; the effective number of choices per token. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- Retrieval-augmented generation (RAG): retrieving relevant evidence at query time and placing it in the context window, so a model answers from fresh, private, citable facts instead of only its parametric memory. Read more: Chapter 27: Retrieval and RAG
- LLM judge: a language model used as a scorer, treated as a noisy instrument whose malformed replies are missing readings. Read more: 29.3 LLM judge with position control
- Confidence interval: a procedure that, over repeated samples, produces intervals covering the fixed true parameter at the stated rate. Read more: 13.11 Probability problem set, part c: limit theorems and confidence intervals in Mathematics
- Entropy: , the average surprise of a distribution. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- KL divergence: , the excess cost of predicting with ; nonnegative and asymmetric. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- Branching factor: the number of choices at each node of a search tree; a backtracking search costs roughly the branching factor raised to the depth, such as for subsets and for permutations. Read more: Chapter 11: Backtracking in Programming and Algorithms
- exact match: the fraction of hypotheses equal to their reference after stripping white space at both ends. Read more: 10.5 Sequence metrics: exact match, BLEU, chrF
- Log-likelihood: , the log probability of the observed counts under a distribution . Read more: 13.3 MLE, Laplace, absolute discounting in Mathematics
- Variance: the expected squared distance from the mean; its square root is the standard deviation. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller in Mathematics
- MRR: the reciprocal of the rank of the first relevant result. Read more: 29.2 Agent evaluation scorers
- TTFT / TTLT / ITL: time to first token, time to last token, and mean inter-token latency. Read more: 29.2 Agent evaluation scorers
- time to first token (TTFT): the time until the first body byte of an answer is ready to send to the client. Read more: 25.3 The SSE streaming proxy
- Service Level Objective (SLO): a target for a defined measure of service behavior over a specified window. Read more: Chapter 13: Engineering Acronyms in Software Engineering
- Pure function: a function whose output depends only on its inputs and which mutates nothing. Read more: Chapter 4: Coding and Design Patterns in Software Engineering
- Acceptance rate: accepted draft tokens over proposed draft tokens; per token, . Read more: 21.6 Speculative decoding: n-gram, prompt-lookup, and model drafts
- Scaling laws: empirical power laws relating loss to compute, data, and parameter count. Read more: Chapter 2: Deep Learning
- cluster bootstrap: a bootstrap that resamples cases, each bringing all its samples, so correlated samples do not inflate confidence. Read more: 29.1 Eval runner
- strict parsing: accepting exactly one JSON object with known keys and valid values, and nothing after it. Read more: 29.3 LLM judge with position control
- position bias: a judge’s tendency to prefer whichever answer occupies a particular slot. Read more: 29.3 LLM judge with position control
- Cohen’s kappa: agreement between two raters corrected for the agreement their label marginals would produce by chance. Read more: 29.3 LLM judge with position control
- paired delta: the per-case difference between the experiment’s and the base’s score on the same case. Read more: 29.4 Paired A/B experiments
- paired bootstrap: a bootstrap that resamples whole pairs, so the two arms are never drawn independently. Read more: 29.4 Paired A/B experiments
- p-value: the fraction of null arrangements at least as extreme as the observed one, with a correction. Read more: 29.4 Paired A/B experiments
This chapter starts with the intrinsic side, because the training loss is the first and cheapest evaluation you have, and then moves outward to benchmarks, LLM-as-judge, RAG metrics, and agent metrics. It ends by treating evaluation as what it really is on a platform: a pipeline you build, not a number you read. The four sections that follow the overview build that pipeline for agents in Go, from the runner that executes suites to the paired experiments that tell you whether a change helped.
Learning objectives
- Compute cross-entropy, perplexity, and bits-per-byte from per-token log-probabilities, and explain why they are one quantity.
- Explain why per-token metrics are unfair across tokenizers and why bits-per-byte is comparable.
- Choose the right evaluator (benchmark, LLM judge, human, code scorer) for a question, and name each one’s biases.
- Decompose RAG and agent failures into retrieval, generation, tool use, and latency metrics.
- Build an eval harness that runs suites, scores outputs, reports confidence intervals, and compares two versions with a paired test.
Before you begin
Section titled “Before you begin”You should be comfortable with entropy, KL divergence, and cross-entropy from Information Theory in Mathematics, and with the training loss from Deep Learning in AI Engineering. The RAG metrics assume you know what a retrieval pipeline does, from Retrieval and RAG. The agent sections use the agent SDK from the agent SDK chapter and the durable EvalSuite workflow from Durable orchestration and workers.
Cross-entropy: the loss that is an eval
Section titled “Cross-entropy: the loss that is an eval”A language model outputs a probability distribution over the next token, and it is trained to minimize the cross-entropy loss
the average negative log-probability the model assigned to the actual next token. With log base 2 the unit is bits; with the natural log it is nats.
The information-theoretic meaning is what makes this the foundation. Cross-entropy is the average number of bits needed to encode samples from the true distribution using the model’s distribution . The model’s loss is the cross-entropy between real text and its predictions. Minimizing loss is learning the true distribution, which is compressing text: a better language model is literally a better compressor, as Shannon showed. This is why evaluation begins at training. The loss curve is your first, cheapest, and most honest signal.
Perplexity, bits-per-token, and bits-per-byte
Section titled “Perplexity, bits-per-token, and bits-per-byte”That one quantity shows up under several names, normalized in different ways. The comparison below is the crux:
| Metric | Definition | Reads as | Tokenizer-dependent? |
|---|---|---|---|
| Cross-entropy | average (bits per token) | bits to predict the next token | Yes |
| Bits-per-token | cross-entropy in bits | the same thing, named for the unit | Yes |
| Perplexity (PPL) | the effective number of equally likely choices: the branching factor | Yes | |
| Bits-per-byte (bpb) | total bits / total UTF-8 bytes of text | compression rate, tokenizer-free | No |
| Bits-per-character (bpc) | total bits / total characters | an older character-level variant | No |
Perplexity is the most intuitive: a PPL of 100 means the model is, on average, as uncertain as if it were choosing uniformly among 100 tokens. Lower is better.
The trouble is that perplexity and bits-per-token depend on how text is split. A model with a bigger vocabulary has fewer, “easier” tokens and looks better on per-token metrics, which makes the comparison unfair. Bits-per-byte fixes this by dividing the total cross-entropy, summed in bits over the sequence, by the number of bytes of the underlying text rather than the number of tokens. The result is a tokenizer-independent compression rate you can compare across models; The Pile and language-modeling leaderboards use it, and it is the rigorous way to compare base models. The conversion is
the same loss, renormalized from per-token to per-byte. A good exercise is to take two models with different tokenizers, compute both metrics on the same held-out text from per-token log-probabilities, and watch perplexity flatter the larger vocabulary while bits-per-byte does not.
Intrinsic versus extrinsic evaluation
Section titled “Intrinsic versus extrinsic evaluation”Everything so far is intrinsic: it measures modeling quality on held-out text. It is cheap, automatic, and ideal for pretraining and tracking scaling, but it is blind to usefulness. A model can have great perplexity and still be unhelpful, biased, or wrong. Extrinsic evaluation measures task performance: does the model answer correctly, follow instructions, stay grounded, and refuse appropriately? There is no closed form, so it is approximated with benchmarks, judges, and humans, and it is what users actually feel. The discipline is simple to state: use intrinsic metrics to steer training, use extrinsic metrics to decide whether the model is good, and never ship on perplexity alone.
Benchmarks and capability evals
Section titled “Benchmarks and capability evals”Task benchmarks are the first extrinsic tool. MMLU measures knowledge, GSM8K and MATH measure reasoning, HumanEval and MBPP measure code, and HellaSwag measures commonsense. Aggregators such as HELM and EleutherAI’s lm-evaluation-harness run many tasks with standardized prompts and scoring. A benchmark scores either by exact match or multiple-choice log-likelihood (pick the highest-probability option), or by generating an answer and running a checker.
The central threat is contamination: if benchmark data leaked into training, the score measures memorization, not capability. Prefer fresh, private, or held-out evals, and treat gains on public benchmarks skeptically. And remember that capability is not deployment fit. A high MMLU score does not mean the model is good at your task; for that you need a domain eval set.
LLM-as-judge and preference evaluation
Section titled “LLM-as-judge and preference evaluation”For open-ended outputs, you can prompt a strong model to grade an output (a pointwise score) or to pick the better of two (pairwise). This is scalable and cheap, and it is the default for subjective quality. But judges have biases you must control: position bias (favouring the first or second answer, which you control by asking in both orders and reconciling the verdicts), verbosity bias (longer looks better), self-preference (favouring its own model family), and limited discrimination on hard cases. Always validate the judge against human labels. Rubrics and reference-guided judging, which give the judge criteria or a gold answer, reduce variance.
Many pairwise verdicts can be aggregated into Elo or Bradley-Terry ratings, as LMSYS Chatbot Arena does. Humans voting on pairs is the closest thing to ground truth for general quality. The point to carry forward is that benchmarks measure capability, LLM judges measure preference, and humans are the ground truth; each evaluator has different biases, so triangulate rather than trusting one.
RAG evaluation
Section titled “RAG evaluation”A RAG system has two stages, and its evaluation should separate them. The metrics divide cleanly by stage:
| Stage | Metric | Asks |
|---|---|---|
| Retrieval | Context precision / recall, MRR, NDCG, Recall@k | Did we fetch the right chunks, ranked well? |
| Generation | Faithfulness / groundedness | Is every claim supported by the retrieved context (no hallucination)? |
| Generation | Answer relevance / correctness | Does it actually answer, and match the truth? |
The value of this split is that it decomposes failure. A bad RAG answer is either a retrieval miss (the right context was not fetched) or a generation miss (the context was fetched but ignored or contradicted). Comparing faithfulness with context recall tells you which, so you fix the right stage. Frameworks such as Ragas use LLM-as-judge to compute these metrics, and saige’s eval/ package exposes exactly this set (ContextPrecision, ContextRecall, NDCG, MRR, Faithfulness, AnswerCorrectness) behind a composable Scorer interface.
Agent and system evaluation
Section titled “Agent and system evaluation”Agents need their own metrics on top of these. Task success, whether the agent accomplished the objective end to end, is the metric that matters most. Tool-call accuracy asks whether it chose the right tool with the right arguments and a valid schema, and whether it recovered from tool errors. Trajectory metrics track turn count, redundant steps, and loops. Operational metrics include TTFT (time to first token), TTLT (time to last token), cost per task, and tokens per task; quality is meaningless if the answer is too slow or too expensive, which ties evaluation to Streaming and SSE and to serving SLOs. saige’s agent scorers track exactly these: TTFT and TTLT, tool success, and turn count.
The eval harness pattern
Section titled “The eval harness pattern”All of this adds up to evaluation as platform infrastructure rather than a notebook. The core is a composable Scorer interface: a metric is a pure function from input, output, and optional reference to a score, which is a functional pattern, and the harness runs dataset times model times scorers into an aggregated report. saige’s framework, an Observation plus pluggable Scorers across RAG, agent, and knowledge-graph subsystems, is this pattern, and so is what you build in this chapter.
Eval datasets are versioned assets: curate, freeze, and review them like code, and guard them against contamination and drift. Run the eval suite in CI on every model or prompt change and block on regressions, the evaluation analogue of unit tests discussed in Software Engineering. And combine offline and online signals: offline evals on fixed sets before you ship, online signals (user feedback, A/B tests, acceptance rate) after, closing the loop back to training data.
When you need to pick a metric quickly, this mapping from question to measurement summarizes the chapter so far:
| Question | Metric |
|---|---|
| How well does the base model predict text? | Cross-entropy / perplexity |
| Compare base models with different tokenizers | Bits-per-byte (bpb) |
| General capability | Benchmarks (MMLU, GSM8K, HumanEval); watch contamination |
| Open-ended quality, at scale | LLM-as-judge (control position and verbosity bias) |
| General “which model is better” | Pairwise plus Elo (human arena) |
| Did RAG retrieve the right context? | Context precision/recall, NDCG, MRR |
| Did RAG hallucinate? | Faithfulness / groundedness |
| Is the agent actually working? | Task success plus tool accuracy plus TTFT/TTLT |
| Ship-gate a change | Versioned eval suite in CI |
Where these ideas show up in practice
Section titled “Where these ideas show up in practice”The same patterns appear across the industry. OpenAI and Anthropic combine held-out loss with capability and safety evals: bits-per-byte, internal evals, and red-teaming. LMSYS turns human pairwise votes into Elo ratings in Chatbot Arena. Stanford CRFM standardizes multi-metric benchmarking in HELM, and EleutherAI provides a reproducible benchmark harness in lm-evaluation-harness. Ragas and other eval vendors use LLM-as-judge for RAG metrics such as faithfulness and context precision and recall, and saige, the reference implementation for this chapter, applies one composable Scorer across RAG, agent, and knowledge-graph metrics.
The ideas also connect to other parts of the library. Entropy, KL divergence, and compression in Information Theory explain why the loss measures bits. Deep Learning covers the training objective and scaling laws behind the loss as the first eval. Retrieval and RAG is the pipeline that context precision, recall, and faithfulness evaluate. LLM Systems and Streaming and SSE cover the TTFT, TTLT, cost, and serving SLOs behind operational evaluation. Coding and Design Patterns motivates the scorer as a composable pure function, Software Engineering treats eval suites as CI-gated tests, and the agent eval harness case study works through grading an agent that changes state, with replay, set-based checks, oracles, and mutants.
In this chapter
Section titled “In this chapter”The four sections build the agent evaluation pipeline in Go, each plugging into the one before it. As you read, try to predict each aggregate and each step before you read the solution’s.
- Eval runner: run every case of a suite through a subject concurrently and deterministically, exclude and count failed scores, and report means with cluster bootstrap intervals in a schema-defined file.
- Agent evaluation scorers: deterministic scorers for text, schema, timing, tool use, retrieval ranking, citations, and state changes graded as sets.
- LLM judge with position control: a rubric judge with strict parsing, a pairwise comparison that detects position bias, Cohen’s kappa against human labels, and deterministic sampling.
- Paired A/B experiments: compare two versions on the same cases with paired deltas, a paired bootstrap interval, and a sign permutation p-value.
Key terms
Section titled “Key terms”- Intrinsic evaluation: measuring how well a model predicts held-out text, with cross-entropy, perplexity, or bits per byte; cheap and automatic, ideal for steering training, but blind to usefulness.
- Extrinsic evaluation: measuring whether a model’s outputs are good for a task (correct, helpful, grounded, safe) with benchmarks, judges, and humans; it decides whether a model is fit to ship.
- Benchmark contamination: benchmark data leaking into a model’s training set, so its score measures memorization instead of capability; countered with fresh, private, or held-out evals.
- Verbosity bias: an LLM judge’s tendency to rate longer answers higher regardless of quality.
- Self-preference bias: an LLM judge’s tendency to favour outputs from its own model family.
- Elo rating: a rating computed from many pairwise wins and losses, as Chatbot Arena does with human votes, that ranks models on general preference.
- Faithfulness / groundedness: whether every claim in a generated answer is supported by the retrieved context; low faithfulness with good retrieval means the generation stage hallucinated.
- Context precision / context recall: retrieval-stage RAG metrics asking whether the retrieved chunks are relevant and whether all the context needed for the answer was retrieved.
- Task success: whether an agent accomplished its objective end to end; the agent metric that matters most.
- Tool-call accuracy: whether an agent chose the right tool with valid arguments and recovered from tool errors.
- Trajectory metrics: measures of an agent’s path to an answer, such as turn count, redundant steps, and loops.
- Eval harness: evaluation built as infrastructure, in which versioned datasets run through a model and a set of composable scorers into an aggregated report, run in CI on every model or prompt change to block regressions.
Further reading
Section titled “Further reading”- MacKay, Information Theory, Inference, and Learning Algorithms (free), for cross-entropy, perplexity, and compression.
- Stanford HELM (free).
- EleutherAI lm-evaluation-harness (free).
- Ragas documentation (free), for RAG metrics.
- The Chatbot Arena paper (free).
- The Pile (free), for bits-per-byte.
- Hendrycks et al., MMLU (free).
- saige’s
eval/package, as a reference for a composableScorerframework.