How RAG works
Retrieval-augmented generation lets a language model answer from your own documents instead of from memory. Scroll to follow a question from the documents to a cited answer.
- 1
Split the documents
Long documents are cut into short chunks, a few paragraphs each, so the system can retrieve just the part that answers a question.
- 2
Turn chunks into vectors
An embedding model turns each chunk into a list of numbers. Chunks with similar meaning land close together in this space.
- 3
Embed the question
The question goes through the same embedding model, so it lands in the same space as the chunks.
- 4
Find the nearest chunks
An approximate nearest-neighbor search returns the chunks closest to the question. It's fast, but closeness in embedding space is only a rough guess at relevance.
- 5
Rerank
A reranker, usually a cross-encoder, reads the question and each candidate together and scores how well it actually answers. The order changes.
- 6
Answer with sources
The top chunks go into the prompt. The model answers from them and cites the chunk behind each claim.
Split the documents
Long documents are cut into short chunks, a few paragraphs each, so the system can retrieve just the part that answers a question.
Turn chunks into vectors
An embedding model turns each chunk into a list of numbers. Chunks with similar meaning land close together in this space.
Embed the question
The question goes through the same embedding model, so it lands in the same space as the chunks.
Find the nearest chunks
An approximate nearest-neighbor search returns the chunks closest to the question. It's fast, but closeness in embedding space is only a rough guess at relevance.
Rerank
A reranker, usually a cross-encoder, reads the question and each candidate together and scores how well it actually answers. The order changes.
Answer with sources
The top chunks go into the prompt. The model answers from them and cites the chunk behind each claim.
In short
- RAG keeps answers grounded in documents you control, and you can update them without retraining the model (Lewis et al., 2020).
- Retrieval usually decides answer quality: chunking, the embedding model and reranking tend to matter more than prompt wording.
- Hybrid search, keyword matching (BM25) plus vectors, catches exact terms like product codes that embeddings miss.
- Evaluate retrieval and generation separately: was the right chunk retrieved, and did the answer stay faithful to it?
References
- Lewis et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. NeurIPS.
- Robertson & Zaragoza (2009). The Probabilistic Relevance Framework: BM25 and Beyond. Foundations and Trends in IR.
- Nogueira & Cho (2019). Passage Re-ranking with BERT.
- Karpukhin et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. EMNLP.
Share
Keep reading
All writingHow embeddings and vector search work
ExplainerHow meaning becomes coordinates, and how a search finds the closest ones fast.
5 stepsHow to evaluate an LLM app
ExplainerTest cases, scoring, version comparisons and a release gate: measuring quality instead of guessing.
5 stepsHow AI agents work
ExplainerAn agent is a language model in a loop: plan, call a tool, read the result, decide again.