Explainer
What is RAG?
What problem does RAG solve?
A large language model only knows what was in its training data, and that knowledge stops at a cutoff date. It knows nothing about your company's internal policies, last week's product update, or the contract sitting in your shared drive. Ask it anyway and it may guess, producing a confident answer that is simply wrong.
RAG fixes this by giving the model an open-book exam instead of a closed-book one. Rather than relying on memory, the system looks up relevant material at the moment you ask and puts it in front of the model.
The term comes from a 2020 paper by Patrick Lewis and colleagues at Facebook AI Research, University College London, and New York University. They combined a language model with a searchable index of Wikipedia and found that the result produced more specific and factual answers than the model alone, and that its knowledge could be updated by swapping the index rather than retraining.
How does RAG work step by step?
1. Chunking. Your documents are split into smaller passages, often a few hundred tokens each, sometimes with a little overlap so ideas are not cut in half. 2. Embedding. Each chunk is converted into an embedding: a list of numbers that captures its meaning, so passages about similar topics end up with similar numbers. 3. Indexing. Those embeddings are stored in a vector database or search index. Steps 1 to 3 happen ahead of time.
4. Retrieval. When you ask a question, it is embedded the same way, and the system finds the chunks whose embeddings are closest to it. This is vector search, or semantic search. Many systems also run a traditional keyword search and combine the results, because keyword search is better at exact terms like product codes and names. A reranking step may then reorder the top results.
5. Prompt assembly. The best few chunks are pasted into the prompt along with your question and instructions such as “Answer using only the sources below and cite them.” 6. Generation. The model reads that prompt and writes an answer grounded in the retrieved passages, often with citations pointing back to them.
Does RAG stop AI hallucinations?
RAG reduces hallucinations but does not eliminate them. When the right passage is retrieved, the model has the facts in front of it and is much less likely to invent things. Citations also let you click through and check.
But errors still happen. If retrieval fetches the wrong passage, or nothing relevant exists, the model may fill the gap from memory. It can also misread a correct passage, merge details from two documents, or cite a source that does not actually support its claim.
Good systems reduce this by instructing the model to say “I don't know” when the sources do not contain the answer, showing citations prominently, and testing regularly against a set of questions with known answers.
RAG vs fine-tuning vs long context: which should you use?
These three approaches solve different problems. RAG is best when the model needs facts that are large in volume, change often, or must be cited, such as a help centre, a legal archive, or internal wikis. Updating knowledge is as simple as updating the documents.
Fine-tuning means further training a model on your own examples. It is best for teaching a consistent style, format, or specialized behaviour, like always writing in your brand voice or classifying tickets into your categories. It is a poor way to add facts that change, because every update means retraining.
Long context means pasting the whole document set straight into the prompt. As context windows have grown to hundreds of thousands of tokens or more, this is often the simplest option for a handful of documents. It gets expensive and slower at scale, and research such as the 2023 “Lost in the Middle” study found models can overlook information buried in the middle of very long inputs. Many real systems combine approaches: RAG to find the right material, a long context to hold plenty of it, and sometimes a fine-tuned model to answer in the right format.
Why do RAG systems give bad answers?
Most RAG failures are retrieval failures, not model failures. If the right chunk never reaches the model, even the best model cannot use it. Common causes include chunks that are too small to carry context (a passage saying “the fee is 3%” without saying which product), chunks that are too large and dilute the relevant part, and questions phrased very differently from the documents.
Other frequent problems: outdated or duplicate documents competing with the current version, tables and scanned PDFs that were extracted badly into text, keyword-heavy queries that pure vector search handles poorly, and permission mistakes where users can retrieve documents they should not see.
Fixes usually start with the data: clean the documents, chunk them along natural sections, and add titles or short summaries to each chunk. Anthropic has reported that adding a brief, chunk-specific context sentence before embedding, combined with keyword search, cut retrieval failures by about half in its tests.
Where is RAG used in real products?
RAG is one of the most common ways businesses deploy language models. Customer support assistants answer from help-centre articles and policy documents. Internal assistants search company wikis, tickets, and shared drives. Legal and compliance tools retrieve relevant clauses and regulations.
AI search engines and chatbot web-search features are a form of RAG too: they run a web search, pull text from the top results, and have the model write an answer with links. Coding assistants retrieve relevant files from your codebase before suggesting changes.
RAG vs fine-tuning vs long context
| RAG | Fine-tuning | Long context | |
|---|---|---|---|
| Best for | Large or changing knowledge that must be cited | Consistent style, format, or task behaviour | A small set of documents used right now |
| How knowledge is added | Update the documents and re-index | Retrain on new examples | Paste documents into the prompt |
| Keeps up with changes | Yes, as soon as documents are re-indexed | Only after retraining | Yes, every request |
| Citations to sources | Natural fit | Not really; knowledge is baked in | Possible if you ask for them |
| Cost profile | Setup effort plus small per-query cost | Upfront training cost | High per-query cost for big inputs |
| Main weakness | Wrong or missing retrieval | Stale facts, can still hallucinate | Slow, costly, can miss details in the middle |
Examples
- An HR chatbot that answers “How many days of parental leave do I get?” by retrieving the current policy document and quoting it.
- A bank's support assistant that pulls the latest fee schedule before answering, so it never quotes last year's rates.
- A law firm tool that searches thousands of past contracts for indemnity clauses similar to a new draft.
- An AI search engine that runs a web search, reads the top pages, and writes a short answer with source links.
- A coding assistant that retrieves the relevant files from a large codebase before suggesting a fix.
Common questions
Is RAG the same as giving ChatGPT a file?
Close, but not always. When you upload a short file, many chatbots simply place the whole thing in the context window. With large files or many files, they switch to RAG: splitting the content into chunks and retrieving only the relevant parts for each question. Either way, the model answers from your material rather than memory alone.
Do I need a vector database for RAG?
Not necessarily. A vector database is the common choice for semantic search at scale, but small projects can store embeddings in an ordinary database or even in memory. Some effective RAG systems use plain keyword search alone. What matters is that the retrieval step reliably finds the right passages for real questions.
Is RAG still needed now that context windows are huge?
Often, yes. Even a million-token window is far smaller than a company's full document collection, and filling it on every request is slow and expensive. Models can also miss details in very long inputs. Long context and RAG work well together: retrieval narrows the material down, and a large window holds plenty of it.
Is RAG secure for private company data?
It can be, but security has to be designed in. The retrieval step should respect the same access permissions as the original documents, so users only retrieve what they are allowed to see. You should also check where documents, embeddings, and prompts are stored and whether the model provider retains them.
How do you measure whether a RAG system works?
Build a test set of real questions with known correct answers and source documents. Then measure two things separately: whether retrieval returns the right passages, and whether the generated answer is correct and supported by them. Re-run the test whenever you change chunking, embedding models, prompts, or documents.
Sources and further reading
Glossary terms
- RAG
RAG (retrieval-augmented generation) is a technique where an AI system first searches a collection of documents for passages relevant to a question, then gives those passages to a language model to write the answer. It lets a model use current or private information it was never trained on.
- Embedding
An embedding is a list of numbers (a vector) that represents the meaning of a piece of content, such as a word, sentence, document or image. Content with similar meaning gets similar numbers, so software can find related items by measuring how close their embeddings are.
- Hallucination
An AI hallucination is when a language model produces information that sounds confident and plausible but is false, invented or unsupported by its sources, such as a fake citation, a wrong date or a quote nobody said. The model is not lying; it is generating likely-sounding text.
- Fine-tuning
Fine-tuning is extra training applied to an already-trained AI model, using a smaller set of examples, so it performs better at a specific task, style or domain. It changes the model’s internal weights, producing a customised version rather than just giving it new instructions.
- Context window
A context window is the maximum amount of text, measured in tokens, that an AI model can work with in a single request. It covers everything at once: instructions, conversation history, attached documents and the model’s own reply. Anything beyond the limit must be cut or summarised.
Keep reading
- What are embeddings?
Embeddings are lists of numbers that represent the meaning of a piece of content, such as a word, sentence, document, or image. Content with similar meaning gets similar numbers, so software can find related items by measuring how close their embeddings are, even when they share no words in common.
- RAG vs fine-tuning
RAG (retrieval-augmented generation) gives a model new information at question time by searching your documents and pasting the relevant passages into the prompt. Fine-tuning changes the model itself by training it further on example inputs and outputs. Use RAG to change what the model knows; use fine-tuning to change how it behaves.
- What is an AI hallucination?
An AI hallucination is when a language model states something false or unsupported as if it were true, such as an invented fact, a fake citation or a quote nobody said. It happens because models generate plausible-sounding text rather than looking facts up. Grounding answers in real sources and verifying important claims are the main defenses.
- What is a context window in AI?
A context window is the maximum amount of text, measured in tokens, that a language model can consider at once. It covers everything in a single request: the system prompt, the conversation so far, any pasted documents or tool results, and the answer the model writes. Anything outside the window is invisible to the model.
- What is fine-tuning?
Fine-tuning is the process of taking an AI model that has already been trained on broad data and training it further on a smaller, focused set of examples, so it gets better at a specific task, format, or style. It changes the model’s internal weights, unlike prompting, which only changes the instructions you give it.
- What is an LLM?
An LLM (large language model) is an AI system trained on huge amounts of text to predict the next piece of a sentence. By doing that prediction billions of times, it learns grammar, facts, and patterns of reasoning well enough to answer questions, write, summarize, translate, and generate code. Chatbots like ChatGPT and Claude are built on LLMs.