rumblr Work in progressWIP

● The AI Primer · Lesson 48 · Part 6: building systems people rely on

Evaluation

Evals: turning "it seems better" into numbers you can gate a release on

This lesson covers How to know a change helped before a customer finds out that it did not

Members · open during launch 23 min8 figures and diagrams

What this is

What is an eval?

An eval (short for evaluation) is a test for an AI system: a fixed set of real tasks, a grade for each result, and a score. It is rerun every time a prompt, an AI model, a tool or a search setting changes, so that a hunch becomes a number a release can be held to. Think of a driving test: a fixed route with known hazards and a checklist, retaken whenever the car changes. The example here is a small customer-support agent, graded in code where code can check the result and by a second language model where it cannot. A reworded prompt makes it cheaper and passes a spot check, and the eval catches it refunding an order of 900 that its rules say must go to a person.

Why it matters: The moment a change can reach users, a reworded prompt or a new version of the language model can break something that worked, and without an eval the break is found by a customer. Trying your three favourite questions by hand is not an eval.

At a glance

Key takeaways

  1. An eval is a fixed set of real tasks plus graders; rerun it on every prompt, model, tool or retrieval change.
  2. Grade outcomes and end state, plus constraints on the path (required and forbidden tools, step limits), not an exact sequence of calls.
  3. Use code graders wherever possible; use an LLM judge with a rubric for open-ended output, and calibrate it against humans with Cohen's kappa.
  4. Report cost per successful task and p95 latency beside quality.
  5. Gate releases on regressions, and turn every production failure into a golden task.

How it works, from scratch

Take a small customer-support agent. It has three tools (look up an order, refund it, or escalate it to a person) and one rule: a refund above 500 must go to a person. Someone rewords its prompt. To find out whether the new version is better, run both versions on the same fixed tasks, the golden set, and grade every run. A run leaves behind its trajectory: the answer, every tool call, the end state of the order database, the tokens used and the time taken. The grades add up to a scorecard for each version, and the two scorecards decide whether the new version ships. That whole procedure is an eval.

Figure 1 · Diagram

Reading it: follow one version of the agent from left to right. Only the second box changes from one eval run to the next; the golden set stays the same. The trajectory goes to two kinds of grader, code and a language model acting as judge, and both feed one scorecard. The diamond is the release gate: a new version ships only if it does at least as well as the current one on the same tasks.

In code: evaluate runs every golden task through run_task, grades each run and returns the scorecard. View source

Chapter 1

The golden set

The support agent's golden set has four tasks. Each one runs against the same small order database: A100 has shipped (total 25), A200 has been delivered (total 40), and A300 has been delivered (total 900). For each task, the right-hand column says what must be true when the agent has finished.

Task id User says Pass means
status "Where is order A100?" answer names A100 and "shipped"; no refund issued
refund-small "Please refund order A200, it arrived broken." A200 ends up refunded
refund-over-limit "Refund order A300, the TV was damaged." A300 ends up escalated, not refunded
out-of-scope "Can you change my shipping address to Paris?" answer hands off to the support team; no refund

A real golden set starts with 30 to 50 requests taken from real traffic, and grows by one task for every failure found in production (online evaluation, below).

In code: Task holds one golden case and GOLDEN_TASKS is the table above; Run holds one trajectory. View source

Chapter 2

Grade outcomes, not paths

With the tasks fixed, every run needs a grade. An agent can reach the right result by more than one route, so a grader that demands one exact sequence of tool calls fails runs that did the job. Grade two things instead:

  1. Outcome: is the database in the expected end state? Does the answer contain the facts it must?
  2. Constraints on the path: were the required tools used, were the forbidden tools avoided, and did the run stay within the step limit?

For refund-small, a run that calls refund first and lookup_order second passes: the end state is right, the required tool was used, and 2 steps is within the limit of 5. For status, a run with a perfect answer that also called refund fails, because refund is forbidden on a status question.

Figure 2 · Diagram

Reading it: a run passes only if it gets through all four diamonds, from top to bottom. No diamond looks at the order of the tool calls, so two different correct runs both pass. A run with the right answer that called a forbidden tool fails at the third.

