Debug a flaky agent run end to end
For engineers
Traces, logs, the agent loop and framework internals behind a flaky run.
A flaky agent run is debugged like any other bug, with one addition: the model's behaviour is a symptom of what the harness, the tools and the framework showed it, so you look there first.
The case
Harbor Desk sells parcel lockers. Its support agent, Dispatch, answers "where is my order?" tickets. It runs on a Claude model through an agent SDK, with one main tool, search_orders. On 1 December 2026 the dashboard shows 120 runs in 24 hours. 111 answered in a median of 14 seconds. 9 ran to the 25-turn limit and took over four minutes each. Support learned about it from a customer. Nobody changed the model.
Six skills apply, and the order in which you use them matters.
Step 1: reproduce before you theorise (eng-debugging)
"Dispatch loops sometimes" cannot be debugged. One looping run, saved with its ticket text, tool responses and config, can. Replace the customer's email with a placeholder, replay the run with the tools stubbed from the recording, and check that it fails every time. Then write it as a test. A fix that cannot make this test pass is not a fix, and a pass from rerunning a flaky run proves nothing.
Step 2: read the trace (llmops-tracing)
A trace is a tree of spans: the request, the agent run, one span per model call and one per tool call. The looping run shows 12 model calls of about 1.8 seconds each and 11 search_orders calls of 2.0 seconds each, all ending in a timeout error with the same argument. The model is not slow. A tool failed and the run repeated it. Record the model, token counts, finish reason, prompt version and a session id on the spans, and keep message content opt-in, masked and short-lived, because tickets hold personal data.
Step 3: join the logs (eng-observability)
The traces say what, and the logs say why. Structured JSON logs with a request id on every line, passed to the order service too, let you filter a whole run across services. Log names, ids, durations and error codes, not raw arguments or results. If you cannot join the lines, fix that gap now. An alert such as "more than 5% of runs hit the turn limit in 30 minutes" would have told the team before the customer did.
Step 4: name the loop failure (agent-loop)
The log tail shows the cause. The order service timed out, and the tool returned {"orders": []} with a small note. The model read an empty list as "no orders" and tried again, with the same input and the same result. The tool contract made failure look like an answer. Fix the contract: return an explicit, retryable error with an instruction. Then add the guards the loop lacked: stop after three identical calls, a token budget above a normal run, and a handoff to a person when any stop fires. A bigger turn limit would only make each bad run longer.
Step 5: look inside the framework (agent-frameworks)
A second problem may sit under the first. The SDK trims history to save tokens. If it trims the note about the timeout, the model sees an empty list only. Capture the real HTTP request for a late turn and read the messages. Then decide: configure the SDK, or move this one workflow to a short plain loop, where retries, trimming and logging are visible in one file. The answer depends on whether the framework's other features, such as tracing and handoffs, are worth the lost visibility.
Step 6: direct the assistant (code-debugging)
A coding assistant can help, if you give it the redacted trace, the replay and the SDK version, and one hypothesis: failure looks like success. Ask for the cheapest probe first. Refuse a patch that changes four things at once. If it offers a fourth variant of a failed idea, take back control, write down what you know and start a fresh session.
What good looks like
- One recorded failing run that is now a test.
- Traces that show where the time and the errors are.
- Logs that join by request id and hold no raw customer data.
- Tools that report failure as failure.
- Stops, budgets and a human handoff in the loop.
- A captured request that proves what the model saw.
Common traps
Raising limits instead of fixing the cause. Trusting a model's own story of why it looped. Capturing full prompts for every run. Treating a fall in the loop rate after a cap as a root-cause fix. Each of these makes the numbers better and leaves the bug in place.
Steps
Capture one failing run and make it replayable
Pick one run that hit the turn limit. Save the ticket text, the tool responses and the config, with the customer email replaced by a placeholder. Replay it with the tools stubbed from the recording, and confirm it fails every time. Turn it into a test that fails for the reported reason: the timeout.
Read the trace before forming a theory
Open the trace of that run as a tree. Look at the span durations and errors, not at the final answer. Eleven identical search_orders spans, each ending in a timeout error, show where the run spent its 4 minutes and what repeated.
What the span tree shows Goal: Run T-5521
- agent.run 4m10s, finish max_turns
- chat 1.8 s x 12 (normal)
- search_orders 2.0 s error=timeout x 11 (same argument hash)
Join the logs by request id
Filter the structured logs to the request id of the run. If the order service logs the same id, the first error and its time are visible across both services. If lines lack the id, that is an instrumentation gap to fix next, not to work around.
Name the loop failure and fix the tool contract
The tool returned a timeout as an empty list, so the model read it as 'no orders' and tried again. Return an explicit retryable error with an instruction, add a stop on the same call with the same arguments repeated three times, add a token budget well above a normal run, and hand off to a person when a stop fires.
Inspect what the framework really sent
Capture the HTTP request for a late turn and read the messages. If the SDK trimmed history and dropped the timeout note, the model could not have known. Decide whether to configure the SDK, patch the trimming, or move this one workflow to a plain loop.
Direct the assistant with one hypothesis and real evidence
Give the assistant the redacted trace, the replay and the SDK version, and state one hypothesis: failure looks like success. Ask for the cheapest probe, then the fix, and require the replay to pass. If it offers a fourth variant of a failed idea, stop and restart with the facts.
Skills this scenario uses
Where to study
- Building effective agents (Anthropic) (opens in a new tab)ArticleFree
Explains the agent loop, tool design, stopping conditions and why simple, inspectable code often beats a framework.
- OpenTelemetry semantic conventions for generative AI (opens in a new tab)StandardFree
The standard attribute names for model and agent spans, including opt-in content capture.
- Traces (OpenTelemetry) (opens in a new tab)DocsFree
What spans, parent and child relationships and context propagation are.
- Tool use overview (Claude docs) (opens in a new tab)DocsFree
How tool calls and tool results flow through the messages a model receives.
- Monitoring Distributed Systems (Google SRE Book) (opens in a new tab)BookFree
The four golden signals and how to write alerts that are actionable and not noisy.