How Do You Trace and Debug an Agent Run?
How to record each step of an AI agent run, find the first step that went wrong, and turn the failure into a test, explained simply for your AI interview.
In short
Record every step of an agent run as a trace: each model call, tool call, input, output and error, in order. When a run goes wrong, open the trace, find the first step that went wrong, fix its cause, and save the run as a test.
11 min readIntermediateCommon · estimateFacts checked 26 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
I record every step of an agent run: each model call, each tool call with its inputs and outputs, and each error, in time order. When a run goes wrong, I open the trace and start at the first step, because the final answer only shows the symptom. Later steps build on that first mistake. I fix its cause, then save the run as a test, so the same bug can't come back unnoticed.
An agent run is a loop, not a single reply. The model decides, calls a tool, reads the result, and decides again. A run can have many steps, and the final sentence can look fine while an early step has already gone wrong. So I record every step as it happens: what the model saw, what it chose, the tool name and arguments, what came back, any error and how long each step took. Those steps, saved together under one run ID, form the trace.
When a run fails, I walk the trace in order and look for the first step where something went wrong. It might be a tool that returned stale data, or a check that was skipped. Later mistakes often grow from that first one, so fixing it often fixes the rest. Anthropic makes a related point in its evaluation guide: a transcript shows whether the agent made a real mistake or whether the grader, the check that marks answers right or wrong, rejected a valid answer. I also check the outcome in the real system, because an agent can say it did something it did not do.
Then I turn the failure into a test. The failing run goes into the test set with its inputs and the correct result, so every later change is checked against it. Reading transcripts is also how I check that the tests measure what matters, and that is a skill I use all the time.
Recording everything has a cost. Traces hold customer data, so I remove or hide personal details, limit who can open them, and set how long they are kept. Tools such as LangSmith, the OpenAI Agents SDK and Microsoft Foundry let you inspect traces in a dashboard or portal. What matters most is the habit: record each step, read down to the first failure, and keep the case.
What is a trace, in plain words?
An agent run is one request that makes the agent take several steps. It calls a model, calls a tool, reads what came back, and calls the model again. A trace is the saved record of every step in that run.
Think of a hospital chart for one patient visit. It lists each test, each result and each decision in time order, so a doctor can see where things went wrong. A trace does that job for an agent.
Tools use the words differently. Anthropic says a transcript, also called a trace or trajectory, is the full record of a trial. LangSmith keeps a trace as the runs of one operation. The idea is the same: a record of each step.
Key words
- Trace
- The saved record of every step in one agent run, in time order.
- Tool call
- The model asking your code to run a function, such as lookup_policy. Your code does the running.
- Transcript
- The full record of a run or test trial, including outputs and reasoning.
- Regression case
- A saved failing run that a test checks after every change, so the bug stays fixed.
What do you record at each step?
Record enough to replay a step without guessing. For each step I keep these facts:
What the model saw
The full input sent to the model, including its instructions and their version.
What it chose
The model's reply, and any tool it asked for.
The tool call
The tool's name and its arguments, exactly as they were sent.
What came back
The tool's result or error, not a trimmed summary.
The bookkeeping
A run ID, the time taken, the token counts (the pieces of text the model reads and writes, which set the cost) and the model version.
What one trace records
Customer's claim
a claim email arrives
The agent's run
Model call
decides to look up the policy
Tool: lookup_policy
args and result are saved
Model call again
reads the result, decides next
Trace record
every step, in time order
You read it
find the first wrong step
- 1The claim goes to the model, which starts the run.
- 2The model asks for a tool, and the tool runs.
- 3The tool's answer goes back to the model, which decides again.
- 4Every step is saved into one trace record.
- 5You open the record and check each step in order.
Much of this comes built in. The OpenAI Agents SDK traces each run by default, and the Claude Agent SDK has hooks that log and audit tool calls.
“Traces address these challenges by allowing you to view the inputs and outputs of each primitive involved in a particular agent run, displayed in the order they were invoked, making it easy to understand and debug your AI agent's behavior.”
Microsoft Foundry
How do you find the first wrong step?
Start at the first step and read down. The final message is only the symptom. Here is a case from a claims triage agent at a mid-size insurer. It reads a customer's email, looks up the policy, and sends the claim to the right queue.
One customer claimed £1,200 for a cracked windscreen. The agent sent the claim to a manager, saying it was over the £800 cover limit. The policy had been renewed on 1 March 2026, and the new cover was £1,500. The agent was wrong because its data was out of date.
The trace showed the cause in two steps. Step 2, the lookup tool, returned the policy that had been replaced on 1 March 2026. Step 3, the model, then reasoned correctly from the wrong number. So the bug was in the tool.
The script checks the one rule we already know about, so it can point at the step. In real life you read each step yourself.
# One recorded run of a claims triage agent (made-up numbers), read back the way a debugger reads a trace.
CLAIM_DATE = "2026-11-02"
run = [
{"step": 1, "kind": "model", "says": "Look up the policy for POL-7731."},
{"step": 2, "kind": "tool", "tool": "lookup_policy", "args": "POL-7731",
"result": {"cover_limit": 800, "superseded_on": "2026-03-01"}},
{"step": 3, "kind": "model", "says": "The claim is over the limit of 800, so refer it."},
{"step": 4, "kind": "tool", "tool": "send_to_queue", "args": "REFER", "result": {"ok": True}},
]
def first_wrong_step(steps):
for s in steps:
if s["kind"] == "tool" and s["tool"] == "lookup_policy":
old = s["result"]["superseded_on"]
if old and old <= CLAIM_DATE:
return s
return None
bad = first_wrong_step(run)
after = next(s for s in run if s["step"] == bad["step"] + 1)
print("First wrong step:", bad["step"], bad["tool"], bad["args"])
print("Tool said the policy was replaced on", bad["result"]["superseded_on"])
print("Model's next step:", after["says"])Output
First wrong step: 2 lookup_policy POL-7731 Tool said the policy was replaced on 2026-03-01 Model's next step: The claim is over the limit of 800, so refer it.
The fix belongs in the tool: it must return the policy that was in force on the claim date. I changed the lookup, then saved this run as a regression case. A prompt change would have hidden the bug for one customer and left the next claim wrong.
Finding the bug, step by step
Bad answer reported
a wrong decision on a claim
Walk the trace in order
Check each tool result
old or wrong data often starts here
Check each model choice
right data, wrong call? check here
Fix the first wrong step
fix the cause, not the symptom
Save as a test case
the same run must pass next time
- 1Open the run that gave the bad answer.
- 2Walk down: first the tool results, then the model's choices.
- 3Once the first wrong step is found, fix its cause.
- 4Keep the run as a test, so the bug can't return.
“When a task fails, the transcript tells you whether the agent made a genuine mistake or whether your graders rejected a valid solution.”
Anthropic
How do you check what really happened?
An agent can describe a success that never happened. Anthropic's evaluation guide gives the case of a flight-booking agent: its last message can say the flight is booked, but the real test is whether a reservation exists in the database. So I compare what the trace claims with the system's own record.
- 1
Read the claim
Note what the agent says it did, such as referring a claim to a manager.
- 2
Check the record
Look in the queue, the database or the email log for proof that it happened.
- 3
A mismatch is a bug
If the two differ, the trace is where you find out why.
Tip
Don't grade a run on its last message alone. Grade it on what changed in the system.
What does the interviewer want to hear?
A strong answer covers
- A plain definition: a trace is the saved record of every step in a run.
- What you log per step: inputs, outputs, tool calls, results, errors and ids.
- Find the first wrong step and fix its cause.
- Check the outcome in the real system, not the agent's own summary.
- Turn each failure into a regression case that a test keeps.
- Remove or hide personal data in traces and limit who can read them.
Common traps
Trap"The final answer tells us what broke."
BetterIt shows the symptom. Walk the trace to the first step that went wrong.
Trap"We'll fix it in the prompt."
BetterA prompt can hide a bad tool result. Find the cause in its step, then fix that.
Trap"Logs are enough, so we don't need tests."
BetterLogs show what happened once. A regression case stops it happening again.
Can you spot the missing clue?
Spot the clue
A claims run gave a wrong answer. Which record would help most?
Quick check
A run used the wrong cover limit. Where do you look first?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
- Q181How do you evaluate agents?11 min
- Q260What is tracing for LLM apps?10 min
- Q275Eval scores dropped but the model is unchanged. How do you debug?Coming soon
- Q242How do you log without storing sensitive data?Coming soon
- Q174What are common agent failure modes?12 min
- Q184How do you manage agent state?12 min
Learn it in depth
Last updated on 26 November 2026
Is an agent working in a demo but failing in production?
I design and build AI agents with tracing, tests and checks built in from the start. Book a short call and tell me what your agent does and where it goes wrong.