Each diamond is a code grader, a check written in code; an exact match against an expected answer and a schema check are two more. Code graders are cheap and fast, and give the same answer every time, so use them wherever code can check the outcome. Trajectory metrics don't pass or fail a run. They measure how well it got where it did, which helps find what went wrong. One is tool selection accuracy:

The formula and its symbols

Symbols

Symbol Meaning here
the set of tools this task needs
the set of tools the agent actually called
tools in both sets
how many tools a set contains

In words: the share of the tools the task needs that the agent actually used.

New to Σ, ∂ or ‖x‖? The symbols reference decodes each one as a few lines of Python.

On a worked example: a refund needs {lookup_order, refund}; an agent that only looked the order up scores |{lookup_order}| / 2 = 0.5.

In Python:

T_required = {"lookup_order", "refund"}
T_called = {"lookup_order"}
# ∩: tools in both sets
T_required & T_called  # → {'lookup_order'}
len(T_required & T_called) / len(T_required)  # → 0.5

In code: grade_run walks the diamonds and returns a Grade listing every reason a run failed; exact_match is the simplest code grader; tool_selection_accuracy is the formula above. View source

Chapter 3

LLM-as-judge, and checking the judge

Code can check that an answer names order A100. It cannot check whether an open-ended answer is clear or helpful. For that, the grader is an LLM judge: a second language model given an explicit rubric, a short list of pass/fail criteria, and asked to grade. A judge is only worth using if it agrees with people. Checking that is called calibration: people label a sample of answers, the judge labels the same sample, and the two sets of labels are compared.

Counting how often they agree is misleading. A judge that always says "pass" agrees with the people on every answer they passed. Cohen's kappa corrects for the agreement that would happen by chance:

The formula and its symbols

Symbols

Symbol Meaning here Range
kappa (Greek letter), the chance-corrected agreement 1 = perfect, 0 = no better than chance, below 0 = worse
observed agreement: share of items where judge and human give the same label 0 … 1
agreement expected by chance, if each labelled at their own rates independently 0 … 1
a label, here "pass" or "fail"
share of items the human gave label 0 … 1
add up over both labels

In words: kappa is how much of the possible improvement over chance agreement the judge actually achieves.

On the worked example: 20 answers; both say pass on 10, both fail on 6, they disagree on 4. So = 16/20 = 0.80. The human passes 12/20 = 0.6 and the judge passes 12/20 = 0.6, so = 0.6·0.6 + 0.4·0.4 = 0.52. Then κ = (0.80 − 0.52) / (1 − 0.52) = 0.28 / 0.48 = 0.583. A judge that always says pass agrees 60% of the time, but = 0.6·1 + 0.4·0 = 0.6, so κ = (0.6 − 0.6) / 0.4 = 0: all of its agreement is luck. A common rule of thumb is to want κ ≥ 0.6 before trusting a judge unsupervised. The judge in this example, at 0.583, falls just short, so its rubric needs work.

In Python
# both pass on 10, both fail on 6
p_o = (10 + 6) / 20
p_human = {"pass": 12 / 20, "fail": 8 / 20}
p_judge = {"pass": 12 / 20, "fail": 8 / 20}
# Σ over the labels
p_e = sum(p_human[label] * p_judge[label] for label in p_human)
round(p_e, 2)  # → 0.52
# κ
round((p_o - p_e) / (1 - p_e), 3)  # → 0.583
always_pass = {"pass": 1.0, "fail": 0.0}
p_e = sum(p_human[label] * always_pass[label] for label in p_human)
# agrees 60% of the time, all by luck
round((0.6 - p_e) / (1 - p_e), 3)  # → 0.0

Figure 3 · Diagram

Reading it: the people and the judge send their labels separately, and only the calibration step sees both. At 0.6 or above, the judge is trusted, but only for this rubric and this judge model. Below it, the last arrow goes back to the people, who fix the rubric and check again. A new judge model or a changed rubric means calibrating again.

Figure 4 · Chart

near-perfect worked example coin flip always pass 0.0 0.2 0.4 0.6 0.8 Judges vs. the same 20 human labels trust threshold raw agreement Cohen's kappa

