Skip to content

Chapter 32: Case Study: Agent Evaluation Harness

An agent is given an instruction and a set of tools over a business system, such as a helpdesk or an inventory. It reads, it writes, and it stops. Grading it is much harder than grading text, because the answer is a changed state that many different call sequences can reach, because attempts must not see each other, because the model is stochastic and its provider is outside your control, and because the harness is itself software that can have silent bugs. This case study builds a harness that runs an LLM agent against a simulated system it can change and produces a grade that someone else can verify without trusting the agent, the harness author, or a second model.

Concepts from other sections (10)

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.

  • API key: a credential tl_<id>_<secret> whose public id locates a record and whose secret proves possession. Read more: 10.1 API Keys, Scopes, Model Allowlists, and Tenants 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
  • tool result: the Result every Call returns, rendered as a tool message, prefixed error: on failure. Read more: 28.2 Tools, a registry, and JSON Schema argument validation
  • Cost per successful task: total spend, including retries, cache misses, and escalations, divided by the number of tasks completed correctly; the cost figure that a routing comparison should report. Read more: 24.5 Routing implementations and selection
  • Reinforcement fine-tuning (RFT): tuning a model against an evaluator that returns a reward from 0 to 1 for each output, with gold completions optional; it fits tasks that are verifiable but lack reference answers. Read more: 24.5 Routing implementations and selection
  • Idempotency: the property that re-running a step does not change its result, which makes pipelines recoverable. Read more: Chapter 1: Foundations in Data Engineering
  • Atomic claim: a single statement that moves work from pending to claimed and reports what it moved. Read more: Chapter 13: Case Study: Exactly-Once Event API in Systems Engineering
  • Mutation testing: judging a test suite by planting small faults (mutants) in the code and counting how many the suite catches; it measures whether tests check behavior, which line coverage cannot. Read more: Chapter 3: The Testing Mentality in Software Engineering
  • Test double: a stand-in for a real dependency in a test: a fake (a working lightweight implementation), a stub (canned answers), or a mock (asserts how it was called). Fakes are preferred because mock-heavy tests couple to the implementation and break on refactors. Read more: Chapter 3: The Testing Mentality in Software Engineering
  • Regression gate: a check that fails a head run only when it is worse than the base by more than a budget and the difference is unlikely under noise. Read more: 23.12 Run comparison and the regression gate

The runnable implementation is eval_harness.py: an isolated world, deterministic tools, replay verification, a set-based checker, and an oracle and mutant suite. It uses the standard library only and needs no API key; run it with python eval_harness.py and read the table it prints. Seven attempts end with completed and fail. That table is the argument for the whole design.

Learning objectives

  • Keep execution outcome, evidence completeness, and grade as three separate facts, and decide what counts in the denominator.
  • Isolate attempts with private sessions over a shared world, and make time, ids, and tool order deterministic.
  • Grade state changes with the set relation R⊆Δ⊆AR \subseteq \Delta \subseteq A alongside trace criteria, and replay every record before grading it.
  • Test the checker with oracles and mutants before trusting any score.
  • Sample, pin, budget, and attribute live runs so that results are comparable and failures are explained.

This study applies the theory in LLM evaluation. The Grounded SQL agent develops the same ideas for a single query, and is a gentler place to start.

Four things make grading an agent harder than grading text:

  1. The answer is a changed state, and there are many correct sequences that reach it.
  2. Attempts must not see each other, or one agent’s write becomes another’s starting condition.
  3. The model is stochastic and the provider is outside your control.
  4. The harness is software. It has bugs, and a bug in a grader is silent.

The sections that follow address them in turn: first what a result even is, then isolation and determinism, then the checker and the evidence it grades, then how to test the checker, and finally what live runs revealed and how to run them responsibly.

Completed is not passed. A run can finish cleanly after editing the wrong row, so a result has three parts:

FactQuestionValues
OutcomeDid execution end, and how?completed, error, turn limit, budget limit, interrupted
EvidenceIs the record complete and consistent?verified, ungraded, lost
GradeDid every criterion pass?passed, failed

