Skip to main content
Intermediatehow it works Verified ~8 min

What problem does RAG solve? Walk through a RAG pipeline end to end.

Source: Lewis et al. 2020: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks

Quick answer

A model does not know private or recent data and can answer confidently anyway. RAG retrieves relevant text at question time and puts it in the prompt, so the answer is grounded and can cite sources.

A model does not know private or recent data and can answer confidently anyway. RAG retrieves relevant text at question time and puts it in the prompt, so the answer is grounded and can cite sources. Offline: load documents, chunk them, embed the chunks and index them, keyword and vector. Online: embed the question, retrieve candidates with hybrid search, rerank, build a prompt with instructions and the top chunks, generate an answer with citations, and log the run for evaluation.

Animated explanation
  1. 1Questionfrom the user
  2. 2Retrievevector + keyword, top 50
  3. 3Fuse + rerankkeep the best 5
  4. 4Promptrules + chunks + question
  5. 5Answergrounded, with citations
  6. 6Logtrace for evaluation
The online half of RAG. The offline half (chunk, embed, index) decides how good step 2 can be.
See the concept: Next-token predictionAn LLM writes one token at a time
Next-token prediction

Data pipelines must be idempotent so that a rerun never doubles revenue

Done: the model emitted an end token.

The model never writes a whole sentence at once. It predicts one token, appends it, and predicts again.

Key points

  • private and recent data
  • index path
  • query path
  • citations
  • evaluation

Common mistakes

  • Shipping with no evaluation set, so nobody knows whether retrieval or generation is failing.
  • Letting the index go stale: documents change or are deleted, but the old chunks are still retrieved and cited.
  • Making chunks too big, so the right sentence is buried in noise and the prompt fills with irrelevant text.
  • Making chunks too small, so the answer is split across chunks and no single chunk contains it.
  • Using vector search only, which often misses exact terms such as error codes, ids and product names.
  • Not telling the model to answer only from the context, so it fills gaps from memory.

Best practices

  • Build an evaluation set of real questions first, and measure retrieval and generation separately.
  • Chunk along the document structure (headings, paragraphs) and keep metadata such as source, date and access rights.
  • Use hybrid search (keyword plus vector) and a reranker, then send only the best few chunks.
  • Re-index when documents change, and apply access control at retrieval time so users only see what they may read.
  • Return citations and log every run (question, chunks, answer) so failures can be debugged.

Likely follow-ups

  • Why not just use a long context window instead of RAG?
  • How do you choose chunk size and overlap?
  • How do you evaluate a RAG system?
  • How do you tell a retrieval failure from a generation failure?
  • How do you keep the index fresh and respect document permissions?

References

Discussion