When to reach for it
- Multiple components contribute to an answer.
- Error causes and latencies must be analyzed.
- Production operations require continuous monitoring.
Spans across the whole agent loop.
Agent runs, tool calls, and subprocesses are made visible as connected end-to-end traces. The cross-vendor standard is the OpenTelemetry GenAI semantic conventions (gen_ai.* span attributes), so a run instrumented once is portable across observability backends; a common sampling policy keeps all error traces and only a small fraction of successful ones.
In practiceA multi-step research agent emits OpenTelemetry spans for each tool call and sub-agent invocation, so an on-call engineer can pinpoint which retrieval step caused a P99 latency spike.
Every step of a run records a span: what it was, how long it took, what it used. The spans share one trace ID and point to their parent, so the whole run can be read as a tree.
1 · Instrument
2 · Use
| Span | Kind | Seconds | Note |
|---|---|---|---|
| research_run | Run | 41.0 | The whole run |
| chat | Model call | 2.1 | Planning, 900 tokens |
| execute_tool web_search | Tool call | 1.4 | 8 results |
| execute_tool retrieve_papers | Tool call | 33.8 | Timed out twice, succeeded on the third attempt |
| chat | Model call | 3.5 | Writing, 2,400 tokens |
from opentelemetry import trace
tracer = trace.get_tracer("research-agent")
def call_model(prompt, model_name):
# one span per model call
with tracer.start_as_current_span("chat") as span:
span.set_attribute("gen_ai.request.model", model_name)
reply = model(prompt, model=model_name)
span.set_attribute("gen_ai.usage.input_tokens", reply.input_tokens)
span.set_attribute("gen_ai.usage.output_tokens", reply.output_tokens)
return reply.text
def call_tool(name, **arguments):
with tracer.start_as_current_span(f"execute_tool {name}") as span:
span.set_attribute("gen_ai.tool.name", name)
try:
return TOOLS[name](**arguments)
except Exception as error:
span.record_exception(error) # the failed span is kept
raise
def research(question):
# the parent of every span below
with tracer.start_as_current_span("research_run"):
plan = call_model(f"Plan the research: {question}", "large")
papers = call_tool("retrieve_papers", query=plan)
return call_model(f"Answer {question} using {papers}", "large")When to reach for it
When it backfires
Every LLM call dumps its full prompt and response into the trace. Storage costs explode and the trace UI becomes unusable.
Countermeasure
Sample full payloads at 1%. For the rest, log token counts, latency, and model name only. Summarize prompts if needed.
The agent passes a trace ID to a tool, but the tool calls another service that generates its own ID. The distributed trace breaks.
Countermeasure
Propagate trace context via standard headers (e.g., W3C traceparent). Reject tools that don't forward the trace ID.
EU law
Duties from EU law that this pattern helps meet, or that land on it. A reading aid, not legal advice.
An engineer's reading aid, not legal advice. The legal texts and their interpretation are authoritative.
Keep going
Search patterns, frameworks, and pages.