Topic 2 of 5 · 4 min read
Tracing agent runs with spans
Goal: Read a span tree of an agent run and locate the step responsible for most of its latency or cost.
A trace records one agent run as a tree of timed spans: one span for the run, with child spans for each model call, tool call and handoff. Reading the tree tells you where the time and the tokens went.
Check yourself · 1 of 3
In a trace of an agent run, what is a span?
Why it matters
A transcript shows what the agent said and did. It does not show that one tool call took 14 seconds, or that the same model call ran four times, or that a sub-agent used most of the tokens. Those are span questions. Once an agent has several steps, tools and sub-agents, you need both views.
How to do it
- Know the span types. Most agent frameworks record the same kinds: the whole run, a model call (often called a generation), a tool or function call, a handoff to another agent, and sometimes a guardrail check. The OpenAI Agents SDK, Langfuse and LangSmith each name them a little differently.
- Look for the standard names. OpenTelemetry has semantic conventions for generative AI. They give model and tool spans agreed attribute names, such as the model used and the input and output token counts. If you emit them, any tool that reads OpenTelemetry can show your traces.
- Read the tree from the top. Each span has a start, a duration, its parent and its attributes. Parents contain their children, so a slow parent with fast children means time was spent between them, for example waiting on a queue.
- Find the heavy span. Sort by duration for latency and by tokens for cost. Check whether the same span repeats: a loop of five identical search calls is a bug, not a slow tool.
- Link back to the transcript. A span's attributes hold the input and output. Open the heavy span and read it as in a transcript.
Set up tracing before you need it. Each run should carry an id, and a user or session id, so you can find the run a customer complained about.
Before and after
Weak: "The agent is slow, so use a faster model." Nobody looked at the trace.
Better: "The run took 31 seconds. The trace shows model calls totalling 6 seconds and one lookup_account tool span of 22 seconds. The model is not the problem."
What goes wrong
- Tracing only model calls. Slow or failing tools, which are most of the time in many agents, stay invisible.
- Dropping inputs and outputs from spans to save space. You then have a timeline with nothing to read. If the data is sensitive, redact it and keep the structure.
- No shared id across handoffs. Each sub-agent starts its own trace, and you cannot see one run end to end.
- Sampling away the failures. Keep every errored or flagged run, and sample only the normal ones.
Examples
Where did 31 seconds go?
run support_agent 31.2s
generation (plan) 1.8s
tool lookup_account 22.4s
generation (answer) 3.1s
generation (format reply) 3.9s
The tool span is about 72 percent of the run. Faster models would save little. Look at the account lookup: its query, a missing index or a retry inside the tool.
The loop that costs money
A trace shows 9 search_docs spans in one run, each with the same query text and each followed by a model call. Tokens climb at every round. The agent is stuck in a loop because the tool returns the same result and nothing tells it to stop. The fix is a stop rule or a result the model can use, not a cheaper model.
Key points
- A trace is a tree of spans: the run, then child spans for model calls, tool calls and handoffs.
- Spans show duration and token use per step, which a transcript alone does not.
- OpenTelemetry's GenAI semantic conventions give model and tool spans shared attribute names, so traces work across tools.
- Find the heavy span by sorting on duration or tokens, and check for repeated identical spans.
- Record inputs and outputs on spans (redacted if needed) and keep one trace id across handoffs.
Common mistakes
- Tracing model calls but not tool calls, so slow tools never show up.
- Storing only timings and token counts, with no inputs or outputs to read.
- Starting a new trace in each sub-agent instead of passing the trace id through handoffs.
- Sampling traces uniformly, so the rare failures are the ones that get dropped.
Check yourself · 2 of 3
A run took 40 seconds. The trace shows model call spans totalling 5 seconds and one tool span of 31 seconds. What is the best next step?
Check yourself · 3 of 3
Why do many teams emit OpenTelemetry GenAI semantic conventions for model and tool spans?
Practice items, not counted towards your mastery.
Start here
- OpenTelemetry semantic conventions for generative AI (opens in a new tab)StandardFree
The standard names for model-call and tool-call spans and their attributes, so traces look the same across tools.
- Langfuse observability overview (opens in a new tab)ToolFree
Shows how traces, nested observations and sessions map onto an agent run, in an open-source tool.
- Tracing (OpenAI Agents SDK docs) (opens in a new tab)DocsFree
Lists the span types an agent framework records: agent, generation, function, handoff and guardrail.