OpenTelemetry GenAI: A Reproducible Agent Trace
Build and inspect a synthetic agent-style trace with pinned OpenTelemetry versions, a failed tool span, parentage assertions, and a content-free export policy.

An agent's final answer cannot explain which model call failed, which tool ran, or where a handoff lost its parent trace. Structured telemetry makes those questions inspectable, provided it records the right metadata and keeps sensitive payloads out.
This article uses the OpenTelemetry GenAI conventions at commit e6658f1, inspected on 12 November 2026. The conventions are in Development. The runnable example pins OpenTelemetry Python SDK 1.45.2. The semantic-conventions revision and SDK package version are separate contracts.
Give the run a traceable shape
The pinned agent-span convention distinguishes model-driven invoke_agent control flow from application-defined invoke_workflow control flow. Inference spans describe model requests. Tool spans describe execution.
A minimal topology is:
invoke_workflow synthetic_workflow INTERNAL
chat synthetic-model CLIENT
execute_tool lookup_status INTERNAL, ERROR
That topology describes an application-controlled demonstration. A real agent may make several inference calls and tool calls, and may invoke another agent. The trace should reflect the actual nesting and ownership, rather than flattening every operation into one list.
Use bounded identity and outcome attributes: operation, provider, model, agent or tool name, and a stable error class. Record token use only when it is actually measured or reported by the provider. Invented counts make a cost dashboard look complete while corrupting its totals.
Run a synthetic trace without a model or API key
Download the complete Python example, then run:
uv run genai-trace.py > trace.json
Its script metadata pins opentelemetry-sdk==1.45.2, the published SDK release. It exports JSON to standard output through an in-memory exporter. It sends no telemetry to a backend and performs no inference. Installing the pinned dependency may require network access.
The important instrumentation is small:
with tracer.start_as_current_span(
"execute_tool lookup_status",
kind=SpanKind.INTERNAL,
attributes={
"gen_ai.operation.name": "execute_tool",
"gen_ai.tool.name": "lookup_status",
},
) as tool:
tool.set_attribute("error.type", "timeout")
tool.set_status(Status(StatusCode.ERROR))
The full script creates a workflow parent, a synthetic inference child, and a failed tool child. It deliberately keeps two private sentinel strings out of span attributes and events. Before printing, it asserts three spans, one trace ID, two children with the correct parent, an error status on the tool, an attribute allowlist, and absence of both sentinel strings from the serialized result.
Trace IDs, span IDs, and timestamps change on each run. The assertions validate structure and this example's payload policy. They do not validate automatic instrumentation, cross-process propagation, a live model, a Collector deployment, or a general-purpose redaction system.
Content capture is a separate decision
The pinned content-capture convention treats instructions, messages, and outputs as sensitive, potentially large content. Tool arguments and results can be equally revealing: a lookup key may contain a name or account identifier.
The convention defines OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT with NO_CONTENT, SPAN_ONLY, EVENT_ONLY, and SPAN_AND_EVENT. That is an instrumentation contract, not a universal switch that changes every framework or custom span. Setting it does not prevent application code from calling set_attribute with a prompt.
For each installed instrumentation package, pin its version, inspect its own configuration, and test the exported payload. Package defaults and attribute names can differ from the development conventions. A vendor's tracing system may also export through a separate pipeline that an OpenTelemetry Collector does not control.
The example uses an explicit allowlist and emits no content. For production evaluation that requires content, consider an access-controlled artifact store with retention rules and only opaque references in traces. A reference is not automatically anonymous: access paths, identifiers, baggage, and error messages also need review.
OpenTelemetry's sensitive-data guidance recommends minimization and describes Collector processors for removing or transforming attributes. Treat those as an additional control after source-side decisions. They cannot recover data already sent to a different exporter.
Keep context connected across boundaries
W3C Trace Context defines traceparent and tracestate for propagation. An in-process context manager handles nesting in the example. It does not prove that context survives an HTTP request, queue, subprocess, or MCP message.
The pinned MCP convention carries context in each request or notification's params._meta. MCP SEP-414 identifies the unprefixed keys traceparent, tracestate, and baggage. The receiving process must extract that context before creating its span.
Test a boundary with both processes running. Assert the shared trace ID, the remote parent relationship, and behavior when context is missing or malformed. Where a hosted tool prevents injection or extraction, record the trace break explicitly. Do not fabricate a parent relationship to make the waterfall appear complete.
Avoid putting personal or secret values in propagation fields. Baggage travels beyond the originating span and can reach systems with different access controls. A trace correlation mechanism should not become another copy of the user record.
Use traces for evaluation without mistaking them for enforcement
A trace can show an unexpected tool, a missing approval, a retry, or a failed verification. Recording the event does not stop it. Permissions, approval binding, and state transitions still belong in the application or gateway.
The canonical agent-evaluation article separates tool contracts, trajectory invariants, and outcome checks. Telemetry supplies evidence to those checks. A useful test records an attempted prohibited action and verifies both that execution was blocked and that the trace accurately reflects the outcome.
Evaluation results can be correlated with the span they judge. The pinned GenAI event convention defines gen_ai.evaluation.result. Preserve evaluator identity and version outside or alongside the score so a changed rubric does not silently rewrite the meaning of an old result.
Also trace waiting and failure recovery. A short model call nested inside a long approval wait is a workflow problem the token counter cannot explain. The coordination article describes the corresponding operational measures.
Make instrumentation changes reviewable
Maintain one mapping between emitted attributes and downstream dashboards or evaluators. Store the instrumentation package versions and semantic-conventions revision with the application release. When upgrading, compare exported payloads and rerun topology, error, and privacy checks.
Start with a small synthetic trace like this one. Add a real tool boundary, then a provider integration, then the exporter and Collector configuration. Each step should demonstrate what it records, what it withholds, and where parentage remains intact. A readable waterfall is useful evidence only when those contracts hold.





