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, 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:
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:
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, pick the five attributes you'd actually query, and get them right before adding the other twenty.

