Stage 4 · Assumes you can code · Production RAG

Production RAG

Rewrite, retrieve for recall, rerank, cite — and measure the retrieval miss, not just the prose.

The earlier RAG lesson is the idea: fetch chunks, stuff them in the prompt. Production RAG is the pipeline around that idea, because users do not query the way your documents are titled, and a fluent answer can hide a retrieval miss.

Typical stages: rewrite the query (or generate a hypothetical answer to embed), retrieve a wide set with hybrid search (high recall), rerank a shortlist with a cross-encoder, generate only from that shortlist, and cite chunk IDs so faithfulness evals have a target.

If the right paragraph never made the shortlist, the generator will still speak. Logging "the answer sounded good" is not a retrieval metric. Track recall@k (was the gold chunk in the top k?) separately from answer quality.

Production RAG Is a Pipeline
"how late can I take vacation?"
Users do not speak in document headings. Literal embedding of this query may miss a policy titled "PTO advance notice."

Step through rewrite → retrieve → rerank → generate. Most quality lives before the LLM sees the prompt.

Worked example. User: "how late can I take vacation?" The policy heading is "PTO advance notice: 5 business days."

Embed the raw query. Top hits: holiday calendar, sick-leave notice, HR portal how-to. The PTO chunk is rank 18. You keep top 5. The model answers "most companies want two weeks" — hallucination from the first RAG lesson, but the root cause is recall, not sampling.

Rewrite to "paid time off request deadline business days policy," hybrid retrieve 20, rerank to 4. The PTO chunk is first. The answer cites Policy §4. A faithfulness check can pass or fail on the citation; without the chunk, it was doomed.

Fluent Answers Hide Retrieval Misses
"how late can I take vacation?"
Company holidays 2026
Sick-leave notice period
How to use the HR portal
The generator still answers — often with a generic "two weeks." Answer accuracy fails because recall@k failed. Log retrieval hits, not just the final paragraph.

Same question: top-5 miss versus rewrite + rerank putting the policy on the shortlist.

  • Lost-in-the-middle: even with a huge window, stuffing 40 chunks is worse than 4 reranked ones (see Context Engineering).
  • Access control belongs in retrieval (filter by tenant), not in hoping the model ignores other customers' docs.
  • Prompt injection via retrieved PDFs is still in play — treat chunks as untrusted data.
💡

Debug RAG from the index outward: if recall@k is bad, do not swap the chat model. If recall is fine and answers still drift, then look at prompting, citations, and the generator.

Check your understanding
1. Why can a RAG answer be fluent and still be a retrieval failure?
2. In the vacation/PTO example, what actually put the right policy in the prompt?
All lessons in Stage 4: Builders (optional)