Tracing and SLOs with OpenTelemetry GenAI
Evals tell you whether outputs are good. Traces tell you what actually happened to produce them: which model was called, with what prompt, which tools ran, how many tokens it cost and where the time went. OpenTelemetry (OTel) has a set of GenAI semantic conventions that standardise the names for all of this, so a trace from your app looks the same in Langfuse, Phoenix, Datadog or Grafana. On top of those traces and metrics you can build the dashboards and service level objectives (SLOs) that keep an AI feature healthy.
Why it matters
Without shared conventions, every SDK and vendor invents its own attribute names, and switching tools means re-instrumenting. With them, you instrument once and send the same data anywhere that speaks OTLP. Langfuse, for example, accepts OTLP traces at its /api/public/otel endpoint and maps gen_ai.* attributes to its own data model; Arize Phoenix ingests OTLP and uses its OpenInference instrumentation libraries.
Traces are also the raw material for everything else in this track. Error analysis needs traces to read. Online evaluation needs traces to score. Cost and latency SLOs need token counts and durations.
How it works
Status first. As of late 2026 the GenAI conventions are still marked Development, not stable, and attribute names have changed between releases. A few examples from recent versions: gen_ai.system was replaced by gen_ai.provider.name, per-message events were replaced by aggregated gen_ai.input.messages and gen_ai.output.messages attributes, and later releases added tool, retrieval, MCP, cache-token and reasoning-token attributes. The conventions were also moved into their own repository in 2026 so they can version independently. Always check the spec version your instrumentation library targets.
Spans. Each model call is a span, typically named {gen_ai.operation.name} {gen_ai.request.model} (for example chat my-model). Core attributes include:
gen_ai.operation.name(such aschat,embeddings,execute_tool,invoke_agent)gen_ai.provider.namegen_ai.request.modelandgen_ai.response.model(the model actually served)gen_ai.usage.input_tokensandgen_ai.usage.output_tokensgen_ai.response.finish_reasons
Tool executions and agent invocations get their own spans, so an agent run becomes a tree.
Rendering diagram…
Metrics. The conventions define histograms such as gen_ai.client.token.usage (split by gen_ai.token.type into input and output) and gen_ai.client.operation.duration, plus server-side ones like gen_ai.server.time_to_first_token. Check your library's version, as some of these names have been reorganised.
Content capture. Prompt and completion text are opt-in in most instrumentations because they can contain personal data. The official OpenAI instrumentation, for example, uses an environment variable to enable message content capture. Decide deliberately where content goes.
Reference architecture
Rendering diagram…
Running a Collector in the middle lets you redact, sample and fan out to several backends without changing app code. That is useful when the AI team wants an LLM-specific tool and the platform team wants everything in the company APM.
Dashboards and SLOs. I group AI dashboards into three rows:
- Cost: tokens per request (input, output, cached), cost per request and per user or tenant, computed from token counts times your price table (the conventions record tokens, not money).
- Latency: time to first token and total duration at p50, p95 and p99, broken down by model and by span type (retrieval vs generation vs tools).
- Quality: online judge pass rates, user feedback rate, refusal rate, error and finish-reason distribution (a spike in
lengthfinish reasons often means truncated answers).
SLOs follow the usual SRE recipe: pick an indicator, a target and a window. Examples: "95 percent of chat requests have time to first token under a target you set from user research, over 28 days", or "the faithfulness judge passes on at least 97 percent of sampled traces per week". Alert on error-budget burn rate, not single bad requests.
Code
Manual instrumentation of one chat call with the OTel Python SDK, exporting over OTLP/HTTP. In real code you would usually use an instrumentation library (OpenInference, OpenLLMetry, OpenLIT or the official OTel GenAI instrumentations) instead of setting attributes by hand, but writing it once shows what those libraries do.
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.trace import SpanKind, Status, StatusCode
provider = TracerProvider(resource=Resource.create({"service.name": "support-bot"}))
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) # uses OTEL_EXPORTER_OTLP_* env vars
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("support-bot")
def chat(model: str, messages: list[dict]) -> str:
with tracer.start_as_current_span(f"chat {model}", kind=SpanKind.CLIENT) as span:
span.set_attribute("gen_ai.operation.name", "chat")
span.set_attribute("gen_ai.provider.name", "openai")
span.set_attribute("gen_ai.request.model", model)
try:
resp = call_provider(model, messages) # your client call
except Exception as exc:
span.record_exception(exc)
span.set_status(Status(StatusCode.ERROR))
raise
span.set_attribute("gen_ai.response.model", resp["model"])
span.set_attribute("gen_ai.usage.input_tokens", resp["usage"]["input_tokens"])
span.set_attribute("gen_ai.usage.output_tokens", resp["usage"]["output_tokens"])
span.set_attribute("gen_ai.response.finish_reasons", [resp["finish_reason"]])
return resp["text"]
def call_provider(model: str, messages: list[dict]) -> dict:
raise NotImplementedError
Set OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_HEADERS to point at your Collector or directly at a backend.
Trade-offs and pitfalls
- Convention churn. Names are still moving. Pin instrumentation versions, and if you build dashboards on raw attribute names, expect to update them when you upgrade.
- Mixed conventions. OpenInference, OpenLLMetry and the official GenAI conventions overlap but are not identical. Pick one per service, or normalise in the Collector.
- PII in spans. Message content in traces is a data-protection decision. Redact in the Collector and limit retention.
- Sampling breaks evals. Head sampling that drops 90 percent of traces also drops 90 percent of your error-analysis material. Consider tail sampling that keeps errors, low scores and a random slice.
- Cost without prices. Token counts are standard; prices are not. Keep a versioned price table and recompute history when it changes.
- Averages lie. Report latency percentiles, not means. One slow tool can dominate p99.
- Quality SLOs need calibrated judges. An SLO on an uncalibrated judge is an SLO on noise.
Checklist
- Every model call, tool call and agent step emits a span with GenAI attributes.
- Instrumentation library and semantic convention versions are pinned and documented.
- Message content capture is an explicit, reviewed decision with redaction in place.
- A Collector handles redaction, sampling and fan-out to backends.
- Dashboards cover cost, latency percentiles and quality signals per model and tenant.
- At least one latency SLO and one quality SLO exist with burn-rate alerts.
- Sampling keeps error and low-score traces for review.