Skip to content

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.

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.

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.

A language model outputs a probability distribution over the next token, and it is trained to minimize the cross-entropy loss

L=−1N∑ilog⁡p(xi∣x<i),L = -\frac{1}{N} \sum_i \log p(x_i \mid x_{<i}),

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 H(p,q)=−∑xp(x)log⁡q(x)H(p, q) = -\sum_x p(x) \log q(x) is the average number of bits needed to encode samples from the true distribution pp using the model’s distribution qq. 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:

MetricDefinitionReads asTokenizer-dependent?
Cross-entropyaverage −log⁡2p-\log_2 p (bits per token)bits to predict the next tokenYes
Bits-per-tokencross-entropy in bitsthe same thing, named for the unitYes
Perplexity (PPL)2cross-entropy in bits=eloss in nats2^{\text{cross-entropy in bits}} = e^{\text{loss in nats}}the effective number of equally likely choices: the branching factorYes
Bits-per-byte (bpb)total bits / total UTF-8 bytes of textcompression rate, tokenizer-freeNo
Bits-per-character (bpc)total bits / total charactersan older character-level variantNo

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

bpb=loss in natsln⁡2×tokensbytes,\text{bpb} = \frac{\text{loss in nats}}{\ln 2} \times \frac{\text{tokens}}{\text{bytes}},

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.

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.

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.

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.

A RAG system has two stages, and its evaluation should separate them. The metrics divide cleanly by stage:

StageMetricAsks
RetrievalContext precision / recall, MRR, NDCG, Recall@kDid we fetch the right chunks, ranked well?
GenerationFaithfulness / groundednessIs every claim supported by the retrieved context (no hallucination)?
GenerationAnswer relevance / correctnessDoes 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.

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.

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:

QuestionMetric
How well does the base model predict text?Cross-entropy / perplexity
Compare base models with different tokenizersBits-per-byte (bpb)
General capabilityBenchmarks (MMLU, GSM8K, HumanEval); watch contamination
Open-ended quality, at scaleLLM-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 changeVersioned eval suite in CI

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.

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.

  1. 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.
  2. Agent evaluation scorers: deterministic scorers for text, schema, timing, tool use, retrieval ranking, citations, and state changes graded as sets.
  3. 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.
  4. Paired A/B experiments: compare two versions on the same cases with paired deltas, a paired bootstrap interval, and a sign permutation p-value.
  • 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.