A report that collapses these cannot distinguish a model mistake from a dead worker. Decide up front what counts in the denominator. In this build, a budget cut-off counts as an attempt, because the budget is part of the task’s conditions. An external interruption, a setup failure, and an ungraded record do not count, and are reported beside the quality figure instead of inside it.

Isolation: one shared world, private sessions

Section titled “Isolation: one shared world, private sessions”

The world is loaded, validated, and indexed once, and then never mutated. Each attempt gets a thin private layer over it: a write stays in the layer, and a read falls through to the parent on a miss. That design has three properties worth having:

PropertyWhy it matters
An empty session copies nothingOpening one measured about 40 ns against a 30,000-ticket seed and a 641-ticket seed alike
A read returns an independent copyA caller that edits a returned row cannot reach shared memory
The final diff inspects only touched keysGrading cost follows the work done, not the world size

Warning: Session isolation is not process isolation. Private rows remove state conflicts, but attempts still share CPU, memory, and one provider quota. The first live batch exhausted a 30,000 token-per-minute limit, which is a harness failure and says nothing about the agent.

Isolation must also preserve the shape of the data. Real data distinguishes a field that is absent from a field that is null. In the seed this build was given, one owner field was absent 19 times and null 22 times. A harness that normalizes the two hides exactly the case agents get wrong: two live attempts failed by handling null owners and ignoring rows where the field did not exist.

Replay, which the grade will depend on, needs the same call sequence to produce the same state. Four sources of variation are under the harness’s control:

Source of variationControl
TimestampsA private logical clock that advances per write. Never wall time
Generated idsA pure function of the entity name and a per-session counter
Tool orderTools execute one at a time, in request order
Setup errorsCollect every issue and sort them, so a broken seed reports the same list each run

What you do not own is the provider. At temperature 0 with a fixed seed, the same task produced different call sequences across runs. Identical inputs support a repeatability check; they do not promise identical output. So the rule is to sample the agent, never the checker.

The grade is a set relation. Let Δ=diff⁡(start,final)\Delta = \operatorname{diff}(\text{start}, \text{final}), let RR be the required changes, and let AA be the allowed changes. The state criterion is

R⊆Δ⊆A.R \subseteq \Delta \subseteq A.

In code it reads R ⊆ delta ⊆ A. One half catches missing work, and the other catches unrequested work, as four example attempts show:

AttemptR ⊆ deltadelta ⊆ AReading
Correct row, correct valueyesyesPass
Right item, wrong warehousenonoMissing work and unauthorized work, reported separately
Did nothingnoyesMissing work only
Did the task, then added an unrequested commentyesnoUnauthorized work only

Three rules kept the checker honest. First, derive target sets from the starting state. A task that names two example tickets may require all seven that match; hardcoded ids would pass on a small seed and miss 145 targets on a large one. Second, state is not enough. Final rows cannot show a read that never happened, a refused call, or a write that was reverted, so trace criteria sit beside state criteria. Third, a read is what was returned. A read-before-write criterion credits only rows the tool actually handed back; a page that was never fetched does not count.

The checker is pure and contains no model. Evaluating the same verified record twice gives the same grade.

A record stores the world’s hash, every call with its arguments and result, the before and after image of every write, and the final touched rows. Before any criterion is evaluated, the calls are re-executed against a fresh session and compared. Evidence that does not replay is not graded, and uncertainty is never a pass.

This step exists because of a real defect. An early verifier did not compare recorded read results, and substituting one of them changed a failing attempt into a passing one. The fix was to compare the whole observation and the whole effect: results, typed errors, sequence numbers, write images, and final rows.

Two limits should be stated plainly. Replay proves consistency, not authenticity: a self-consistent record can still be fabricated. And a hash identifies an input; it does not archive it. If the file is gone, knowing its hash does not bring it back.

Try it: Delete the replay check from grade and watch three tests fail. Then edit a recorded read result by hand and see a wrong attempt pass.

The supplied fixtures tested the world’s behavior. None of them showed that a wrong answer receives a failing grade, and that needs its own evidence. Test the checker before you trust a score, using these techniques:

TechniqueWhat it proves
OracleThe task is solvable and the checker accepts a correct solution
MutantThe checker rejects one specific mistake, on the expected criterion
Coverage ruleEvery automated criterion has at least one mutant that fails it
Break it on purposeA test that has never failed has not been shown to work
Fault injectionBehavior at a crash, a short write, a lost response

