Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 8 of 15
Observability and Tracing for LLM Apps: A Span per Stage, Redaction First, Bad Traces Become Tests
"The agent booked the wrong meeting room yesterday. Explain why." A span for every stage shows the bug sat in retrieval, a simulated request shows a 900 ms tool span hiding inside 2,462 ms, redaction runs before storage, tail sampling keeps the 6 slow requests in 200, and an incident trace becomes a test that takes the eval set from 9/9 to 9/10.
What you will learn
- Record one trace per request with a span for retrieval, the model call, each tool and each guardrail
- List what a span should carry: stage, time, model, tokens, cache hit and queue wait
- Redact personal data in the app before a trace is stored, and say where simple redaction fails
- Choose head or tail sampling, and turn a bad trace into an eval case
Before you start
- The RAG Pipeline Explained
- Measuring Latency of an ML API with OpenTelemetry and Locust
Here's an interview question that starts with a complaint. A user says: the agent booked the wrong meeting room yesterday. It's today now. The request is gone, and nobody can explain it. So the real question is: what should you have recorded?
The thirty-second answer has four parts. A span for every stage. An agreed list of what each span records. Redaction before anything is stored. And keep the traces that matter, turning the bad ones into test cases. Some examples below are made up for illustration, and some come from a small simulation; each one says which.
A flight data recorder for one request
A flight data recorder runs on every flight. It writes each reading, one after another, on one timeline. Nobody looks at it on a normal day. When something goes wrong, investigators rebuild exactly what happened, in order, instead of guessing. It prevents nothing by itself, but its lessons go back into training.
A trace is that recorder for one request. Each entry on its timeline is a span: one stage, with a start, a duration and some details.
The wrong room, span by span
Here's the trace you'd want for that booking. This one is made up for illustration, not from a run.
Without that first span, you'd have blamed the model. With it, you can see stale data came in at the very start.
What each span records
Now the timing side, from a small simulation. One request takes 2,462 ms, and each stage has its own span:
trace t071, 2462 ms (simulated)
retrieval 77 ms 3%
tool 900 ms 37%
model_ttft 418 ms 17%
model_stream 1050 ms 43%
guardrail 18 ms 1%
The tool takes 900 ms, 37% of the request. Imagine you never added spans for retrieval or the tool. That time would disappear into the model's number, and you'd go off tuning the wrong thing.
So what goes on a span?
- The stage and its time.
- The model name, and input and output tokens.
- Whether the cache hit.
- How long it waited in a queue.
OpenTelemetry has a GenAI convention for these field names, so different tools can read your spans the same way. And every span carries one trace id, so a complaint, a slow request and a bill can all point to the same record. If you've used spans on a plain model API, this is the same idea; see Measuring Latency with OpenTelemetry and Locust.
Redact before you store
Now the incident on the other side: the tracing itself stores customer details. A trace is a copy of what users typed, kept for weeks, and read by more people than the app is.
A request from the simulation, before and after redaction:
raw : my invoice looks wrong, mail sam@example.com
stored : my invoice looks wrong, mail <email>
The address becomes a placeholder, and the invoice words stay, so you can still debug it. Where does this happen? In the app, before the data leaves, not later in the dashboard, because by then it's already written down.
Be honest about the limits. This simple version knows two patterns, email and phone, so a person's name goes straight through. Redaction needs tests of its own.
Head or tail sampling
You usually can't keep every trace. So which ones do you keep?
Head sampling decides at the start of the request. For illustration, keep one in ten. In our simulation, only 6 of 200 requests hit a slow tool call. One in ten of six is 0.6, so on average you'd keep less than one of them.
Tail sampling decides at the end, when you know how it went. You keep the errors and the slow ones, plus a small share of normal ones. The price: you have to hold every span until the request finishes, which takes memory and a collector. Say that trade-off out loud in the interview.
A bad trace becomes a test
This is where the recorder feeds back into training. In the simulation:
router v1, old eval set: 9/9
incident t008: wrong answer, model=small
new case: inc-001
text: my invoice looks wrong, mail <email>
router v1, new eval set: 9/10
router v2, new eval set: 10/10
The test set passes 9 out of 9. Then a user gets a wrong answer, and the trace says the small model handled it. We take that request, already redacted, and add it as a case that must pass. The same setup now scores 9 out of 10, so the test set can see what only users saw before. Change the routing, and it passes 10 out of 10. If that mistake comes back, the test catches it before your users do. For how to keep such a set, see Building a Golden Dataset; the routing side is in Model Routing.
The follow-ups
- Would you store the full prompt? Only after redaction, and store the least you need, because redaction misses what it doesn't know.
- What can't a trace tell you? It shows what happened, not whether the answer was right. You still need evaluation and user feedback beside it.
- The agent called a tool that changed something. Traced differently? Record the tool's name and result, and link the trace id to the change it made, so you can undo it. That's a design answer, not simulated.
| Level | What it sounds like |
|---|---|
| Weak | "We'd add logging and a dashboard." |
| Good | A span per stage, with tokens, cache and queue time, and redaction before storing. |
| Strong | Also explains head and tail sampling, says where redaction fails, and turns incidents into test cases. |
The rubric is a guide for practice, not a measurement.
Recap
| Idea | Remember |
|---|---|
| Span every stage | One missing stage hides the real cause (a 900 ms tool inside 2,462 ms, simulated) |
| Redact first | In the app, before storing; test the redaction too |
| Sample by outcome | Tail sampling keeps the errors and slow requests |
| Close the loop | A bad trace becomes an eval case: 9/9, then 9/10, then 10/10 |
Try it yourself
Think of an AI tool you use. Name the spans you'd want on the day it goes wrong, and which field on each span would tell you the most.
Related course on Udemy
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.