Near-perfect judge: 95% agreement, kappa 0.89; worked example: 80%, kappa 0.58; coin flip at 50% and always-pass at 60% both score kappa 0

Reading it: each pair of bars is one judge scored against the same 20 human labels, grey for raw agreement and blue for kappa, with the dashed line at the 0.6 trust threshold. Only the near-perfect judge's blue bar clears the line; the judge from the worked example stops just under it. The coin flip and the always-pass judge have no blue bar at all, a kappa of 0, although the always-pass judge agrees more often than the coin.

In code: rubric_judge asks a judge model to grade an answer against the rubric; cohens_kappa computes from two lists of labels; calibrate_judge decides whether the judge is trusted; always_pass_judge is the useless judge. View source

Chapter 4

Metrics for retrieval-augmented answers

A judge grades a whole answer. An agent that answers from retrieved documents (RAG, retrieval-augmented generation; see Retrieval-augmented generation) can go wrong in either of two halves, finding the documents or writing from them, so each half gets its own score. Retrieval recall@k is the share of the relevant documents that appear in the top k search results. Faithfulness is the share of the answer's claims that the retrieved documents support. If recall is low, fix the search. If recall is high but faithfulness is low, fix how the answer is written.

In code: recall_at_k scores the retrieval half; faithfulness scores the generation half with primer.agents.guardrails.groundedness. View source

Chapter 5

Quality next to cost and speed

Grades say whether the agent did the job. Two more numbers go beside them on the scorecard: what each version costs and how long it takes. A change that adds 2% success but doubles the cost may not be worth shipping. Latency is reported as p95, the 95th percentile: sort all response times, and p95 is the time that 95% of requests come in at or under. An average would hide the slowest requests, which are the ones users notice. Cost is reported as cost per successful task (total cost ÷ number of successes), because a cheap run that fails still has to be paid for.

In code: percentile computes p95 by the sort-and-count rule above, and evaluate puts it and the cost per successful task on the scorecard. View source

Chapter 6

The release gate: catching regressions

Everything so far scores one version. A release compares two: the current version and the candidate that would replace it. A regression is a task that used to pass and now fails. The release gate runs both versions on the whole golden set and blocks the release if any task regressed or the success rate dropped.

Take v1, which passes all four tasks, and v2, whose prompt was edited to "always be maximally helpful". v2 now refunds A300 (900, over the 500 limit) instead of escalating it. It also skips looking the order up, which makes it cheaper. The gate names the task that regressed, refund-over-limit, and blocks the release.

Figure 5 · Diagram

Reading it: no person runs the eval: the change goes to CI (continuous integration: the checks that run on every proposed change before it can merge), which runs the golden set on both versions, as it runs unit tests. The last arrow names the task that regressed and carries the trace of what the agent did, so whoever fixes it starts from evidence.

Figure 6 · Chart

status refund-small refund-over-limit out-of-scope fail pass Golden set results by task v1 v2 ("maximally helpful")

v1 passes all four golden tasks; v2 passes three and fails only the over-limit refund

Reading it: each pair of bars is one golden task, blue for v1 and red for v2. A bar that reaches "pass" passed; a missing bar failed. All four blue bars are there. The one red bar missing is the over-limit refund: one task in four, easy to miss in a spot check, and a 900 refund each time it happens in production.

Figure 7 · Chart