A correct scripted oracle must pass, and every deliberate mutant must fail. A criterion that no mutant can fail has never been tested. The original build ended with 13 oracles and 60 mutants across 69 automated criteria. That is evidence for the tested fault set; it is not a proof against every defect, and the write-up says so.

Two smaller lessons came from the same work. A passing fixture proves nothing until you see it fail, so a temporary defect was introduced to confirm that the relevant fixture could catch it. And benchmarks lie when the compiler deletes the work: the first allocation benchmark discarded its result and reported zero allocations, and keeping the result alive corrected it.

Try it: Add a mutant of your own, predict which criteria it fails, then run it. When your prediction is wrong, find out whether the mutant or the checker is mistaken. In the original build one prediction was wrong, and the checker was right.

With a trustworthy checker, the next question is how to run the agent so that results mean something. Sample the agent, never the checker: each sample gets a fresh session and its own seed, and the record is graded once.

Pin a revision before anything runs. Hash the world, the selected tasks, the system prompt, the driver settings, the sample count, the budgets, and the build. Executing the same run directory with a changed turn limit then produces a revision mismatch that names the component that changed. Some facts belong in the revision and others are only context:

In the revisionRecorded as context only
World and contract hashesFile paths
Task text and criteriaTimestamps and process ids
System prompt hashParallelism
Model, temperature, limits
Build identity

Parallelism stays out of the revision but must be written down, because it changes error rates and latency.

Change one factor at a time. Between two live runs in this build, the code and the provider both varied, and the write-up says the difference cannot be attributed to either. And remember that small samples are small: two or three samples per task show that a task can pass and can fail. They do not give a pass rate.

Before reading on, write the evaluation protocol you would use for swapping one model for another, then compare it with the one this build planned before any comparison:

GateMeasure
QualityAutomated success plus required human acceptance
Critical casesResults per required task or failure group, because an average hides a severe defect
CoverageAccepted tasks over all requested attempts, so refusing hard cases does not look like accuracy
ReliabilityErrors, incomplete records, recovery attempts
LatencyQueue time and p50 and p95 completion
Total costAll cost, including retries and failed samples, over accepted tasks
UncertaintySample count and an interval for each comparison

The arithmetic that motivates the cost gate is simple. A model that costs $10 for 100 accepted tasks is $0.10 each. A model that costs $6 and yields 40 accepted tasks is $0.15 each. The cheaper request was the more expensive result.

The harness was run with a scripted oracle and with a small live model:

RunDriverPassed
ReferenceScripted oracle20 of 20
Live, temperature 0, first buildSmall model13 of 20
Live, temperature 0, second buildSmall model14 of 20, at $0.11 over 128 requests
Live, temperature 1, two worldsSmall model23 of 39

In the temperature 0 runs, every failed attempt ended with completed. None hit a turn, call, or budget limit. The agent stopped because it believed it was done.

Attribute every failure before you fix anything, to the agent, the world, or the task text:

SourceMeaningShare of failures in the analysed run
AgentTools worked, the agent chose badlyAll but one
WorldA wrong tool result or a missing capabilityNone
Task textThe instruction asked for something the tools could not expressOne

The task-text failure is the instructive one. The instruction said to create a ticket “describing” an issue. The create tool had no body field, so the model put the description in a comment, which the criteria forbade. Rewording the instruction took that task to 4 of 4. It also changed the task’s hash, so the new result is not comparable with the old runs and is reported separately.

The agent failures fell into three recurring patterns, two failures each:

  1. Narrow search, then stop. The agent takes the first result as the target. One attempt called a list with a limit of 1, received a total of 7 and an offset for the next page, used the first item, and stopped.
  2. Incomplete filter. The task names two statuses, the filter accepts one value, so two calls are needed. The agent made one.
  3. Skipped or added step. A requested reply was omitted, or an unrequested comment was added.

A sampled, schema-constrained model judge was built for the prose criteria, and then removed. A model judge is an opinion, and the evidence showed why. One judgment split four to one; a later three-sample judgment was unanimous, and neither fact established accuracy. One of five samples approved an attempt that had made zero writes. And the judge read text written by the agent being judged, which is untrusted input.

