Topic 4 of 5 · 4 min read
Reproducing non-deterministic agent failures
Goal: Set up a replay of a failed agent run with recorded tool outputs and decide how many reruns show the fix works.
An agent failure that happened once may not happen again. To debug it, record what the tools returned so you can replay the run, and rerun a failing case several times before you believe a fix.
Check yourself · 1 of 3
Why replay a failed agent run with recorded tool results?
Why it matters
Model output varies from run to run, even with the same input. Tools add more variation: a search returns different pages today, an API is down, a clock moves. A bug that shows up in 1 run in 5 can look fixed after one lucky rerun. Without a way to repeat the failure on demand, you are guessing.
How to do it
- Record every tool call. Save the tool name, arguments and exact result for each step, with the run id. Also save the system prompt, the model name and its settings, and the user input.
- Replay with recorded results. Run the agent again, but have a stub answer each tool call with the saved result. Now the outside world is fixed. Only the model can vary. If the failure appears again, you have a reproduction that does not depend on live services.
- Fix the clock and the randomness. Pass in a fixed date and a fixed seed where your stack supports it. Some model APIs accept a seed but do not promise identical output, so treat it as a help, not a guarantee.
- Measure the failure rate. Run the replay many times, say 10 or 20, and count failures. "Failed 6 of 10" is a baseline you can compare to.
- Test the fix the same way. After the change, run the same number of replays. If the rate drops from 6 of 10 to 0 of 10, that is a strong sign. 0 of 3 is not: a task that fails 1 time in 3 passes three times in a row about 30 percent of the time.
- Keep a live check too. A replay with stubbed tools cannot catch a bug in the real tool. Run the fixed agent once against live tools to confirm.
If a change affects what the tool is called with, the recorded results no longer match. Then the replay needs a fresh recording, or a stub that answers by arguments.
Before and after
Weak: "I reran it once and it passed, so the fix works."
Better: "The failing case failed 7 of 10 replays. After the change it passed 20 of 20 replays and one live run."
What goes wrong
- Replaying live tools. The failure disappears because the data changed, and you think you fixed something.
- Rerunning too few times. A flaky failure hides in small samples.
- Stubbing too much. If you replace the model's inputs with a simplified version, the bug may no longer exist.
Examples
A replay harness
import json
def replay(run_file, agent):
run = json.load(open(run_file))
recorded = {c["key"]: c["result"] for c in run["tool_calls"]}
def fake_tool(name, args):
key = name + json.dumps(args, sort_keys=True)
return recorded[key] # KeyError means the agent changed its call
return agent.run(run["input"], tools=fake_tool, now=run["started_at"])
A KeyError tells you the new agent made a different call than the recorded one, which is itself useful evidence.
How many reruns?
A fixed agent passes 5 of 5 replays. If the old failure rate was 1 in 5, a run with no fix would still pass 5 of 5 about a third of the time (0.8 to the power 5 is roughly 0.33). Twenty passes in a row would happen by chance about 1 time in 100. Pick the number of reruns by how common the failure was.
Key points
- Record the prompt, model settings, user input and each tool call's arguments and result for every run.
- Replaying recorded tool results removes outside variation, so only model behaviour can change.
- Seeds and temperature zero reduce variation but do not guarantee identical model output.
- Measure a failure rate over many reruns, and test a fix with the same number of reruns.
- A few passing reruns prove little when the failure was intermittent, so also confirm once against live tools.
Common mistakes
- Calling a fix successful after one passing rerun of an intermittent failure.
- Replaying against live tools whose results change between runs.
- Recording only the final answer and not the tool results needed to replay.
- Assuming a fixed seed makes a model reproduce the same output exactly.
Check yourself · 2 of 3
A flaky agent failed 3 of 10 replays. After a prompt change it passed 3 of 3. What should you do?
Check yourself · 3 of 3
Which data must you save from each run so a failure can be replayed later?
Practice items, not counted towards your mastery.
Start here
- OpenTelemetry semantic conventions for generative AI (opens in a new tab)StandardFree
The standard names for model-call and tool-call spans and their attributes, so traces look the same across tools.
- Langfuse observability overview (opens in a new tab)ToolFree
Shows how traces, nested observations and sessions map onto an agent run, in an open-source tool.
- Tracing (OpenAI Agents SDK docs) (opens in a new tab)DocsFree
Lists the span types an agent framework records: agent, generation, function, handoff and guardrail.
More for this topic
- Demystifying evals for AI agents (Anthropic) (opens in a new tab)ArticleFree
Explains tasks, graders and transcripts, and why reading transcripts is part of running evals.