Anatomy of an Agent Trace
In this article
- Draw the span tree of one agent run, with a root span, a model call span, tool call spans and a policy check, each linked to its parent.
- Keep trace id, run id and session id apart, and say which one links the runs of one conversation.
- Given interleaved logs from two runs, assign each tool call to the run that made it and name the instrumentation that is missing.
- Say which timing, token and error attributes a span needs, and what you lose when cost or errors are not recorded.
- Terms defined here: trace ID.
Overview
Two customers write to your support assistant in the same second. Later a refund looks wrong, and you open the logs: both conversations are mixed together, and one line says an order was looked up, with nothing to say for whom. Which customer's request made that call?
Brightline, a made-up home-internet provider, runs a support assistant that looks up orders with lookup_order, and issues refunds with issue_refund. Code refuses any refund above $50, whatever the model proposes. Afterwards you can draw one run as a tree of timed steps, keep its three kinds of id apart, and decide which run owns a stray tool call and what the record was missing.
From a list of steps to a tree
An agent run is one request handled by a model that calls tools in a loop, and its agent trace is the ordered record of each step: inputs, model outputs, tool calls and results. A list says what happened next, but not how long a step took, which step caused it, or which run a line is from. Tracing adds two things.
The first is the span: one timed unit of work, such as one model call or one tool call, with a start time, an end time and a pointer to the span that contains it, its parent. [4] Spans nest, so a run becomes a tree: a root span, with no parent, for the whole run and a child span for each step. [5]
The second is the trace id: a unique id for one trace that every span in it also carries, so each span records which trace it belongs to. [4] Langfuse groups steps the same way, by a shared trace_id, and lists one agent run, from task to result, as a typical trace. [1] [7]
The span names come from OpenTelemetry, an open standard for collecting telemetry, the data a running application records about its own behaviour. [1] Its conventions for AI agents call the root span invoke_agent, used when a language model drives the control flow through tool calls. [2] A model call is a chat operation and a tool run an execute_tool operation. [3] These GenAI conventions have Development status (Nov 2026), so names may still change. [2]
Reading trace-91
Here is one Brightline run, r-91: a customer asks for a refund of a $25.00 charge on order 5120, and the refund is issued. Real ids are longer: the OpenAI Agents SDK, for one, writes them as trace_ followed by 32 letters and digits. [4] The drawing is trimmed to the spans that matter here:
trace-91
invoke_agent (root, run r-91)
├── chat 1.8 s
│ gen_ai.usage.input_tokens 1,240
│ gen_ai.usage.output_tokens 180
├── execute_tool lookup_order 220 ms ok amount: 25.00
├── policy check (event) code: refund 25.00 allowed, cap 50.00
└── execute_tool issue_refund okThe root stands for the whole run. The chat span is the model call that decided to look up the order: 1.8 s, 1,240 input tokens read, 180 output tokens written. The lookup_order span took 220 ms and returned amount: 25.00. The model's later turns, which propose the refund and write the reply, are left out. A GenAI span lasts from the start of the operation until its response is fully received, or until an error or cancellation ends it. [3] The token counts carry OpenTelemetry's names, gen_ai.usage.input_tokens and gen_ai.usage.output_tokens. [2]
The policy check is not a model step. It records a decision your code made: $25.00 is under the $50 cap, so the refund may go ahead. The drawing marks it as an event, a note with a time but no duration of its own, because a check has no work to time. Had the amount been $60, this line would show code refusing, a policy decision and not a model error. Langfuse, for one, offers events as an observation type beside spans and generations. [1]
Three ids, three jobs
The customer writes again: "Will it show on my next bill?" That message starts a new run with a new trace. The two runs belong together, so a third id must link them. Each id has one job:
| id | covers | example |
|---|---|---|
| trace id | the spans of one run | trace-91 |
| run id | your application's name for the run, stored on the root span so a ticket leads to the trace | r-91 |
| session id | the whole conversation, shared by every run in it | s-7 |
If the session field holds r-91, the follow-up gets a different value and the conversation splits into strangers. If one trace spans the whole chat, it grows with every message and never closes. Langfuse recommends one trace per chatbot turn and one session per conversation, partly because you cannot know in advance when a conversation ends. [7]
Whose tool call is this?
A trace is useful through attribution: with a stable trace id on every span and a parent link to the span that started the work, every model call and tool call can be assigned to the run that made it. Now take a call that lacks both.
Run r-92, another customer, starts 0.4 s after r-91. The log interleaves them in order of start time; a1, a2, b1, b2 and b3 are span ids:
| start | end | trace | span | parent | name |
|---|---|---|---|---|---|
| 10:02:00.000 | open | trace-91 | a1 | none | invoke_agent (r-91) |
| 10:02:00.020 | 10:02:01.820 | trace-91 | a2 | a1 | chat |
| 10:02:00.400 | open | trace-92 | b1 | none | invoke_agent (r-92) |
| 10:02:00.420 | 10:02:02.500 | trace-92 | b2 | b1 | chat |
| 10:02:01.830 | 10:02:02.050 | none | none | none | execute_tool lookup_order |
| 10:02:02.510 | 10:02:02.730 | trace-92 | b3 | a2 | execute_tool lookup_order |
This log is made up. Before reading on, decide: which run owns the fifth row, the one with no trace and no parent? The row just above it is from trace-92.
It is r-91. A tool runs only after a model call asks for it, and at 10:02:01.830 only one model call had finished: a2 in trace-91, 10 ms earlier. The r-92 call b2 ran until 10:02:02.500, so r-92 had not asked for anything yet. The row belongs under root a1 in trace-91: it is the 220 ms lookup_order of the drawing. Its neighbour in the log was a coincidence. A span with no parent and no trace id is an orphan: nothing in the record says which run it belongs to.
The sixth row fails differently. Its trace is trace-92, but its parent a2 is a span of the other run. That is a parent mismatch, and code can flag it by comparing a span's trace with its parent's. Its trace id and its timing, 10 ms after b2 ends, agree, so the span is r-92's and the broken field is the parent, which should be b1.
Now name the missing instrumentation. Row five's wrapper, the code around the tool function that opens its span, recorded the tool name and times but not the trace id, the parent span id or the run. Row six's wrapper picked up the wrong current span. The fix lives where a tool span starts: attach the trace id and parent span id of the run that requested the call.
The weak point: timing worked only because exactly one model call had just finished. Had both runs been waiting on lookup_order at once, two orphans would start in the same millisecond and no clock could tell them apart. Timestamps are a fallback; ids on every span make them unnecessary.
What a span should carry
To tell why a run was slow, costly or wrong, each span also needs its numbers: start and end times, each model call's token counts, and an error status. OpenTelemetry requires error.type whenever an operation ended in an error, naming its class, such as timeout or 500. [2]
Missing values hide two things. Cost: Claude's Agent SDK records durations, model names and tool names on every span, but token counts only when the API request returns usage data, so spans of failed or aborted requests may have none. [8] Sum such a trace and the failed calls cost nothing on paper. Errors: a lookup_order span with no error status reads as a quick success, and the wrong refund after it has no visible cause.
The model at work
Trace each run end to end with stable ids and causal parent links, so every model call, tool call, policy decision and action can be assigned to its run. Then ask of any trace: Does every span carry the trace id? Does every parent belong to the same trace? Does each model span carry its tokens, and each failed span its error?
Common mistakes
- Using the run id as the session id. Each follow-up gets a new value and the conversation falls apart. Give the conversation its own id.
- Starting tool spans without the parent. The span lands as an orphan and you are back to timestamps. Pass the trace id and parent span id into every tool wrapper.
- Calling a cap refusal a model error. The cap is code's decision; record it as a policy check.
- One span for the whole agent loop. You see only the summed cost and the final output, not what the agent decided after each tool result or which tool call blew up the context window. [7]
In production
Run the three questions on a sample of real traces after each change to the agent, since a tool added without the wrapper makes orphans silently. Alert on parent mismatches, which code finds without reading content. Expect span names to shift while the GenAI conventions stay in Development (Nov 2026), so keep the names your queries rely on in one place. [2]
Sources
- Langfuse: Observability data model. Langfuse. Checked 24 Oct 2026.
- Semantic Conventions for GenAI agent and framework spans (OpenTelemetry). OpenTelemetry. Checked 25 Oct 2026.
- OpenTelemetry GenAI semantic conventions: model spans. OpenTelemetry. Checked 25 Oct 2026.
- Agents SDK: Tracing. OpenAI (openai-agents-python). Checked 24 Oct 2026.
- Agent tracing overview (Microsoft Foundry). Microsoft. Checked 21 Nov 2026.
- Observability for AI agent developers (Google Cloud). Google Cloud. Checked 21 Nov 2026.
- Langfuse: What does a good trace look like?. Langfuse. Checked 24 Oct 2026.
- Observability with OpenTelemetry. Anthropic. Checked 24 Oct 2026.
Check yourself
Check yourself
Question 1 of 13
What does one span in an agent trace record?
Your first check counts towards your result.