Sub topic 16
Reading traces to debug agents
This chapter assumes your agent already emits an invoke_agent root span with child chat and execute_tool spans, as built in earlier chapters. Here you learn to read the resulting trace.
Read the shape first
Open one trace in your backend and look at the tree before any attribute. The shape of an agent trace shows most problems.
- A wide root with a few short children means the time is spent outside your spans, such as in queueing or unpatched code.
- One very long child means a single slow step.
- Many alternating
chatandexecute_toolchildren means the agent is looping. - Red spans, which have error status, show where a failure started.
Find the slow step
Sort the root's children by duration. If a chat span is slow, compare gen_ai.usage.output_tokens with a fast trace. Long outputs are slow. If an execute_tool span is slow, open its children. A tool that calls an HTTP API should show a client span for that call.
Find a runaway loop
A loop shows up as the same tool span name repeated with similar inputs. Count spans by name under the root. A count of 15 execute_tool search_orders spans in one run is a bug, even if every call succeeded. Check gen_ai.usage.input_tokens on each later chat span too. It grows each turn as history accumulates, so a loop gets more expensive as it runs.
Find the first failure
Errors cascade. A failed tool call can lead the model to retry with bad arguments, and the retries fail too. Order the error spans by start time and read the earliest one. Its exception event holds the type, message and stack trace.
You can script these checks over exported spans. This example uses a small hand-written list of spans.
from collections import Counter
spans = [
{"id": "a", "parent": None, "name": "invoke_agent support-agent", "ms": 9200, "error": False},
{"id": "b", "parent": "a", "name": "chat example-model", "ms": 900, "error": False},
{"id": "c", "parent": "a", "name": "execute_tool search_orders", "ms": 3000, "error": True},
{"id": "d", "parent": "a", "name": "chat example-model", "ms": 850, "error": False},
{"id": "e", "parent": "a", "name": "execute_tool search_orders", "ms": 3100, "error": True},
]
root = next(s for s in spans if s["parent"] is None)
children = [s for s in spans if s["parent"] == root["id"]]
slowest = max(children, key=lambda s: s["ms"])
repeats = Counter(s["name"] for s in children)
errors = [s["name"] for s in children if s["error"]]
print("slowest:", slowest["name"], slowest["ms"], "ms")
print("repeated:", [n for n, c in repeats.items() if c > 1])
print("errors:", errors)Running it prints the slowest child, execute_tool search_orders at 3100 ms. It also prints the names of the repeated spans and of the failed spans.
Compare, don't guess
Never judge a trace in isolation. Pull one slow or failed trace and one normal trace for the same kind of request. Differences in span count, token usage and tool arguments usually point to the cause.
Exercise
A trace has a root span of 20 seconds. Its children are 8 alternating chat and execute_tool lookup_policy spans, each under 3 seconds, and none has error status. What is the likely problem, and what do you check next?
Show answer
The agent is probably looping: the time comes from many repeated steps, not one slow step. Check the arguments passed to each lookup_policy call to see whether they repeat, and check the loop's exit condition. Also check whether gen_ai.usage.input_tokens grows on each chat span, which confirms the history is accumulating.