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
- An eval is a fixed set of real tasks plus graders; rerun it on every prompt, model, tool or retrieval change.
- Grade outcomes and end state, plus constraints on the path (required and forbidden tools, step limits), not an exact sequence of calls.
- 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.
- Report cost per successful task and p95 latency beside quality.
- 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
flowchart LR
G[Golden set<br/>real tasks + expected outcomes] --> R[Run the agent<br/>version under test]
R --> T[Trajectories:<br/>answer, tool calls,<br/>end state, tokens, time]
T --> C[Code graders<br/>exact, schema, end state]
T --> J[LLM judge<br/>rubric, calibrated]
C --> S[Scorecard<br/>success, steps, cost, p95]
J --> S
S --> D{Better than the<br/>current version?}
D -->|yes| SHIP[Ship]
D -->|no| FIX[Fix and rerun]
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:
- Outcome: is the database in the expected end state? Does the answer contain the facts it must?
- 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
flowchart TD
R[A finished run] --> E{End state as<br/>expected?}
E -->|no| F[Fail]
E -->|yes| A{Answer has the<br/>required facts?}
A -->|no| F
A -->|yes| Q{Required tools used,<br/>forbidden tools avoided?}
Q -->|no| F
Q -->|yes| S{Steps within<br/>the limit?}
S -->|no| F
S -->|yes| P[Pass]
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
sequenceDiagram
participant H as Human reviewers
participant J as LLM judge
participant C as Calibration
H->>C: labels for 20 sampled answers
J->>C: labels for the same 20
C->>C: agreement p_o, chance p_e, kappa
alt kappa >= 0.6
C-->>J: trusted for this rubric and judge model
else kappa < 0.6
C-->>H: fix the rubric, add examples, re-check
end
Figure 4 · Chart
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
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
sequenceDiagram participant Dev as Engineer participant CI as CI pipeline participant E as Eval runner Dev->>CI: change the prompt (v2) CI->>E: run golden set on v1 and v2 E-->>CI: v1: 4/4 pass, v2: 3/4 pass CI->>CI: task refund-over-limit passed before, fails now CI-->>Dev: release blocked, with the failing trace
Figure 6 · Chart
v1 passes all four golden tasks; v2 passes three and fails only the over-limit refund
Figure 7 · Chart
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
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
flowchart LR P[Production traffic] --> S[Signals<br/>thumbs, rephrase,<br/>retry, abandon, escalate] S --> T[Flagged traces] T --> H[Human review] H --> G[New golden task] G --> CI[Release gate] CI --> P
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 paperCohen, A Coefficient of Agreement for Nominal Scales (1960), Introduced kappa, agreement corrected for chance, which this lesson uses to check a judge.
The paperEs, 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 paperResearcher'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.