RAG in Production
RAG that survives contact with real users
Shipping RAG means measuring retrieval and groundedness, controlling latency (caching, re-rankers, hybrid search), handling chunk updates, and building evaluation sets that catch regressions before users do.
What you will learn
- Build eval sets and metrics for retrieval quality
- Measure groundedness/faithfulness of answers
- Manage latency, caching and chunk-level freshness
What is this?
Production RAG is a system with measurable components: retrieval precision (are the right chunks in the top-k?), groundedness (does the answer cite only the context?), and latency/cost (sub-second at your QPS). You need a labeled eval set of queries→expected chunks and queries→expected answers, plus pipelines that compute retrieval hit-rate@k and LLM-judged faithfulness on every corpus or prompt change. Freshness: chunk upsert/delete on doc change, plus versioned indexes for rollback.
Why do you need it?
- Quality user-reported bugs are too late; eval catches regressions pre-launch.
- Latency budget decides architecture: re-rankers, cached chunks, embedding choice.
- Freshness bugs (deleted docs still cited) erode trust and may breach compliance.
- LLM-judged groundedness is the cheapest scalable hallucination detector.
Mental model
A support queue with QA gates between every handoff
Production RAG pipelines
Every retrieved document and every model answer passes a check — 'is this the right doc?' then 'is this answer faithful to the doc?' — like a QA stamp on each hand-off. Stamps are cheap; rework and angry users are not.
How it works
- Eval set: 100-500 real queries with golden chunk-ids and golden brief answers.
- Retrieval metric: hit-rate@k (gold chunk in top-k) and MRR; hybrid BM25+vector usually lifts hit-rate.
- Re-rank: after ANN top-50, a cross-encoder scores the final top-5 — big quality-per-latency win.
- Groundedness: have an LLM judge whether each claim in the answer is supported by the cited chunk.
- Caching: dedupe embeddings and cache exact/prompt-keyed responses; LRU by recency.
- Freshness: content hash → upsert/delete; run low-traffic index versions and hot-swap.
Production RAG with gates and measurement.
In production
- Always-on support/customer assistants with SLA and audit trails.
- Compliance-sensitive Q&A where every answer must trace to a source doc.
- High-QPS public search over docs where retrieval is the latency bottleneck.
Best practices
- Automate the eval: run on CI at corpus and prompt changes; block on hit-rate/faithfulness regressions.
- Instrument every span: chunk ids, scores, model, temperature, cache hit — the audit trail.
- Set retrieval threshold: below it reply with a fallback rather than a hallucinated pass.
- Cache aggressively (same-question repeat is common in support) and shard re-rankers by query type.
Common mistakes
- Eval-ing only answer 'niceness' and never retrieval precision — retrieval bugs hide.
- No freshness story: edited docs keep being cited from the stale index.
- Single-model single-prompt config with no regression hooks at all.
- Ignoring tail latency — one slow re-rank fan-out collapses p99 under load.
Common issues
hit-rate@k acceptable but answers still wrongGrounding gap — model ignores provided chunks
Harden the prompt, lower temperature, add a faithfulness gate that rewrites/declines non-grounded claims.
p99 latency spikes under burstRe-ranker or decoding bottleneck fan-out
Cache common queries, cap top-k, batch re-rank, and stream the decode for first-token latency.
Retrieved chunks cite deleted contentIndex not upsert/delete-synced with the source
Event-driven index reconciliation plus content-hash dedupe and a delete sweep on a schedule.
Debugging tips
- Attach chunk-ids to every answer span so any user complaint maps to retrieved context.
- Compute hit-rate@k per query-cluster; a dip in one cluster localizes the failing part of the corpus.
- Shoot the same query through ANN vs hybrid vs re-ranked to see where the leak comes from.
- Rotate a few real support tickets manually each week to ground automated metrics in reality.
Interview checkpoint
Hands-on lab
- Compute hit-rate@k on a labeled mini-corpus
- Implement a claim-level groundedness check
- Build a latency budget sketch for QPS vs p95
Score a RAG Pipeline
35 min · advanced
Quick quiz
A faithfulness/groundedness check verifies:
RAG indexes must be refreshed when source documents change.