Distributed Tracing

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.

Level
04 · Production
Subdomain
Observability & Evaluation
Also known as
Agent Tracing, End-to-End Trace, Span-based Observability
Trade-off
Deep diagnostic capability is achieved against telemetry and privacy management effort. Indispensable in production: vendors and practitioners consistently report large reductions in mean-time-to-diagnose once end-to-end traces are in place.
Native in
OpenAI Agents SDK, LangSmith, Microsoft Agent Framework, AWS X-Ray, Google Cloud Trace
The mechanism

How it works.

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

  1. Start a trace per runOne trace ID
  2. Wrap each step in a spanModel call, tool call, sub-agent
  3. Attach attributesModel, tokens, tool name, error

2 · Use

  1. Export the spansTo any backend that speaks the standard
  2. SampleAll errors, a fraction of the rest
  3. Open one traceThe tree, with durations
  4. Find the slow or failed spanAnd look at its inputs
Span
One step of a run with a start, an end and attributes. Spans nest: a tool call is a child of the run.
Trace ID
The ID all spans of one run share. It is passed on to sub-agents and tools so that their spans join the same tree.
Sampling
Keeping only some traces. A common policy keeps every trace with an error and a small share of the successful ones.
Walk it through

A real run, step by step.

TaskA research agent took 41 seconds for one answer. The trace of that run:
Illustrative run.
SpanKindSecondsNote
research_runRun41.0The whole run
chatModel call2.1Planning, 900 tokens
execute_tool web_searchTool call1.48 results
execute_tool retrieve_papersTool call33.8Timed out twice, succeeded on the third attempt
chatModel call3.5Writing, 2,400 tokens
What this shows: without the trace the finding is “the agent is slow”. With it the finding is one tool and two timeouts, and the model accounts for less than six of the 41 seconds. The price is that spans carry prompts and tool data, which need redaction and access rules, and that keeping every trace is rarely affordable, hence the sampling.
In code

The pattern in code.

OpenTelemetrypython
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")
Fit

Use it, or not.

When to reach for it

  • Multiple components contribute to an answer.
  • Error causes and latencies must be analyzed.
  • Production operations require continuous monitoring.

When it backfires

  • The system remains minimal and local.
  • Trace data creates unprotected privacy risks.
  • Telemetry costs exceed the benefits.
Pitfalls

Two ways this pattern will hurt you.

Token-flood traces

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.

Trace IDs that don't survive tool boundaries

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

Regulation

Duties from EU law that this pattern helps meet, or that land on it. A reading aid, not legal advice.

  • EU AI Act Art. 9A risk-management system run as a continuous, iterative process over the whole lifecycle.Providers of high-risk systems · from 2 Dec 2027 (Annex III) or 2 Aug 2028 (Annex I products)
  • EU AI Act Art. 12The system technically allows automatic recording of events (logs) over its lifetime, for traceability.Providers of high-risk systems · from 2 Dec 2027 (Annex III) or 2 Aug 2028 (Annex I products)
  • EU AI Act Art. 13Operation is transparent enough for deployers to interpret the output and use it appropriately; instructions for use come with the system.Providers of high-risk systems · from 2 Dec 2027 (Annex III) or 2 Aug 2028 (Annex I products)
  • EU AI Act Art. 72Providers actively and systematically collect and analyse performance data over the system's lifetime.Providers of high-risk systems · Chapter IX, in application since 2 Aug 2026; the high-risk requirements it refers to follow from 2 Dec 2027 or 2 Aug 2028
  • GDPR Art. 35Before processing that is likely to result in a high risk, in particular with new technologies, the controller assesses its impact on the protection of personal data.Controllers · since 25 May 2018Explained on the DPIA page

An engineer's reading aid, not legal advice. The legal texts and their interpretation are authoritative.

Framework support

Where Distributed Tracing is native.

OpenAI Agents SDKbuilt-in TracingNative
LangSmithServicerun trees + span exportNative
LangGraphtraces to LangSmithAdaptable
Microsoft Agent FrameworkOpenTelemetry exportNative
AWS X-RayServicedistributed-trace backendNative
Google Cloud TraceServicemanaged trace backendNative

Search

Search patterns, frameworks, and pages.