Tracing LLM Calls with OpenTelemetry: Your Token Counts Don't Mean the Same Thing OpenTelemetry's GenAI semantic conventions, now hosted in the open-telemetry/semantic-conventions-genai repository and still marked Development, specify that gen_ai.usage.input_tokens should include cached tokens, with cache_read and cache_write input tokens reported as breakdowns. The guidance warns that providers disagree: OpenAI's prompt_tokens already includes cached tokens, while Anthropic's input_tokens counts only the uncached portion and reports cache_read_input_tokens and cache_creation_input_tokens separately, so copying each provider's input_tokens field directly into dashboards misstates cost. The conventions also call for one CLIENT span per logical call named '{gen_ai.operation.name} {gen_ai.request.model}' that covers automatic retries, with gen_ai.operation.name, gen_ai.provider.name and server address set at span creation so samplers can see them. The first time an LLM feature gets slow or expensive in production, most teams reach for console.log response.usage . It works for an afternoon. Then you have three providers, a retry wrapper, a streaming endpoint, and an agent loop that calls a tool four times per request, and the logs can't tell you which call ate the budget or why the p95 doubled on Tuesday. You already have the answer if your app speaks OpenTelemetry. An LLM call is just an outbound request with some extra fields worth recording, and OpenTelemetry has a set of GenAI semantic conventions that name those fields so every backend reads them the same way. The catch is in the details: what goes in the span, what stays out, and the fact that "input tokens" means different things depending on who you ask. One span per logical call, named the boring way The conventions live in their own repo now, open-telemetry/semantic-conventions-genai https://github.com/open-telemetry/semantic-conventions-genai , and everything in it is still marked Development . Attribute names can still change, so pin your instrumentation versions and don't build a dashboard you can't rename. The shape is simple. An inference span is a CLIENT span named {gen ai.operation.name} {gen ai.request.model} , so chat claude-sonnet-4-5 . It should cover the whole logical operation, including automatic retries . That's the part people get wrong: if your retry wrapper sits outside the span, a 429 followed by a success looks like one fast call and your latency numbers are fiction. Here's a hand-rolled wrapper using only @opentelemetry/api , so it works with whatever SDK and exporter you already run: python import { trace, SpanKind, SpanStatusCode } from "@opentelemetry/api"; import Anthropic from "@anthropic-ai/sdk"; const tracer = trace.getTracer "llm-client" ; const client = new Anthropic ; // SDK retries live inside this call export async function chat params: Anthropic.MessageCreateParamsNonStreaming { return tracer.startActiveSpan chat ${params.model} , { kind: SpanKind.CLIENT, // Set these at creation time so samplers can see them attributes: { "gen ai.operation.name": "chat", "gen ai.provider.name": "anthropic", "gen ai.request.model": params.model, "gen ai.request.max tokens": params.max tokens, }, }, async span = { try { const res = await client.messages.create params ; span.setAttributes { "gen ai.response.id": res.id, "gen ai.response.model": res.model, "gen ai.response.finish reasons": res.stop reason ?? "unknown" , ...usageAttributes res.usage , } ; return res; } catch err { span.recordException err as Error ; span.setAttribute "error.type", err as any ?.status?.toString ?? " OTHER", ; span.setStatus { code: SpanStatusCode.ERROR } ; throw err; } finally { span.end ; } }, ; } The spec asks for gen ai.operation.name , gen ai.provider.name , and the server address at span creation, not after the response, because those are what a sampler uses to decide whether to keep the trace. Everything else can land when the response arrives. Normalize tokens before they reach a dashboard This is the part that quietly breaks cost reporting. The convention says gen ai.usage.input tokens should include cached tokens , with gen ai.usage.cache read.input tokens and gen ai.usage.cache write.input tokens as breakdowns on top. Providers don't agree. OpenAI's prompt tokens already includes cached tokens. Anthropic's input tokens counts only the uncached portion, with cache read input tokens and cache creation input tokens reported separately. Copy each provider's input tokens field straight across and a heavily cached Anthropic call looks almost free next to an identical OpenAI call, in the same chart. So do the math once, at the edge: js function usageAttributes u: Anthropic.Usage { const cacheRead = u.cache read input tokens ?? 0; const cacheWrite = u.cache creation input tokens ?? 0; return { // Convention: total input, cached included "gen ai.usage.input tokens": u.input tokens + cacheRead + cacheWrite, "gen ai.usage.output tokens": u.output tokens, ... cacheRead && { "gen ai.usage.cache read.input tokens": cacheRead } , ... cacheWrite && { "gen ai.usage.cache write.input tokens": cacheWrite } , }; } Now cost is a query, not a guess: total input minus cache reads at the full rate, cache reads at the discounted rate, output at the output rate. Keep the price table in your query layer, not in the span. Prices change; recorded token counts shouldn't need a migration when they do. For streaming, add gen ai.request.stream: true and record gen ai.response.time to first chunk in seconds. Time to first chunk is what users feel, and total duration hides it. Prompts stay out by default The obvious move is to dump every prompt and completion onto the span. The spec says don't, at least not by default. Content attributes such as gen ai.input.messages , gen ai.output.messages , gen ai.system instructions , and tool-call arguments are opt-in , controlled by OTEL INSTRUMENTATION GENAI CAPTURE CONTENT , which defaults to NO CONTENT . The other values are SPAN ONLY , EVENT ONLY , and SPAN AND EVENT . There are good reasons for that default. Prompts carry user PII, they're large enough to blow past attribute size limits in many backends, and your tracing vendor probably has looser access controls than your database. The spec's suggested production pattern is to upload content to separate storage and record only a reference gen ai.input.messages ref and friends on the span. That way the people who can see latency graphs aren't automatically the people who can read customer conversations. A reasonable setup: SPAN ONLY in local and staging, NO CONTENT in production, and a sampled external upload when you're actively debugging a quality issue. Where to go from here You don't have to hand-roll any of this. Auto-instrumentation packages for the major SDKs emit these attributes for you, and most LLM observability tools ingest OTLP directly. Still, write the wrapper once by hand. Then you'll know what the attributes mean, you can check that a library normalizes tokens the way you expect, and you'll notice when the conventions change under you, which they will while they're marked Development. Start with the client inference spec https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/client-inference.md , pick the five attributes you'd actually query, and get them right before adding the other twenty.