Retrieval-augmented generation (RAG)
How a language model answers from documents it was never trained on: cut them into chunks, embed them, retrieve the closest, add them to the prompt, and answer with citations.
In 30 seconds
A language model only knows what it saw in training. Ask it about your own documents and it will guess. Retrieval-augmented generation (RAG) fixes that by looking things up first.
Documents are cut into short passages, and each is turned into a list of numbers that captures its meaning. When a question arrives, the passages closest in meaning are fetched and pasted into the prompt.
The model then answers from those passages and cites them, so a reader can check. If the wrong passages are fetched, the answer goes wrong too.
Retrieval-augmented generation (RAG), step by step
The two-number vectors are made up so the picture stays flat; real embeddings have hundreds or thousands of numbers. Many systems also combine this with keyword search and re-rank the results.
- Step 1: A model answering alone. A language model answers from what it absorbed in training. Ask about a store’s own return policy, a document it never saw, and it fills the gap with what is typical. The answer sounds confident and is wrong: this store gives laptops 14 days, not 30. RAG is the fix: look the answer up first.
- Step 2: Cut documents into chunks. Preparation happens once, before any question. Each document is cut into short passages called chunks, often a paragraph or a few hundred words. A chunk should be small enough to be about one thing, and complete enough to make sense when read on its own, because later it will be pulled out and quoted alone.
- Step 3: Turn each chunk into numbers. An embedding model turns each chunk into a vector, a list of numbers that places the text on a map of meaning. Texts about similar things point in similar directions: the two return rules sit close together, and shipping lies far away. The vectors are stored in an index, ready to be searched.
- Step 4: Retrieve the closest chunks. Now the question arrives. It is embedded by the same model, and the index returns the chunks whose vectors point most nearly the same way. Closeness is scored by cosine similarity: 1 means same direction, 0 means unrelated. The top two, chunks 2 and 1, are retrieved. No keywords had to match, only meaning.
- Step 5: Add them to the prompt. The retrieved chunks are pasted into the prompt as context, together with an instruction to answer only from them and the user’s question. This is the “augmented” part. Nothing about the model changes and nothing is retrained. It simply reads the right passages a moment before it answers.
- Step 6: A grounded answer, cited. The model now generates its answer from the passages in front of it, so the answer is grounded in the store’s real policy and no longer a guess. It also says which chunk it used. That citation lets a reader open the source and check the claim, which is the main reason to trust a RAG answer more than a bare one.
- Step 7: Where it goes wrong. RAG moves the problem rather than removing it. If the search fetches the wrong passages, the model has nothing right to answer from. Chunks cut in the wrong place lose their meaning, outdated sources give outdated answers, and a model can still ignore or misread what it was given. Citations make such errors easier to catch.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?