Single logo
  • Courses
  • Blog
  • About
Join free on SkoolJoin free on Skool

Learning path · 15 steps

AI System Design: Reliable, Fast and Affordable LLM Apps
  1. 1
    Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
  2. 2
    Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
  3. 3
    Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
  4. 4
    A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
  5. 5
    Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels
  6. 6
    Semantic Caching: Why a Similarity Threshold Can't Stop Confident Wrong Answers
  7. 7
    LLM Model Routing: Rules, a Classifier or a Cascade, Judged on Cost per Win and Every Topic
  8. 8
    Observability and Tracing for LLM Apps: A Span per Stage, Redaction First, Bad Traces Become Tests
  9. 9
    Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success
  10. 10
    Guardrails for an LLM Agent: Check What Comes In, What It Wants to Do, and What Goes Out
  11. 11
    Embedding Migration With Zero Downtime: Two Indexes, a Dual-Write Backfill, a Shadow Check and a Flag
  12. 12
    Design a Multi-Tenant RAG Assistant, Part 1: Requirements, Rough Numbers and a Contract
  13. 13
    Design a Multi-Tenant RAG Assistant, Part 2: Tenant Isolation, a Scoped Cache and Walking an Outage
  14. 14
    A 500 MB PDF Takes 50 s to Answer, Get It to 2 s: Measure First, Then Promise Per Path
  15. 15
    A 500 MB PDF From 50 s to 2 s, the Fast Path: Queue, Status, Streaming and an Acceptance Test That Fails
  1. Blog
  2. /
  3. AI Engineering
  4. /
  5. AI System Design

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.

Chaand Sheikh

·27 November 2026AI System Design

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.

The wrong room, span by span 1 Retrieval found last month's room list 2 Model call read that list, picked room 4 3 Tool call book room 4: succeeded 4 Guardrail nothing unsafe: passed Nothing failed and every stage was happy. The bug sits in the first span. Plain logs would blame the model.
For illustration: a made-up booking agent. The trace points at retrieval, not the model.

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 versus tail sampling Head sampling decides at the start keep 1 in 10 (for illustration) 6 slow calls in 200 (simulated) 1 in 10 of 6 = 0.6 kept cheap, simple, blind Tail sampling decides at the end keep errors and slow ones, plus a small share of normal holds every span until done costs memory and a collector
Head is cheap and blind. Tail costs more and keeps what you came for.

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.
LevelWhat it sounds like
Weak"We'd add logging and a dashboard."
GoodA span per stage, with tokens, cache and queue time, and redaction before storing.
StrongAlso 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

IdeaRemember
Span every stageOne missing stage hides the real cause (a 900 ms tool inside 2,462 ms, simulated)
Redact firstIn the app, before storing; test the redaction too
Sample by outcomeTail sampling keeps the errors and slow requests
Close the loopA 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.

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in the learning path AI System Design: Reliable, Fast and Affordable LLM Apps · step 8 of 15

Previous
LLM Model Routing: Rules, a Classifier or a Cascade, Judged on Cost per Win and Every Topic
Next post
Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success

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.

On this page

  • A flight data recorder for one request
  • The wrong room, span by span
  • What each span records
  • Redact before you store
  • Head or tail sampling
  • A bad trace becomes a test
  • The follow-ups
  • Recap
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.