The resulting rule: deterministic checks decide automated criteria, and prose criteria stay not_reviewed until a person reviews them. If a judge returns later, it triages immutable evidence for human review, its opinion is stored apart from the grade, and it is calibrated on held-out human labels. Unanimous models can share one error, so unanimous cases are audited too.

Each kind of limit bounds something different:

LimitBounds
TurnsModel round trips
Tool callsWork done, which a large target set can dominate
ContextRequest size, which a turn cap does not bound
Requests and moneySpend, reserved before dispatch

Check feasibility before spending. Run the oracle for each selected task and count its calls and writes. One task’s reference solution needed 42 calls, 38 of them writes, under a 40-turn limit, and a live run had already spent all 40 turns on it. The precheck refuses a run whose limit is below the minimum number of writes, and warns when only the reference solution does not fit.

Three more budget facts are easy to miss. Retries multiply: eight outer attempts over a client that retries twice is up to 24 HTTP requests, so give retries one owner. Reservations are estimates: a lost response leaves a charge of unknown size, which should be kept as uncertain exposure instead of treated as free. And context must be bounded explicitly: replacing old tool results with short notices cut one simulated attempt from 454k to 315k estimated prompt tokens, with the full trace kept separately for audit.

A grade names the criterion that failed; it does not name the cause. Advisory observations read the trace and report what it shows: unfollowed_page, no_writes, write_without_read, refused_calls, and ended_with_question.

Two rules govern them. An observation never changes a grade, and a passing attempt can carry one. And you should report an observation’s reach: these generic observations explained 2 of 5 failed live attempts. The rest needed the meaning of the instruction, which a generic rule does not have.

To ask “what would the agent have done if…”, the harness can fork a record in three ways:

ForkWhat runsWhat it measures
Replay forkRecorded calls, under a different session policySensitivity to the policy. The model is not called
Live fork, edited callThe edited call runs for real, then the model continuesWhat the agent does after a different action
Live fork, edited resultThe original call runs, the model is shown an invented resultRobustness to an observation. Marked synthetic

Forks are bounded on depth, count, and budget, and they never count as attempts. Unlimited counterfactual search would select the branch that succeeded.

This harness was built with AI coding tools across many sessions, which creates its own evidence problem: decisions live in transcripts. Five practices handled it.

Keep a decision log with stable ids and a status. Each entry states the choice, the reason, an example, the limit, and the evidence. The status is one of built, partial, planned, deferred, or superseded, and when a choice changes, the id stays and the entry names its replacement.

Treat transcripts as searchable evidence. agentspec reads the session stores of several coding tools through one interface:

Terminal window
agentspec session search "judge" --project <name> --role user
agentspec session list --since 7d --format json
agentspec session export <source> <session-id>

--role user finds what was asked, as opposed to an assistant restating it.

Export what you will need. The tool reads each coding tool’s own store and keeps no history itself; retention belongs to each tool, and some prune after about a month.

Do not share a checkout between sessions. An early commit swept up a concurrent session’s change under an unrelated message. Use one worktree per session, stage files by name, and read the final diff before committing.

Disclose the assistance, and own the claims. Commands, source, fixtures, and saved records are the evidence, and the author remains responsible for verifying them.

The harness was built in this order:

  1. Inspect the data first. Count field shapes, absent against null, near-duplicate names, and target-set sizes on the large seed. Most design decisions came from this step.
  2. Validate the contract, then the seed. Reject a broken world before any model runs. Zero tokens spent on an invalid setup.
  3. Build the world and the session layer, with isolation tests.
  4. Make time and ids deterministic, then order the tools.
  5. Write the checker as pure functions over the diff and the trace.
  6. Write oracles and mutants. This is where the checker’s own bugs surfaced.
  7. Run live, and keep the failures. Early provider failures stay in their original records.
  8. Audit the first grades. The substituted-read defect was found here.
  9. Add replay verification, then tighten it to cover observations.
  10. Try a judge, measure it, remove it.
  11. Add revisions, budgets, and the feasibility check.
  12. Add diagnostics and a read-only monitor over the saved runs.

