What is RAG (retrieval-augmented generation)?
A way to answer from your own documents. A search finds the passages that match the question, and they are added to the prompt before the model writes.
intermediate · Also called Retrieval-Augmented Generation
A model knows only what was in its training text, up to its knowledge cutoffKnowledge CutoffThe date a model's training text ends. The model knows nothing after it unless it is given new text, for example by a web search.. It has never seen your company's handbook, last month's tickets or this morning's price list. RAG, short for retrieval-augmented generation, is the usual way to give it that knowledge at the moment it answers, without training it again.
The name comes from a 2020 research paper by Patrick Lewis and colleagues. It paired a language model with a searchable index of Wikipedia, and the answers came out more specific and more factual than from the model alone.
How it works
The work happens in 2 stages.
Before any question. Your documents are split into small chunks. An EmbeddingEmbeddingA list of numbers that stands for the meaning of a piece of text, an image or other data. Things with similar meaning get similar numbers, so a computer can find them. model turns each chunk into a list of numbers that stands for its meaning, and the lists go into a vector databaseVector DatabaseA database built to store embeddings and quickly find the ones closest to a query, which is how AI apps search by meaning..
When a question comes in. The question is turned into numbers the same way. The database finds the chunks closest to it in meaning. Those chunks are pasted into the prompt next to the question, and the model writes its answer from them.
The model itself is unchanged. It is reading an open book that someone handed it a moment ago.
Where it goes wrong
The answer can only be as good as the passages the search found. A chunk cut from the middle of a document can lose the thing that makes it findable, such as which product or which year it is about. Anthropic found that adding a line of context to each chunk before indexing cut failed searches by 49%, and by 67% when a second step re-ranked the results.
When the search misses, the model may still answer, from memory, with confidence. So a good RAG app shows its sources, and you should click them.
When you do not need it
If all your documents fit comfortably in the context windowContext WindowEverything a model can see at once while it writes an answer, counted in tokens. When the window is full, something has to be left out., you can skip the search and give the model everything. Anthropic's rule of thumb is under about 200,000 tokens. RAG earns its keep when the library is far bigger than any prompt.
Sources