How AI works
What is RAG (retrieval-augmented generation)? A simple explanation
RAG (retrieval-augmented generation) lets an AI look things up before it answers: it searches your documents, adds the best passages to the prompt and answers from them. How RAG works, examples, and when to use it.
Short answer
RAG, short for retrieval-augmented generation, is a way to make an AI answer from specific documents instead of only from memory. When you ask a question, the system first searches a collection of text, such as your company's help pages, picks the most relevant passages and pastes them into the prompt. The language model then writes its answer from those passages, often with citations, so answers are more current, more accurate and easier to check.
RAG in one sentence
A large language model on its own answers from what it absorbed during training, like a student taking a test from memory. RAG turns that into an open-book test: before answering, the AI looks up the right pages and reads them.
The idea and the name come from a 2020 research paper by Patrick Lewis and colleagues at Facebook AI Research. Today it's the most common way companies connect to their own documents.
How RAG works, step by step
There are two phases. First you prepare your documents once, then every question runs a quick search.
- Split your documents into short chunks, say a few paragraphs each.
- Embed each chunk: turn it into a list of numbers that captures its meaning. These are embeddings, and similar meanings get similar numbers. They're stored in a search index, often called a vector database.
- Retrieve. When a question comes in, embed it the same way and find the chunks whose numbers are closest. Many systems also mix in ordinary keyword search.
- Augment. Put the best few chunks into the prompt, with an instruction like "answer using only these sources and cite them".
- Generate. The LLM writes the answer from the passages in front of it.
Want the details of step 2? Read what embeddings are. You can also try a small retriever yourself in the RAG and agents lesson.
Why RAG is so popular
- Fresh information. Models stop learning at their training cutoff. RAG can search documents updated this morning.
- Private knowledge. Your internal wiki, contracts or support tickets were never in the . RAG lets the model use them without retraining.
- Fewer made-up answers. A model that reads the source is less likely to invent one. See why AI hallucinates.
- Citations. Because the system knows which passages it used, it can show them, so people can check.
- Cheap to update. Add a document to the index and it's searchable straight away. No training run needed.
Examples of RAG
- A customer support bot that answers from the help centre and links the article it used.
- An internal assistant that searches HR policies, engineering docs or past tickets.
- AI search engines that search the web, read the top pages and write a summary with links.
- A legal or medical research tool that only answers from an approved library of documents.
Where RAG goes wrong
RAG is only as good as its search. If the right passage isn't retrieved, the model can still guess. Common problems:
- Bad chunks. Split a table or a list in half and neither half makes sense on its own.
- Wrong passages. The question uses different words from the document, or many documents look similar.
- Too much context. Stuffing in dozens of passages can bury the useful one. More isn't always better, even with a big context window.
- Hidden instructions. A retrieved document can contain text that tries to hijack the model, a form of prompt injection.
RAG vs fine-tuning
RAG gives a model knowledge at the moment it answers. Fine-tuning changes the model itself, which is better for teaching a style, format or skill. For facts that change, RAG is usually the better first choice. We compare all the options in fine-tuning vs RAG vs prompt engineering.
When RAG is put in a loop, where the model decides what to search for next, it becomes an AI agent.
Key takeaways
- RAG = search first, then generate: retrieve relevant text, add it to the prompt, answer from it.
- It lets an AI use private or recent information it was never trained on.
- It cuts down on hallucinations and makes answers checkable with sources.
- Most RAG systems find passages by meaning using embeddings, not just keywords.
Learn it properly, free
- RAG & AI agentsAdvanced · 7 min
- Embeddings: meaning as mapsIntermediate · 5 min
- Meet the chatbots: LLMsBeginner · 6 min
- How we test language modelsIntermediate · 7 min
Terms in this post
Questions
What does RAG stand for in AI?
RAG stands for retrieval-augmented generation. The AI retrieves relevant text from a collection of documents, adds it to the prompt, and generates an answer based on it.
Is RAG better than fine-tuning?
For giving a model up-to-date or private facts, RAG is usually better: it's cheaper, easy to update and can cite sources. Fine-tuning is better for changing how a model behaves, such as its style, format or a narrow skill. Many systems use both.
Does ChatGPT use RAG?
When ChatGPT or similar assistants search the web or read files you upload before answering, they are using a form of retrieval-augmented generation.
What is a vector database in RAG?
A vector database stores the embeddings of your document chunks and quickly finds the ones closest in meaning to a question. It's the search engine part of a RAG system.
Sources and further reading
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks · Lewis et al., 2020
- Hugging Face LLM Course · Hugging Face · Free course
- The Illustrated Word2vec · Jay Alammar