LLM Observability & Tracing
Read a little, play a little. No scary maths, and no rush.
You can't set a breakpoint inside a model
Traditional debugging steps through code line by line. An LLM call is a black box between input and output, so observability has to work from the outside: capture every prompt, every tool call, every retry, and every token/cost/latency number around that black box, then look for patterns across many requests instead of stepping through one.
Traces and spans
A trace is everything that happened for one user request. A span is one step inside it — one model call, one tool invocation, one retrieval. Nest them: a RAG answer might be one trace containing a retrieval span, a rerank span, and a generation span. Without this structure, "the agent gave a weird answer" has no path back to which step went wrong — the lookup, the ranking, or the final generation.
What's worth logging on every call
Prompt and completion text (redact secrets, not substance — you need to read it later), token counts and cost, latency, model/version, and any tool calls with their arguments and results. Model/version matters more than it looks: a silent provider-side model swap can shift behavior with no code change on your side, and without a logged version you can't tell "we broke something" from "they changed something."
The failure mode this catches
Most LLM failures aren't crashes — they're confident wrong answers that return a clean 200. Uptime dashboards miss these entirely. Pairing traces with a small sample of human-reviewed outputs (or an LLM-as-judge pass) is how teams catch quality regressions that no error rate would ever surface.
A worked example: following one bad answer
A customer says the assistant told them the return window is 14 days. The real policy is 30.
- Find the trace. The support ticket includes the request ID shown in the chat, which leads straight to the trace.
- Read the spans in order. The retrieval span shows the top chunk came from a policy page last updated two years ago. The current page ranked fourth.
- Name the cause. The model did exactly what it was told: it answered from the best-ranked context. The real problem was a stale document still in the index.
- Fix and guard. Remove the outdated page, re-index, and add this question to the golden eval set so it can never quietly come back.
Without the trace, the team would likely have rewritten the prompt, which would have changed nothing.
Sampling and retention
Logging the full text of every prompt gets expensive at scale and holds more personal data than you need. A common balance: keep metadata (IDs, model version, tokens, cost, latency, outcome) for every request; keep full text for a sample, plus every request that errored, was flagged, or got a thumbs-down. Delete raw text after a fixed retention period.
Tools without lock-in
You can start with structured JSON logs that share a trace ID across every step. When you outgrow that, OpenTelemetry offers conventions for recording model calls as spans, and dedicated LLM observability tools add views for prompts, costs, and evals. Instrumenting with an open standard keeps you free to switch.
Remember this
- Observability replaces breakpoints: capture everything around the black box.
- Traces nest spans — one user request can be many model/tool calls deep.
- Log model/version too — the model underneath you can change without your code changing.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
End of Ship to Production
Take the section checkpoint
A short mixed quiz on all 4 chapters, to check you are ready to move on.
Related chapters
Spotted a mistake or want a topic covered? Report an issue