What Is RAG? Retrieval-Augmented Generation Explained
You ask your company's HR chatbot, "How many leave days can I carry over to next year?" A general chatbot has never read your company's policy, so at best it gives a generic answer, and at worst it invents a confident number, the problem you met in Why AI Hallucinates. The HR bot instead replies, "5 days, see section 4.2 of the Leave Policy". So what is RAG, the technique behind that second answer, and how does the bot find section 4.2 before it replies?
Closed book versus open book
A language model normally works closed-book. Everything it "knows" is stored in its weights, which were fixed when training ended (see What Is an LLM?). It has a knowledge cutoff, and it has never seen your private documents: your HR policy, your product manual or last week's meeting notes.
Retrieval-augmented generation, or RAG, gives the model an open book. Before the model answers, the app searches your documents for the passages most related to the question and places them in front of the model, next to the question. The model then answers from those pages rather than from memory alone.
Retrieve, augment, generate
The name lists the three steps in order:
- Retrieve: turn the question into an embedding, search a vector database for the nearest passages and keep the top few, exactly as in What Are Embeddings? and What Is a Vector Database?
- Augment: build a prompt that holds instructions, the retrieved passages and the question. "Augment" means "add to": the prompt is enlarged with facts.
- Generate: send that prompt to the language model, which writes the answer.
Here is what an augmented prompt might look like. It is plain text, and the model sees nothing else:
Answer the question using only the passages below.
If the passages do not contain the answer, say you don't know.
Name the source of each fact.
[1] Leave Policy, section 4.2: Employees may carry over up to
5 unused days of annual leave into the next calendar year.
[2] Leave Policy, section 4.3: Carried-over days expire on 31 March.
Question: How many leave days can I carry over to next year?
The passages live only inside this one request, in the model's context window. The model does not learn or remember them. Ask again tomorrow and the app retrieves them again. This is also why only a handful of passages are sent: they must fit in the window next to the question and the answer.
Preparing the book in advance
Before any question arrives, the documents have to be made searchable. This preparation, often called indexing, runs ahead of time and again whenever the documents change.
First, each document is split into chunks: pieces of a few hundred tokens, often a few paragraphs, sometimes overlapping slightly so a sentence cut at a border is not lost. Chunks matter for two reasons. A whole manual would not fit in the context window, and a single embedding for a 50-page manual would blur dozens of topics into one point on the map.
Next, each chunk is turned into an embedding and stored in a vector database with its text and metadata, such as the file name, page and date. When a policy changes, you update that document's chunks. No model is retrained, which is why RAG is the usual way to keep an assistant up to date.
Why teams use RAG, and what can still go wrong
RAG is popular for three reasons. It gives a model fresh or private knowledge without retraining it. It cuts down made-up answers, because the model works from facts placed in front of it. And it lets an answer point to its sources, so a person can check them. It can respect permissions too: the search only returns documents the person asking is allowed to see.
It is not magic, and each step can fail. Retrieval can miss: the right passage is worded differently, is split across two chunks, or is beaten by a passage that looks similar but answers something else. The model can ignore or misread the passages and mix in its own memory. And the documents themselves can be wrong, outdated or contradictory, in which case RAG faithfully repeats a bad source.
So RAG reduces hallucinations but does not remove them. For anything important, open the cited source and check it. You already meet RAG in daily life: company help bots, "chat with your PDF" tools, customer support assistants and search engines that answer with links to the pages they used.
Check yourself
Does RAG change the model's weights?
No. It only adds passages to the prompt for one request, and the model stays exactly as it was trained.
Why are documents split into chunks instead of embedded whole?
Chunks fit in the context window, and each one gets a sharp embedding for one topic, so the search can return just the relevant part.
The search works, yet the bot quotes last year's leave rules. What went wrong?
The store still holds last year's policy; RAG is only as current as the documents you index, so update the document and index it again.
Go deeper
- RAG 101: Build Your First RAG Pipeline over Runbooks: when you are ready to see code, it builds a working RAG pipeline step by step over real runbooks.