Sub topic 2
Traces and spans: the core model
This chapter assumes you can write basic Python with context managers and have called an LLM API. It also assumes you know roughly what logs and metrics are. Here you learn the data model that every later chapter builds on.
What a span is
A span is a record of one timed operation. It has a name, a start time, an end time, a span ID, and a set of key-value attributes. A model call, a tool call, and a vector search are each one span.
A trace is the set of spans that belong to one request or one agent run. All spans in a trace share a trace ID. Each span except the first also stores the ID of its parent span. The first span, with no parent, is the root span.
The span tree
The parent IDs turn a flat list of spans into a tree. For an agent run, the root span covers the whole run. Its children cover each model call and tool call. The tree shows both what happened and what caused it.
The duration of a parent covers its children, so you can see where time went. If the root took 6 seconds and the two chat spans took 1 second each, the tool call is the likely cause of the rest.
Spans in code
The Python API creates a span with a context manager. The span starts when the block starts and ends when the block exits. This example assumes the SDK is configured as shown in the next chapter. Without that setup, the calls are no-ops.
from opentelemetry import trace
tracer = trace.get_tracer("support_agent")
def search_orders(customer_id):
with tracer.start_as_current_span("execute_tool search_orders"):
return [{"id": "A-1001", "customer": customer_id}]
def run_agent(question):
with tracer.start_as_current_span("invoke_agent support-agent"):
orders = search_orders("c-42")
return f"Found {len(orders)} order(s)"
print(run_agent("Where is my order?"))start_as_current_span makes the new span the current one. Any span started inside the block becomes its child. This is why execute_tool search_orders nests under invoke_agent support-agent without any ID passing.
A tracer, not a span, is what you keep
You get a tracer once, usually at module level, with trace.get_tracer(name). The name identifies your instrumentation code, not the service. You create many spans from one tracer.
Common errors
- Starting a span without making it current.
tracer.start_span()creates a span but does not set it as the parent for later spans. Usestart_as_current_spanunless you manage context yourself. - Forgetting to end a span. A span that never ends is never exported. The
withblock ends it for you, even when an exception is raised. - Expecting output with no SDK. The API alone records nothing. You need a configured provider, covered in the SDK chapter.
- Treating a trace as a log. A trace records structure and timing. Put details in attributes and events, which the next chapter covers.
Exercise
An agent run calls the model, then a tool, then the model again, all inside one start_as_current_span("invoke_agent ...") block. How many traces and how many spans are produced, and which span is the root?
Show answer
One trace with four spans: the agent span and three children (chat, tool, chat). The agent span is the root because it has no parent. All four share one trace ID.