Debugging Late, Far and Rare Failures in a Claude Agent: Replay the Turns, Follow the Fact, Split the Runs
A chat goes wrong at turn six, a refund id vanishes between two agents, and one ticket fails three runs in ten. Replay the turns to find that a trim dropped the customer's correction at turn 5, follow R-12 to the handoff that cut it, and split ten runs to find 3 of 3 failures when the archived document came first.
What you will learn
- Find the first bad turn of a chat by replaying what the model actually saw against the full history
- Follow one fact hop by hop across agents and name the hop that lost it
- Run a flaky ticket many times and split the failures by one variable
- Place the cause in the model, the harness, the tools or the data before changing anything
Before you start
- Claude tool use and multi-turn messages
- Passing context to subagents
Three complaints reach SupportDesk in one week. A chat goes wrong at turn six. A refund id vanishes between two agents. And one ticket fails three runs out of ten.
All three get sent to the same place: blame the model. Try a bigger model, a longer prompt, run it again, add retries. The cause stays exactly where it was, and only the symptom moves. Each one looks like a model problem. None of them has to be.
The model can only use what the harness hands it. Lose something on the way, and no model can get it back. So before you change anything, find where the failure starts.
A rattle in the car
A good mechanic asks three questions before touching the engine:
| Question | Failure type | Move |
|---|---|---|
| When did it start? | Multi-turn | Replay the chat turn by turn; find the first bad turn |
| Which part makes it? | Multi-agent | Follow the fact across agents; find the hop that lost it |
| How often does it happen? | Non-deterministic | Repeat the run; split the results by one variable |
When, where and how often. The project runs on a stand-in model (the Anthropic SDK on an httpx mock transport, with scripted replies that follow simple rules about the messages they get), so no API key is needed and the replies are test replies.
Multi-turn: the chat that goes wrong late
The customer asks to ship order A-77 to Leeds, then changes it to Bristol, then chats about a gift note, the invoice and the box. The harness trims long chats to save tokens: it keeps turn 1 and the last three. An excerpt from multi_turn.py:
PROBE = "Where will A-77 ship?"
KEEP_FIRST, KEEP_LAST = 1, 3 # the harness trims long chats to save tokens
def trim(indexes):
"""which turns the model gets to see"""
if len(indexes) <= KEEP_FIRST + KEEP_LAST:
return indexes
return indexes[:KEEP_FIRST] + indexes[-KEEP_LAST:]
def replay(client):
"""after each turn: what the model saw, against what the full history would give"""
rows, first_bad = [], None
for n in range(1, len(TURNS) + 1):
full = list(range(1, n + 1))
seen = trim(full)
got, want = ask(client, seen), ask(client, full)
rows.append((n, seen, got, want))
if first_bad is None and got != want:
first_bad = (n, sorted(set(full) - set(seen)))
return rows, first_bad
After every turn, the replay asks the same probe question twice: once with exactly the turns the model would see, and once with the full history. The first turn where the two answers differ is the first bad turn.
multi-turn: ask the same question after each turn
turn 1 saw [1] Leeds full Leeds ok
turn 2 saw [1, 2] Bristol full Bristol ok
turn 3 saw [1, 2, 3] Bristol full Bristol ok
turn 4 saw [1, 2, 3, 4] Bristol full Bristol ok
turn 5 saw [1, 3, 4, 5] Leeds full Bristol FAIL
first bad turn: 5, the trim dropped turn 2
The customer complained at turn six, but the cause was already there at turn five. And the model did nothing wrong: from turns 1, 3, 4 and 5, Leeds is the right answer. Reconstruct what the model actually saw, not what the log says it was told.
Multi-agent: the fact that vanishes on a hop
A researcher reads the order row and finds a refund, R-12. Then it hands a summary to the coordinator, which answers the customer. The handoff is the harness's own code, and it keeps the handoff short. An excerpt from multi_agent.py:
ORDER_ROW = "order A-77: shipped Monday; refund R-12 issued"
FACT = "R-12"
def short_handoff(summary):
"""the harness keeps the handoff short: first sentence only"""
return summary.split(". ")[0] + "."
def trail(client, handoff):
"""every hop the fact travels, in order"""
summary = ask(client, RESEARCHER, f"Check this: {ORDER_ROW}")
passed = handoff(summary)
final = ask(client, COORDINATOR, f"Research notes: {passed}\nAnswer the customer.")
return [("order data", ORDER_ROW), ("researcher summary", summary),
("handoff to coordinator", passed), ("final answer", final)]
def first_missing(hops):
"""the first hop that no longer carries the fact"""
for name, text in hops:
if FACT not in text:
return name
return None
multi-agent: follow the fact R-12 hop by hop
short handoff ok ok -- -- | lost at: handoff to coordinator
full handoff ok ok ok ok | lost at: nowhere
With the short handoff only the shipping sentence gets through, so the coordinator tells the customer no refund has been issued yet. That's a correct answer from the notes it received. Pass the full summary and the fact survives all four hops. For how to build a handoff that carries what the next agent needs, see passing context to subagents.
Non-deterministic: the ticket that fails sometimes
The help-centre search returns both returns policies, the archived one (30 days) and the current one (14 days), in a changing order. Same question, same documents; only the order differs, and that's the one variable we watch. An excerpt from flaky.py:
def retrieve(seed):
"""the help-centre search returns both documents, in a changing order"""
order = ["old", "new"]
random.Random(seed).shuffle(order)
return order
def repeat(client, runs=10):
return [one_run(client, seed) for seed in range(1, runs + 1)]
def split_by_first_doc(results):
"""failures counted per value of one variable: which document came first"""
out = {}
for first in ("old", "new"):
group = [r for r in results if r["first"] == first]
out[first] = (sum(not r["ok"] for r in group), len(group))
return out
non-deterministic: the same ticket ten times
run 1 first doc new 14 days ok
run 2 first doc new 14 days ok
run 3 first doc new 14 days ok
run 4 first doc new 14 days ok
run 5 first doc old 30 days FAIL
run 6 first doc new 14 days ok
run 7 first doc old 30 days FAIL
run 8 first doc new 14 days ok
run 9 first doc old 30 days FAIL
run 10 first doc new 14 days ok
old doc first: 3 of 3 failed | new doc first: 0 of 7 failed
Three out of ten failed, so one run would have proved nothing. Split by the one variable and the pattern is plain: the old document first, 3 of 3 failed; the new one first, 0 of 7. The failures follow the document order, not luck, and the split points at the data: the archived policy that the search still returns.
About the stand-in. The scripted model follows the first document it's given, so the pattern is easy to see. That's a rule of the demo, not a claim about how Claude behaves. Runs against a live model are messier, but the method is the same: repeat, keep every result, split by one variable at a time.
Common mistakes
- Switching to a bigger model. It can't recover a turn the trim dropped or a fact the handoff cut.
- Rerunning once and calling it fixed. At 3 in 10, a single green run is likely.
- Rewriting the prompt when the fact never reached the model.
- Reading the log instead of the request. Rebuild the exact messages the model received.
- Changing two things at once. Then the split can't tell you which one mattered.
Quick recap
| Failure | Move | Found in the run | Cause sat in |
|---|---|---|---|
| Multi-turn | Replay; first bad turn | Turn 5, the trim dropped turn 2 | Harness |
| Multi-agent | Follow the fact hop by hop | Lost at the handoff to the coordinator | Harness |
| Non-deterministic | Repeat, split by one variable | Old doc first: 3 of 3 failed; new first: 0 of 7 | Data |
Once you know which layer owns the failure, fix that layer. If the failure reached customers, it also earns a regression case; see from incident to regression eval.
Try it yourself
Change KEEP_LAST in multi_turn.py from 3 to 4 and run the replay again. Does any turn fail now? Then think of one place in your own agent where a harness rule trims, summarises or truncates, and write the probe question that would catch it.
Related course on Udemy
Claude Certified Developer - Professional (CCDV-P) Exam Prep
Claude agents in production: reliability, observability, evals, cost, guardrails and fleets.View on Udemy ↗: Claude Certified Developer - Professional (CCDV-P) Exam Prep (paid course, opens in a new tab)Related posts
The Claude Messages API: Your First Call, Field by Field
One request in, one Message out. The four fields of a Messages API call (model, max_tokens, system, messages), how to read content blocks, stop_reason and usage, and why every turn resends the history.
Claude Tool Use: How tool_use and tool_result Work in the Messages API
Follow one order question through Claude's tool round trip: define a tool, read the tool_use block, run your own function and send a matching tool_result back, including parallel calls and is_error.
Claude Client SDKs Wrap One REST API: Raw HTTP, Python, TypeScript and Async Calls Compared
Every Claude client SDK wraps the same REST request: a POST to /v1/messages with a JSON body. See it built by hand, through the Python and TypeScript SDKs, as typed errors, and as async calls that wait together.
Anthropic SDK Timeouts, Retries and Error Classes: Configure the Client Once, Then Give 429, 529, 400 and Timeouts Their Own Move
Configure the Anthropic SDK client (timeout, max_retries, with_options, async, pagination), see which errors it retries by itself, then repair a retry wrapper so a 429, a 529, a 400 and a timeout each get the move the docs support.
