A client called me last month with a strange bug.
Their customer support agent had just refunded a user five hundred dollars by mistake.
When we looked at the final output, the AI gave a polite summary. It claimed the refund matched company policy. But when we checked the policy files, the refund was clearly not allowed.
The agent had run twelve tool calls across four data stores to make its choice.
Where did it go wrong? Did it read the wrong policy file? Did a search tool return bad data at Step 3? Or did the model misread an account date at Step 7?
Without clear tracing, we were flying blind. Standard server logs only showed that a request started and an answer came back. Everything in between was invisible.
Here is why debugging multi-step agents is so hard, how to build trace trees that record each hop, and how to spot bad tool runs fast.
In standard software, debugging is direct. When code crashes, your server prints a stack trace with exact line numbers.
With multi-step AI agents, there is no stack trace. The code does not crash.
The model simply makes a bad choice at one step, and that small error ripples through the rest of the run:
| Step in Run | What the AI Agent Did | Where the Error Started |
|---|---|---|
| Step 1 to 2 | Reads the user prompt and searches the client data store. | Worked as planned. |
| Step 3 | Search tool returns an old policy file from two years ago. | Root Cause: Old policy file matched search terms. |
| Step 4 to 11 | Agent queries billing records and checks user tenure. | The agent used the old policy rules to check the data. |
| Step 12 | Agent runs the refund tool and sends a confirmation note. | Resulted in a wrong five hundred dollar refund. |
If you only log the final prompt and answer, you miss the root cause at Step 3\.
To make multi-step agents observable, you must record each hop in a structured trace tree:
| Trace Pillar | What It Captures | Why It Matters |
|---|---|---|
| 1\. Hop Prompts | The exact prompt text passed to the model at each turn. | Shows what context the AI saw before making a choice. |
| 2\. Tool Payloads | Exact input arguments and raw tool return text. | Proves whether the tool or the model caused the error. |
| 3\. Token Tracking | Prompt tokens, answer tokens, and delay per step. | Pinpoints which tool call burned budget or slowed down. |
| 4\. Trace IDs | One unique ID shared across all twelve tool runs. | Links frontend chats to backend database calls. |
Record the exact text sent to the model before each tool call. This includes the system prompt, tool schemas, and recent chat history.
Always log the exact arguments the AI passed to the tool, and the raw text the tool returned. This tells you if the tool broke or if the model misread good data.
Some tool calls take two hundred milliseconds. Others take five seconds. Track time and token usage for each hop so you can find slow tools fast.
Pass a single Trace ID through each tool run. When a user reports a bad answer, you can pull up all twelve steps in one click.
When building agent tracking, watch out for these three common traps:
If you only log the start and end of a run, you cannot debug multi-step failures. Always log each middle tool call.
Agent traces often contain customer emails, names, and phone numbers. Mask private data before saving traces to your log store.
Do not store huge file downloads in your trace logs. Save a short summary and store large files in cold storage.
Debugging AI agents feels strange at first because you are not debugging code logic. You are debugging an information flow.
When an agent makes a bad choice, it is almost always because a tool gave it bad data, or an earlier step polluted the context window.
When you trace each hop with clean logs and clear timing, the invisible becomes visible. You spot bad tools in seconds and build agent workflows your team can trust.
A trace tree is a visual log that shows the full sequence of steps, tool calls, and model choices that took place during a single task.
Tracing shows you which single tool call used the most tokens or took the longest time, helping you trim prompt bloat.
Yes. Log all runs with light metadata, and sample full prompts and payloads to balance storage costs with debug needs.
Open tools like OpenTelemetry, Langfuse, and Arize Phoenix provide clean trace trees and span tracking for AI agents.