Measurement came before optimization at every step, and the harness was tested before its scores were believed.

TechniqueWhen to apply
Separate outcome, evidence, and gradeAny evaluation of something that executes
Required and allowed change setsAny agent that mutates state
Derived target setsAny criterion that must hold across data sizes
Trace criteria beside state criteriaAny task where order or coverage matters
Replay before gradingAny saved record that feeds a score
Oracles and mutantsAny grader, before its first score is reported
Logical clock and sequence-derived idsAny simulation that must replay
Revision pinningAny result that will be compared later
Feasibility precheckAny run that costs money
Attribute to agent, world, or task textEvery failure, before changing anything
Advisory diagnosticsExplaining failures without touching grades
Decision log with statusAny build that spans many sessions

The harness pattern, judge bias, and contamination are developed in LLM evaluation, and the Grounded SQL agent applies result-set scoring, bounded retry, and deterministic gates to a single query. A verified checker is also a reward function, which connects to cost per successful task, escalation, and reinforcement fine-tuning rewards in Model routing and cascades. What crash survival would require (journals, ownership, recovery) is the subject of Durable orchestration and workers, and committing evidence before acknowledging uses the idempotency and atomic claims of the Exactly-once event API. Testing the tests, with mutation testing and test doubles, is part of The testing mentality.

Where the problem shows up: for agent evaluation and simulation vendors, isolated environments with verifiable grading are the core product; AI infrastructure vendors need reward functions for reinforcement fine-tuning, which means a checker that cannot be gamed; any team shipping agents needs regression gates on tool-using behavior built on outcome, evidence, and grade; and any staff-level AI role expects you to state limits and separate claims from evidence.

  • Outcome, evidence, grade: how execution ended, whether its record is complete and consistent, and whether every criterion passed.
  • Session layer: a private copy-on-write view of a shared, immutable world, one per attempt.
  • Required and allowed sets: the changes a task must make and the changes it may make; the state criterion is R⊆Δ⊆AR \subseteq \Delta \subseteq A.
  • Replay verification: re-executing a record’s calls against a fresh session and comparing every observation and effect before grading.
  • Oracle and mutant: a correct scripted solution that must pass, and a deliberately wrong one that must fail on an expected criterion.
  • Revision: a hash of everything that determines a run’s result, pinned before the run.
  • Completed is not passed: keep outcome, evidence, and grade separate, and define the denominator in advance.
  • Private sessions, a logical clock, and sequence-derived ids make attempts isolated and replayable; the provider’s nondeterminism is sampled, never the checker’s.
  • The checker grades the set relation R⊆Δ⊆AR \subseteq \Delta \subseteq A plus trace criteria, only on records that replay.
  • Oracles and mutants test the checker; 13 oracles and 60 mutants covered 69 criteria.
  • Live failures were almost all the agent’s and one was the task’s; attribute before fixing, budget before spending, and keep model judges out of the grade.
  1. Why must outcome, evidence, and grade be reported separately?
    AnswerA run can complete cleanly after editing the wrong row, and a missing or inconsistent record says nothing about the agent. Collapsing the three makes a model mistake indistinguishable from a dead worker.
  2. What does each half of R⊆Δ⊆AR \subseteq \Delta \subseteq A catch?
    Answer$R \subseteq \Delta$ fails when required work is missing; $\Delta \subseteq A$ fails when unrequested or unauthorized work was done. An attempt that edits the wrong warehouse fails both, and they are reported separately.
  3. What defect made replay verification necessary, and what does replay not prove?
    AnswerAn early verifier did not compare recorded read results, and substituting one turned a failing attempt into a passing one. Replay proves a record is self-consistent, not that it is authentic.
  4. Why was the model judge removed?
    AnswerIts judgments disagreed across samples, one sample in five approved an attempt with zero writes, and it read untrusted text written by the agent it was judging. Prose criteria now stay `not_reviewed` until a person reviews them.
  5. Why is the cheaper model in the cost-gate example the more expensive result?
    AnswerCost must be divided by accepted tasks: \$10 for 100 accepted tasks is \$0.10 each, while \$6 for 40 accepted tasks is \$0.15 each.