Save the Failing Run
Last Christmas I built a coding agent from scratch to read a spec, plan repository edits, run tests, and recover when checks failed.
Single tool calls were straightforward. Multi-step recovery loops were where things broke.
Old command output bloated the prompt until the plan drifted and failed edits cycled back around. Writing up those lessons in What I Learned Building an AI Agent From Scratch shifted my focus away from prompt tweaks toward the deterministic runtime wrapping the model: what state it persists and which actions it refuses to execute twice.
Most of all, flaky multi-turn runs made me want a reliable way to capture and replay a failure.
An agent calls a tool to create a customer record. The downstream service commits the row, then drops the network connection before returning 200 OK. Seeing a timeout exception, the agent loop retries the tool call.
Count the rows in the database afterward.
Exactly one record should exist. Two rows mean the runtime duplicated a business mutation, even if an eval harness grading only the assistant's final "Done!" message marks the run green.


Preventing that duplicate write requires an idempotency key generated by the runtime before the first attempt and preserved across retries, paired with a tool endpoint that deduplicates against that key. Generating a fresh UUID inside each retry attempt defeats idempotency completely.
A seeded fault-injection test exercises that drop-after-commit path directly in Python:
def test_create_customer_idempotent_on_dropped_ack(self) -> None:
sim, db = FaultSimulator(seed=42), InMemoryCustomerDB()
sim.drop_response_after_commit(call=1)
idem_key = "op-create-cust-001"
try:
# First call commits the row, then drops the 200 OK response
create_customer(db, sim, email="[email protected]", idempotency_key=idem_key)
except TimeoutError:
create_customer(db, sim, email="[email protected]", idempotency_key=idem_key)
self.assertEqual(db.count_customers(), 1)
Asserting final row count in a state store requires zero LLM-as-a-judge heuristics.
Reproducing the exact multi-turn interleaving that triggered a bug is the harder systems problem. FoundationDB's deterministic simulation testing runs an entire distributed database cluster inside a single-threaded virtual clock and fault injector so subtle network partitions and crash recoveries replay identically from one seed.
Agent runtimes benefit from the same seam. Put wall-clock time, HTTP tools, concurrency, and random seeds behind injectable interfaces, and treat every LLM inference call as a recorded external RPC. Saving exact model request-response pairs lets CI replay runtime state transitions without paying live token costs or fighting sampling variance.
When a fault-injection test alters an upstream tool error, the resulting prompt diverges from the recorded cassette. Silently returning a canned completion recorded for a different prompt tests fiction, so the replay harness must fail fast on prompt-hash mismatch.
Scripted responses test deterministic runtime invariants like idempotency preservation and context-budget caps, while periodic live-model runs discover fresh failure trajectories worth freezing into new regression fixtures. Rather than boiling the ocean with a full simulator on day one, start with one state-mutating workflow, drop the network response right after commit, and verify row count. Which runtime failure mode has been hardest to reproduce deterministically in your agent tests?
Continue in AI Architecture & Agents
Before You Show Me the Token Chart
I am glad to fund engineers exploring what AI tools can do, knowing upfront that plenty of those experiments will hit dead ends. Demanding a guaranteed ROI…
I Won't Budget an AI Multiplier
I want engineers to use AI aggressively. I also have to decide what an engineering organization can promise on a roadmap. Those two commitments lead to very…