Kolkata ·  
AI engineering
production
deep · 5 min

LLM tracing

Recording every call, prompt, token count, cost and step, so you can debug and measure.

updated 29 Sept 2026

A bug report comes in: "I asked the docs assistant how to rotate my API key, and it told me the wrong steps."

What do we look at? If all we kept was the final answer, not much. We can't see which docs it found, what prompt the model actually got, or whether some tool returned an error along the way. We'd be guessing.

Tracing is how we stop guessing. We record every step the app took to answer that one request, with enough detail to replay it in our heads later. Let's build up what that record holds.

One request, a tree of steps

Behind that one question, the app did several things. It searched the docs (RAG). It called the model with the question and the chunks it found. The model asked to call a tool, say get_account_settings (tool calling). Our code ran the tool and called the model again with the result. Then the answer streamed back.

The record of that whole request is a trace. Each step inside it is a span, with a start time, an end time, a parent, and whatever details we attach. Spans nest, so the trace is a little tree:

Tools use slightly different words. Langfuse calls each step an observation, calls a model call a generation, and groups the traces from one conversation into a session. Same shape everywhere: sessions hold traces, traces hold a tree of steps.

What goes on a model-call span

So what do we write on the spans that are model calls? There's a shared vocabulary for this, the OpenTelemetry GenAI conventions. A model-call span carries fields like:

  • which operation and provider (gen_ai.operation.name, gen_ai.provider.name), and which model was asked for and which answered
  • token counts: input, output, input read from cache, and reasoning tokens
  • why the model stopped, and the error type if it failed
  • the prompt's name and version (gen_ai.prompt.version), so we know which version of the prompt made this answer

Tool calls get their own spans (execute_tool), and so do agent runs in an agent loop. Using these names even though they're not final keeps us free to switch tracing tools later. (They're marked "Development", and they recently moved to their own repository, so expect some renames.)

The token counts come straight from the provider. Every reply has a usage block, something like input_tokens: 12, output_tokens: 6, and the tracing library copies it onto the span.

Notice what's missing from that list: the actual prompt and reply. Those fields exist, but they're opt-in, because they're likely to hold users' personal data. For our bug report we need them. So we turn them on, and now we have a PII handling problem to plan for.

Reading it

With messages captured, the bug report is easy to chase. Open the trace, look at the chunks search returned, the exact prompt, the tool result, the reply. Most bugs show up right there: the wrong chunk came back, a tool returned an error the model glossed over, the prompt was missing a rule.

This is also the step before any real eval work. We can't grade answers we never saved. And the value comes from looking at lots of them, so it has to be easy to open a trace.

Traces answer the "where did the time go" question too. The spans have start and end times, so we can see whether the second model call was slow or the tool was. (LLM latency covers what happens inside the call itself.)

What did that answer cost?

The spans already hold token counts, so cost is one multiplication away: each kind of token times its price, added up.

Say the second model call ran on Claude Sonnet 5. As of 2026-09 that's $2 per million input tokens, $10 per million output, and cache reads at a tenth of the input price. The call used 6,000 input tokens, 5,000 of them read from the prompt cache, and wrote 400:

TokensCountPrice per millionCost
Uncached input1,000$2.00$0.0020
Cached input5,000$0.20$0.0010
Output400$10.00$0.0040
Call total$0.0070

Output was less than a tenth of the tokens and more than half the cost (more on why in token pricing).

Careful with that table, though. In the OpenTelemetry fields, input_tokens already includes the cached ones. Some tracing tools want each token in exactly one bucket. Copy input_tokens as "input" and also send the cache reads, and we've billed those 5,000 tokens twice, once at full price.

The conventions have no cost field at all, only token counts. Price comes from us or from the tracing tool, which matches the model name against a price table it keeps. If we send a cost ourselves, that wins. Tool-side prices get frozen when the trace arrives, so old traces keep old prices, and the table only knows the discounts someone put in it. And with reasoning models, we pay for thinking tokens we never see, so the count has to come from the provider, not from tokenizing the visible reply.

One call's cost isn't that useful alone. The questions we'll actually get are "what does the docs assistant cost per day?" and "why did it get more expensive last week?". So we tag every trace with its feature and prompt version. Then we sum calls into requests, group requests by feature, and sum over a day. Grouping by prompt version is how we'd spot that a prompt change doubled the output length.

The small thing that loses traces

Tracing libraries batch spans and send them in the background, so they don't slow the request. That's fine for a server. A short script or a serverless function that exits right away can lose its traces unless it flushes first.

Back to the bug report. With a trace for that request, we'd open it, see that search returned the page for an old version of the API, and know the fix belongs in retrieval and not in the prompt. Without one, we'd be rewriting the prompt and hoping.

Further reading