Observing & Debugging RAG
From 'wrong answer' to root cause
When a RAG answer is wrong, the bug lives in one of three stages — corpus, retrieval, generation. Logging the right artifacts turns a mystery into a diagnosis.
What you will learn
- Log retrieval artifacts per query
- Run a three-way diagnosis tree
- Inspect chunk-level traces
What is this?
A wrong RAG answer is a symptom. The cause is usually corpus gaps, retrieval failure, or generation letting context drift. Observability records each stage's actual payloads — chunks retrieved, scores, final prompt, output — so any bad answer can be replayed and triaged to a stage instead of argued about.
Why do you need it?
- Debugging without artifacts is guessing with extra steps.
- The three-stage split pinpoints optimal improvement automatically.
- Replayed traces turn support complaints into reproducible tickets.
- Regressions become watchable (stage metrics) rather than vibes.
Mental model
An airplane's flight recorder
RAG observability
After a rough landing, investigators replay the instrument tape: throttle (retrieval), altitude (context), engine (generation). They don't argue about who's guilty — they read data from each stage and fix precisely that system.
How it works
- Attach a trace id per user question; prop it through ingestion and retrieval.
- Log: query text, retrieved chunks (ids+scores), rerank output, final prompt tokens, output, finish reason.
- Compute stage metrics in the trace: retrieval coverage, chunk scores, citation containment.
- Stream traces to a dashboard with sampling for offline analysis.
- Diagnosis tree: corpus? (content missing) → retrieval? (right chunks absent) → generation? (ignored context).
Trace anatomy.
In production
- Support tickets escalate with a trace link — triage in minutes, not arguments.
- Sample 1-in-100 traces for offline eval enrichment.
- Stage dashboards alarm when retrieval coverage or groundedness ratio drops.
Best practices
- Log chunk ids AND scores AND source ids — you need all three.
- Capture the assembled prompt (or its diff) for debugging.
- Keep traces sampled but deep; full detail is expensive at scale.
- Build the diagnosis tree into the dashboard: click fail → see stage.
Common mistakes
- Logging only the final answer — the least useful artifact.
- No trace id, so complaints can't be replayed.
- Query-level stats without chunk-level views.
- Debugging in prod without a sampled replay environment.
Common issues
Repro requires asking the user againNo trace id/artifacts persisted
Add trace ids and persist retrieval + prompt on failure paths.
Traces are too big to inspectFull-detail logging at 100% sampling
Sample deeply (e.g. 2%, plus 100% on errors).
Debugging tips
- Retrieved chunks present but answer wrong → generation stage; check prompts.
- Right chunks absent → retrieval or corpus coverage; check scores/limits.
- Retrieved noise dominates prompt → rerank/threshold too loose.
Interview checkpoint
Hands-on lab
- Log stages under one trace id
- Replay a trace to the diagnosis tree
- Root-cause a failure to a stage
Replay and Triage a Bad RAG Answer
40 min · advanced
Quick quiz
The first thing to log per RAG query is…
A trace id turns a support complaint into a replayable ticket.