LLM observability & tracing
Record every request as a trace with one span per step (retrieval, model call, tool call), carrying prompt version, tokens, latency, cost and feedback, so you can explain, price and replay any answer.
Analogy: A flight data recorder: when something goes wrong you don't guess what the pilot saw, you play back every instrument reading and every input in order.
How it works
- Classic APM says a request took 8 s; LLM tracing shows why the answer was wrong.
- Spans also carry retrieved doc ids and scores, tool calls with arguments, and feedback joined by trace id.
- Emit OpenTelemetry GenAI conventions (
gen_ai.*attributes) so any backend, like Langfuse or LangSmith, can read them. - Payloads are large and full of PII: redact before export, keep metrics on 100%, sample payloads.
- The payoff is replay: rerun a bad trace with a fixed prompt, then add it to your eval set.
Check yourself
Answer to mark it learned; your answers aren't scored.
A support bot's thumbs-down rate jumps from ~4% to ~11% overnight. Latency, error and cost dashboards are flat, and no prompt or model change shipped. You have full traces with feedback joined by trace id. First move?
Go deeper
Full explanation
LLM observability means every request leaves a trace you can read later: what was sent, what came back, how long it took, what it cost and whether the user was happy. Classic APM says a request took 8 s with status 200; LLM tracing says why the answer was wrong: stale chunks, the same tool called 6 times, a prompt version that doubled tokens.
One trace per request, one span per step. On each span record:
- prompt template and version, rendered messages, model, params
- retrieved doc ids and scores; tool calls with arguments and results
- input, output and cached tokens; TTFT and total latency; cost; finish reason
- user, session and tenant ids, plus feedback (thumbs, edits, retries) joined later by trace id
Emit it with the OpenTelemetry GenAI semantic conventions (gen_ai.request.model, gen_ai.usage.input_tokens...) so any backend can read it: Langfuse (open source, self-hostable; part of ClickHouse since 2026), LangSmith, Arize Phoenix, Braintrust, or your existing APM. (Helicone, the proxy-based option, went maintenance-only after its 2026 acquisition by Mintlify.)
Payloads are the hard part: large and full of PII. Redact before export, keep metrics on 100% of traffic but sample full payloads, and give prompts shorter retention than metrics (PII & data privacy).
The payoff is replay: open a bad trace, rerun it with a fixed prompt, then add it to your eval set (Offline vs online evaluation).
Code OpenTelemetry spans with GenAI attributes
from opentelemetry import trace
from openai import OpenAI
MODEL = "gpt-4.1-mini" # any chat model
tracer = trace.get_tracer("support-bot")
client = OpenAI()
def answer(question: str, user_id: str) -> tuple[str, str]:
with tracer.start_as_current_span("support.answer") as root:
root.set_attribute("app.user", pseudonymize(user_id)) # no raw PII
with tracer.start_as_current_span("retrieve") as s:
docs = search(question, k=8)
s.set_attribute("app.retrieval.doc_ids", [d.id for d in docs])
prompt = prompts.get("support-answer", version=14) # versioned template
with tracer.start_as_current_span(f"chat {MODEL}") as s: # GenAI span name
s.set_attribute("gen_ai.operation.name", "chat")
s.set_attribute("gen_ai.request.model", MODEL)
s.set_attribute("gen_ai.request.temperature", 0.2)
s.set_attribute("app.prompt.version", prompt.version)
r = client.chat.completions.create(model=MODEL, temperature=0.2,
messages=prompt.render(question=question, docs=docs))
s.set_attribute("gen_ai.response.model", r.model)
s.set_attribute("gen_ai.usage.input_tokens", r.usage.prompt_tokens)
s.set_attribute("gen_ai.usage.output_tokens", r.usage.completion_tokens)
s.set_attribute("app.cost_usd", price(r.model, r.usage))
trace_id = format(root.get_span_context().trace_id, "032x")
return r.choices[0].message.content, trace_id # UI sends feedback with it
# Export over OTLP to Langfuse, Phoenix or your APM; auto-instrumentors
# (OpenLLMetry, OpenInference) can set the gen_ai.* attributes for you.Real example
A support bot's thumbs-down rate jumps from ~4% to ~11% on a Tuesday while latency and error dashboards stay flat. Filtering traces by negative feedback shows the retrieval spans returning articles from a retired help center that a Monday index job re-added. Replaying 50 bad traces with those docs filtered out fixes 46. The 50 traces go into the eval set, so CI catches the regression next time. Root cause took about an hour because the payloads were there to read.
Trade-offs +3 · −3
+ You gain
- Debug by reading what the model actually saw instead of guessing.
- Cost and latency attributed per feature, prompt version, model and tenant.
- Production traces become eval cases and regression tests.
− You pay
- Payloads are heavy: a 20k-token RAG prompt is ~80 KB per span before outputs.
- Stored prompts are a PII and compliance surface: retention, access control, deletion requests.
- GenAI semantic conventions are still evolving; attribute names shift between versions.
Use it when
From the first production user. It is cheapest to add before launch, and it feeds evals, cost control and incident response.
Avoid it when
Never skip it; scale it down. For huge, low-risk volume keep metrics on everything and sample payloads hard; under strict privacy rules log hashes and lengths instead of text.
Numbers to know 4
- OTel GenAI core attributes: gen_ai.request.model, gen_ai.usage.input_tokens, gen_ai.usage.output_tokens. Capturing message content is opt-in.
- Rule of thumb for sampling: metrics on 100% of requests; full payloads on all errors and negative feedback plus ~1–10% of the rest.
- Rule of thumb: ~4 characters per English token, so payload storage is ~4 bytes × tokens per span.
- Four dashboards: cost (per team, feature, model), latency (TTFT and total at p50/p95/p99), errors (429, 5xx, timeouts, refusals), quality (feedback, online eval scores).
Common pitfalls 4
- Logging only the final answer: without retrieved docs and prompt version, a trace explains nothing.
- Raw PII and secrets in traces with the same retention and access as ordinary app logs.
- No trace id on the feedback event, so a thumbs-down can never be investigated.
- Watching averages: mean cost and latency hide the long tail. Track p95/p99 and per-tenant breakdowns.
Interview questions 3
Answer out loud first, then reveal.
What do you log for each LLM request, and why?Show answer
Enough to replay it and price it. A trace per request with a span per step: retrieval with doc ids and scores, each model call with template and version, rendered messages, model and params, tool calls with arguments and results, then tokens including cached, TTFT, total latency, cost and finish reason. Plus user, tenant and session ids so feedback and cost roll up. I emit it with the OpenTelemetry GenAI conventions so the backend is swappable: Langfuse or Phoenix today, my APM vendor tomorrow.
Legal says prompts contain customer PII. Do you stop logging them?Show answer
No, I change how I log them, because without payloads I can't debug quality. I redact at the collector before export, with patterns plus an NER tool like Presidio, and pseudonymize user ids. I split storage: metadata and metrics on normal retention, payloads in a restricted store with short retention, say 30 days, and audited access. I sample payloads: all errors and thumbs-down, a few percent of the rest. For the strictest tenants I log hashes and lengths only.
How is LLM observability different from regular APM?Show answer
APM answers whether the system is up and fast, and treats the call as a black box that returned 200. Most LLM failures also return 200: a hallucination, a wrong tool, a prompt that doubled tokens. So I need the content, meaning inputs, retrieved context and outputs, plus signals APM lacks: feedback, online eval scores, cost per request. I still use the same OpenTelemetry plumbing and trace ids, so the LLM span sits inside the normal distributed trace next to the database call.