What is RAG (retrieval-augmented generation)?
ExplainerBy Lucas Matías García TorresUpdated (first published )Data checked 4 min read
Reviewed by Lucas Matías García Torres on . How we work.

Short answer
RAG, or retrieval-augmented generation, looks up relevant passages in an outside knowledge base and adds them to the prompt before a model answers. It avoids retraining a model each time your information changes and lets readers check sources. It does not stop errors: retrieval can fail, and Anthropic reports a 5.7% baseline failure rate on its own tests, cut to 1.9% with contextual retrieval, BM25 and reranking.
Prices and limits change often: check the vendor's page before you pay. How we make money.
What is RAG? RAG, short for retrieval-augmented generation, is a technique where a system looks up relevant information in an outside knowledge base and adds it to the prompt before a language model answers. Amazon Web Services describes it as optimizing the output of a model "so it references an authoritative knowledge base outside of its training data sources before generating a response". This guide explains what RAG is, how it works step by step, when it is worth building and when you can skip it, using the primary sources listed at the end (checked October 6, 2026).
What does RAG mean, and where does it come from?
The name comes from a 2020 research paper, "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks" (Patrick Lewis and colleagues, arXiv:2005.11401). The authors describe RAG as models "which combine pre-trained parametric and non-parametric memory for language generation": the parametric memory is what the model learned in training, and the non-parametric memory is an outside store it can consult. Today the word usually means the simpler product pattern: search first, then ask the model with the results in the prompt.
How does RAG work?
AWS lists four steps, which most RAG systems follow in some form:
- Create the external data. Documents are converted into numerical representations (embeddings) and stored in a vector database.
- Retrieve the relevant information. The user's question is converted into a vector and matched against the stored vectors.
- Augment the prompt. The retrieved passages are added to the prompt, with instructions on how to use them.
- Keep the data current. The documents and their embeddings are updated on a schedule or in batches.
The model then answers using the passages in front of it. Because those passages can be shown to the reader, a RAG system can cite where an answer came from, which AWS lists as one of its main benefits for trust.
Table: what each stage of a RAG pipeline does
| Stage | What happens | What can go wrong |
|---|---|---|
| Chunking | Documents are split into passages | A chunk loses the context that explains it |
| Embedding | Each chunk becomes a vector | Exact names and codes may not match semantically |
| Retrieval | The top matches for the question are found | The right passage is not among them |
| Prompting | The passages go into the prompt with the question | Too much text dilutes the answer |
| Generation | The model writes the answer | It can still misread or ignore a passage |
What problem does RAG solve?
A model only knows what was in its training data and what is in the prompt. RAG addresses three limits, according to AWS: it avoids retraining a model every time your information changes, it can connect the model to current sources, and it lets users verify answers through source attribution. It also keeps private documents out of the model's weights, because they live in a store you control.
It does not make a model infallible. Retrieval can fail: Anthropic measured a baseline failure rate of 5.7% on its own test set, meaning that in those tests the right passage was missing from the top results about one time in eighteen. Whatever is retrieved is only as good as the documents behind it, and even with good passages a model can state something they do not say, which is why grounding answers in quotes and citations is one of the mitigations in our guide to model hallucinations.
Do you need RAG or can you paste everything into the prompt?
Anthropic's guidance is to skip RAG when the knowledge base is small: if it is smaller than about 200,000 tokens (roughly 500 pages), include it directly in the prompt. Today's windows are much larger, with 1M tokens listed for current Claude models and 1,050,000 for GPT-6.1 Sol, but a bigger window does not remove two problems: you pay for every token you send, and vendors warn that accuracy can fall as the window fills. Our guide to the context window covers the numbers and the costs.
A simple rule: if all your material fits comfortably in one prompt and changes rarely, try the prompt first. If it is large, changes often or must be cited, use retrieval.
How can you make RAG better?
Anthropic's "contextual retrieval" work gives measured ideas. Traditional chunking "often destroys context", so it prepends an explanation of where each chunk comes from before embedding it. Combined with a lexical ranking function called BM25, which is good at exact words, codes and identifiers, and with a reranking step, it reports fewer failed retrievals than the baseline:
Table: Anthropic's reported results on its test set
| Technique | Failed retrievals | Reduction |
|---|---|---|
| Baseline embeddings | 5.7% | none |
| Contextual embeddings | 3.7% | 35% |
| Contextual embeddings plus contextual BM25 | 2.9% | 49% |
| Plus reranking | 1.9% | 67% |
These are Anthropic's figures on its own data. Your documents will behave differently, so measure your own retrieval: write a list of real questions, note which passage should answer each, and check how often it is retrieved before you judge the model's answers.
RAG, fine-tuning and agents
RAG puts knowledge into the prompt at question time. Fine-tuning changes the model itself, which suits a fixed style or task rather than facts that change. They are not alternatives for every case, and many systems use both: our guide to RAG vs fine-tuning gives a decision guide. An agent can use retrieval as one of its tools, and protocols such as MCP standardize how a model connects to a document store or database.
What to check before you build one
- Which documents go in, and who is allowed to see them: a RAG store can expose anything you index.
- How the knowledge is updated, because stale embeddings give stale answers.
- Whether answers show their sources, so a reader can check them.
- A test set of real questions, to measure retrieval separately from generation.
- The cost of the extra tokens, which you can estimate with our API cost calculator.
What we checked
- The 2020 paper Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (Patrick Lewis and colleagues, arXiv:2005.11401) explores RAG as models that combine pre-trained parametric and non-parametric memory for language generation. (arXiv, )
- AWS defines RAG as optimizing a model's output so it references an authoritative knowledge base outside its training data before generating a response, in four steps: create external data in a vector database, retrieve relevant information, augment the prompt and update external data; benefits listed are avoiding retraining, access to current information, source attribution and developer control. (Amazon Web Services, )
- Anthropic defines RAG as retrieving relevant information from a knowledge base and appending it to the user's prompt; its contextual retrieval work says traditional chunking often destroys context and reports failed retrievals falling from 5.7% to 3.7% (contextual embeddings, a 35% reduction), 2.9% (with contextual BM25, 49%) and 1.9% (with reranking, 67%) on its test set. (Anthropic, )
- Anthropic advises skipping RAG and including the knowledge base directly in the prompt if it is smaller than about 200,000 tokens (roughly 500 pages). (Anthropic, )
- Current Claude models list a 1M-token context window, and accuracy and recall can degrade as the token count grows. (Anthropic, )
- GPT-6.1 Sol has a 1,050,000-token context window. (OpenAI, )
What may change
- Retrieval methods and vendor guidance change as context windows grow; this page is reviewed yearly.
- The figures from Anthropic come from its own test set and will differ on your documents.
Frequently asked questions
What does RAG stand for?
RAG stands for retrieval-augmented generation. The term comes from a 2020 research paper by Patrick Lewis and colleagues, which describes models that combine pre-trained parametric memory (what the model learned) with non-parametric memory (an outside store it can look up) to generate language.
What is RAG in simple terms?
It is search plus a language model. When you ask a question, the system first finds the most relevant passages in your documents, then puts them into the prompt so the model answers from them instead of relying only on what it learned in training. AWS describes this as referencing an authoritative knowledge base before generating a response.
How does RAG work step by step?
AWS lists four steps: convert your documents into embeddings stored in a vector database, convert the user's question into a vector and match it against them, add the retrieved passages to the prompt, and keep the documents and embeddings updated. The model then answers using the passages in its context window.
Is RAG better than fine-tuning?
They solve different problems. RAG adds knowledge at question time and avoids retraining the model whenever your information changes, which AWS lists as a benefit. Fine-tuning changes the model itself and suits a fixed style or task. Many systems use both, so the choice depends on whether your problem is missing knowledge or missing behavior.
Do I need a vector database for RAG?
AWS describes storing embeddings in a vector database, but RAG is not tied to one. Anthropic combines embeddings with BM25, a lexical ranking function, and for a knowledge base under about 200,000 tokens (roughly 500 pages) it suggests skipping RAG and putting the material directly in the prompt.
Does RAG stop hallucinations?
No. It gives the model sources to answer from and lets readers check them, but retrieval can fail and a model can still misread a passage. Anthropic's baseline retrieval failure rate on its own test set was 5.7%, and its best combination of techniques lowered it to 1.9%.
Sources
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (Lewis et al., 2020), arXiv. Accessed October 6, 2026.
- What is RAG (Retrieval-Augmented Generation)?, Amazon Web Services. Accessed October 6, 2026.
- Introducing Contextual Retrieval, Anthropic. Accessed October 6, 2026.
- Context windows (Claude API): sizes by model and thinking tokens, Anthropic. Accessed October 6, 2026.
- GPT-6.1 Sol model page: context window, cached input and long-context pricing, OpenAI. Accessed October 6, 2026.
Spotted an error or an outdated price? Tell us and we will fix it.
Change history
- : Wording revised; no figure, date or source changed.
- : First published from the original RAG paper and AWS and Anthropic documentation.
Next review: .
Part of our Language Models Explained guide.
Keep reading
- What is a context window in a language model?
What a context window is, how big it is for Claude, GPT and Gemini in 2026, what happens when it fills up, why bigger is not always better and what it costs.
- What is a token in a language model?
What a token is in a language model, how many words it equals, why Spanish and French need more tokens than English (we measured it) and how they are billed.
- What is an agent?
What an agent is, how it differs from a chatbot or a workflow, how the plan-act-observe loop works, real examples, when you do not need one and the risks.
- What is MCP (Model Context Protocol)?
What the Model Context Protocol is, how hosts, clients and servers work, what tools, resources and prompts are, and the security rules the MCP spec sets.
- LLM API pricing compared, with a cost calculator
API prices for OpenAI, Anthropic, Google and DeepSeek models per million tokens, plus a calculator with caching, batch discounts and tokenizer differences.
Other topics

Assistants & Chatbots
16 guides

LLM APIs & Costs
11 guides

Agents & Automation
8 guides

Local Models & Hardware
11 guides