status refund-small refund-over-limit out-of-scope 0.0 0.5 1.0 1.5 2.0 tool calls Steps per task (fewer is not better if it's wrong) v1: $2.38 per 1k successes v2: $2.24 per 1k successes

v2 makes one tool call on each refund where v1 makes two, so it is slightly cheaper per success (2.24 vs 2.38 dollars per 1,000) despite the broken limit

Reading it: each pair of bars counts the tool calls one task took, blue for v1 and red for v2. On both refunds the red bar is one call shorter: v2 no longer looks up the order total, the step that enforces the limit. The legend shows v2 cheaper even per successful task, because skipping a check saves what the check costs. The 900 it refunds each time appears on no token bill; only the quality gate catches it.

In code: run_task runs either version against a fresh copy of the order database, and compare_versions is the gate. View source

Chapter 7

Online evaluation: closing the loop

The gate can only test the cases in the golden set, and real users ask for things nobody wrote down. Once the agent is live, production is the second source of evidence. Collect explicit feedback (thumbs up or down) and implicit signals: rephrasing the same question, retrying, abandoning, or asking for a human all suggest the answer didn't help. Sample live traces (the recorded step-by-step history of a request; see Observability) for people to review. Alert on drift, a metric creeping away from its usual level after a deploy or as traffic changes. Turn every confirmed failure into a new golden task.

Figure 8 · Diagram

Reading it: start at production traffic and follow the arrows round. Each lap turns a confirmed failure into a golden task, and from then on the release gate runs that task against every new version.

In code: a Signal is one piece of user feedback; implicit_dissatisfaction_rate is the share of sessions with any unhappy signal; promote_to_golden turns a confirmed failure into a new Task. View source

Test yourself

5 questions

Answer each one out loud or on paper before you open it.

Question 1How do you evaluate an agent whose correct path isn't fixed?Think it through, then reveal

Grade what must be true at the end, not how it got there: the end state of the systems it touched (the database row, the ticket, the file), the facts the answer must contain, and constraints on the path (required tools used, forbidden tools avoided, step and cost limits). Add trajectory metrics (tool selection accuracy, argument correctness, steps, tokens) as diagnostics. For open-ended outputs, use a rubric-based LLM judge that's calibrated against human labels.

Question 2Your LLM judge agrees with humans 85% of the time. Is it good?Think it through, then reveal

Not necessarily. If 85% of answers are good, a judge that always says "pass" also agrees 85% of the time. Compute Cohen's kappa, which subtracts chance agreement; look at the disagreements; and recheck whenever the judge model or rubric changes.

Question 3A new prompt improves the average score. Do you ship it?Think it through, then reveal

Only after checking for regressions task by task, cost and latency changes, and whether the improvement is bigger than run-to-run noise (rerun, or use more cases). An average can go up while a critical case, like an over-limit refund, breaks.

Question 4How do you build the first eval set for a new agent?Think it through, then reveal

Collect 30 to 50 real requests (from logs, support tickets, or domain experts), write down for each what must be true afterwards, and write code checks for as many as possible. Run it on every change from day one. Then grow it from production: every flagged failure becomes a case.

Question 5What would you monitor once the agent is live?Think it through, then reveal

Task success (from outcome checks and sampled human review), implicit dissatisfaction (rephrase, retry, abandon, escalate rates), cost per successful task, p95 latency, tool error rates, and drift in any of these after a deploy.

Primary sources

The papers behind this lesson

Zheng et al., Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena (2023), Measured how well strong models agree with human preferences when used as graders, and catalogued their biases (position, verbosity, self-preference), the basis for calibrating a judge before trusting it.

Read on rumblr →The paper

Cohen, A Coefficient of Agreement for Nominal Scales (1960), Introduced kappa, agreement corrected for chance, which this lesson uses to check a judge.

The paper

Es, James, Espinosa-Anke & Schockaert, RAGAS: Automated Evaluation of Retrieval Augmented Generation (2023), Proposed reference-free metrics such as faithfulness and context relevance that score the retrieval and generation halves of a RAG system separately.

The paper

Researcher's shelf

Further reading

  • Anthropic, Define success criteria and build evaluations: https://docs.claude.com/en/docs/test-and-evaluate/develop-tests
  • Anthropic, Demystifying evals for AI agents: https://www.anthropic.com/engineering/demystifying-evals-for-ai-agents
  • Zheng et al., Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena (2023): https://arxiv.org/abs/2306.05685
  • RAGAS documentation (RAG metrics): https://docs.ragas.io/
  • Cohen's kappa: https://en.wikipedia.org/wiki/Cohen%27s_kappa

About this lesson. This lesson is built from the open-source AI Primer ↗. Its text, figures and numbers are generated from the Primer's source at commit f8b43d1, so the two always agree: the explanation, the code that builds it and the tests that prove it. Its hand-built interactive figures are still to come: the status page shows which lessons have them.