Hand mocks pass agent workflow tests that the real service would fail. Run one contract suite against a verified fake and the sandbox, and catch drift nightly.
Why agent workflow tests drift from reality
An agent-integrated workflow has two layers. The model proposes actions: create a return, issue a refund, email the customer. Deterministic code turns each proposal into calls on existing services, with the right keys, retries and validation. The second layer is where most production failures live, and it is the layer you can test without a model, by scripting the proposals.
The usual test setup replaces each downstream service with a mock that returns a canned success. The tests pass, the workflow ships, and the first real failure is an error code the mock never produced: a rejected idempotency key, a validation rule, a rate limit. The mock encoded the developer's belief about the service, not the service. Agents make this worse in a specific way. A model explores combinations of calls that no developer scripted, so the paths that only the real service would reject are exactly the paths an agent will find.
Google's *Software Engineering at Google* names the property that matters: fidelity, how closely a test double behaves like the real implementation. Its chapter on test doubles recommends fakes, lightweight working implementations, over mocks for services with real behaviour. It also says that a fake must have its own tests, run against both the fake and the real implementation, which it calls contract tests. Martin Fowler's note on contract tests makes the same point from the consumer's side: tests against a double need a separate check that the double still matches the service.
This guide applies that discipline to agent workflows, using a returns API at Belgavi's Book Store. The companion example verified_service_fakes contains every component, simulated in process so it runs anywhere.
The pieces
- The real service's sandbox. It enforces the real rules: an idempotency key is required, a reused key with a different body returns 409, ISBNs must have 13 digits, returns are accepted within 30 days unless the book arrived damaged, and, in this simulation, the sixth call to one instance gets 429 with
Retry-After. - A verified fake. An in-memory implementation of the same rules, owned by the team that owns the service, with no network and no persistence. It is "verified" only while the contract suite passes against both it and the sandbox.
- A hand mock. It returns 201 with a fixed return id for anything.
- One contract suite. Ten checks, each a short sequence of requests with the expected status codes. The same suite runs against every implementation.
Worked example: the suite separates the doubles
| Implementation | Contracts passed (of 10) | What fails |
|---|---|---|
| Hand mock | 4 | idempotency key required, reused key with a different body, invalid ISBN, 30-day window, 404 for an unknown return, rate limit |
| Verified fake | 10 | nothing |
| Sandbox, version 1 | 10 | nothing |
| Sandbox, version 2 | 9 | a request without a reason code is now rejected |
The hand mock passes only the happy paths. The fake passes everything the version 1 sandbox passes, which is the definition of verified.
Now the workflow. The deterministic layer under the agent executes the agent's plan for help-desk case CASE-4102. The plan creates a return with idempotency key case-4102-return. After a timeout, the agent corrects the ISBN and the code retries, but it builds a fresh body and reuses the key. Against the hand mock both calls return 201. Against the verified fake the second call returns 409, idempotency_key_reused_with_different_body, which is what the real service would do. The bug is visible in a unit test that runs in milliseconds, with no model and no network.
def agent_refund_workflow(svc, plan):
out = []
for step in plan:
status, headers, body = svc.handle("POST", "/returns", {"Idempotency-Key": step["key"]}, step["body"])
if status == 429:
out.append(("backoff", headers.get("Retry-After")))
continue
out.append((status, body.get("return_id") or body.get("error")))
return outThe fix belongs in this layer, not in the prompt. Either the corrected request gets a new key, because it is a new intent, or the code refuses to change the body under an existing key and asks for a decision.
Drift: run the suite against the sandbox on a schedule
Services change. In version 2 of the sandbox a reason code becomes required. The fake still accepts requests without one, and every workflow test that uses the fake still passes. Nothing in the agent's repository has changed, so nothing in its CI notices.
The contract suite notices, as long as it runs against the real sandbox on a schedule, for example nightly in the service team's pipeline. In the example, running the suite against version 2 shows exactly one difference from the fake: accepts a request without a reason code. That single line is the work item: update the fake to require the reason, and then fix the workflow code that omits it. Because the service team owns both the fake and the suite, the fake moves with the service instead of with each consumer's guesses.
What stays deterministic
- The contract suite and its expected codes. They are plain data and plain assertions, with no model involved.
- The tool-call layer under the agent: keys, retries on 429 using
Retry-After, mapping errors to structured results the agent loop can read. - The fake itself. It must be deterministic, including its rate limiter and its id generation, or tests become flaky.
- The scripted agent plans used in tests. They are recorded proposals, not live model calls, so a failing test always means the code or the fake changed.
The model's behaviour is tested separately, with evaluation sets that score whether its proposals are sensible. Mixing the two in one test makes every failure ambiguous.
Migration steps for existing mocks
- Inventory the services your agent's tools call, and for each one, list the error responses your workflow code handles today. A short list usually means the mocks have hidden the rest.
- Ask the owning team for a fake, or write one with them. Start with the behaviours that change control flow: idempotency, validation, limits, not-found and conflict.
- Write the contract suite as data: request sequences and expected statuses. Run it against the fake in the owning team's CI and against the sandbox on a schedule.
- Replace hand mocks in agent workflow tests with the fake, one service at a time. Expect previously green tests to fail; each failure is a real bug or a missing behaviour in the fake.
- Add recorded agent plans as test fixtures, including the strange ones from production traces: retries with changed bodies, out-of-order steps, repeated calls.
- Treat a contract failure against the sandbox as a release blocker for the fake, not as a flaky test.
Failure modes
- A fake that only the consumer maintains. It drifts toward what the consumer expects. Ownership by the service team is what keeps fidelity.
- Contracts that test only status codes. Response bodies matter too. If the workflow reads
return_id, the contract should assert its shape. - Sandbox state that leaks between checks. Each contract in the example builds a fresh service. A shared sandbox needs unique keys per run or a reset endpoint.
- Rate limits in the suite itself. Running all checks against a shared sandbox can trip its limiter and turn into false failures. Space the checks or use a dedicated quota.
- Mocking the fake. Once a fake exists, adding a mocking layer on top of it to "speed things up" reintroduces exactly the drift you removed.
When an agent is unnecessary
None of this depends on an agent. A deterministic job that creates returns from a spreadsheet needs the same fake and the same suite. The agent only raises the stakes, because it generates unusual call sequences more often than scripted code does. If the workflow has a fixed shape, such as one return per case with a known reason, write it as ordinary code against the fake and skip the model entirely. Bring in an agent for the parts that need judgement, such as reading the customer's message to decide whether the book arrived damaged, and keep the calls themselves in the tested layer.
Verification
The companion example's tests assert the table above, that the fake and the version 1 sandbox give identical results on every contract, that the reused-key bug is invisible with the mock and visible with the fake, and that running the suite against version 2 reports exactly one drifted contract. For your own services, the check is simple: run the same suite against your fake and your sandbox and compare the two result lists. Any difference is either a bug in the fake or a change in the service that your workflow has not been tested against yet.