AI
What Is RAG? Retrieval-Augmented Generation Explained
What is RAG? Learn how retrieval-augmented generation lets AI answer from your own documents, how it works step by step, and when to use it.
What is RAG? RAG stands for retrieval-augmented generation. It is a way of letting an AI model look things up before it answers. The model doesn't rely only on what it memorized during training. It first fetches relevant text from documents you choose, then writes its answer using that text.
That one idea fixes some of the most annoying problems with chatbots. It is also behind many of the "chat with your docs" tools you see today. This guide explains how it works in plain language, where it breaks, and how to try it yourself.
What Is RAG in Simple Terms?
Think of an exam.
A normal language model takes a closed-book exam. It can only use what it remembers from training. When it doesn't know something, it may guess, and the guess can sound very confident.
A RAG system takes an open-book exam. Before answering, it flips to the right pages in a book you gave it. Then it writes the answer with those pages in front of it.
The "book" can be anything written down: a company wiki, product manuals, support tickets, PDFs, research papers, or your own notes.
The name describes exactly what happens:
- Retrieval: find the most relevant pieces of text.
- Augmented: add those pieces to the prompt.
- Generation: the model writes an answer based on them.
Why Language Models Need RAG
Large language models are impressive, but they have three built-in limits that RAG addresses.
Their knowledge is frozen
Every model has a training cutoff. Anything that happened or changed after that date is unknown to it, unless you supply it.
They don't know your private information
A public model has never seen your internal policies, your customer records, or last week's meeting notes. Fine-tuning can teach it some of that, but it's slow and expensive to keep up to date.
They sometimes make things up
When a model lacks the facts, it can produce a plausible but wrong answer. This is often called hallucination. Giving the model real source text to work from makes this less likely. It doesn't remove the problem completely. It also lets the model point to where an answer came from, so a person can check it.
How Retrieval-Augmented Generation Works
Under the hood, retrieval-augmented generation is a series of simple steps. Together these steps are usually called a RAG pipeline.
Step 1: Split your documents into chunks
Long documents are cut into smaller pieces called chunks. A chunk is often a few paragraphs.
Chunk size matters. If chunks are too big, the search returns a lot of irrelevant text. If they are too small, each piece loses its context.
Step 2: Turn each chunk into an embedding
An embedding model converts each chunk into a long list of numbers. You can picture those numbers as coordinates on a map of meaning. Text about similar ideas lands close together, even when the exact wording is different.
For example, "How do I reset my password?" and "I forgot my login" share almost no words. On the map, though, they end up near each other.
Step 3: Store the embeddings in a vector database
The embeddings, along with the original text, are saved in a vector database. This is a database built to answer one question very fast: which stored items are closest to this one?
You do this preparation once. After that, you only update the index when documents change.
Step 4: Retrieve the best matches
When someone asks a question, the question is turned into an embedding too. The vector database finds the chunks closest to it, usually the top handful.
Many systems also run a normal keyword search alongside it, which is called hybrid search. Some then use a "reranker" model to sort the results more carefully.
Step 5: Generate the answer
The retrieved chunks are pasted into the prompt together with the question and an instruction. A simple version of that instruction would be: "Answer using only the sources below. If the answer isn't there, say so."
The language model then writes a response grounded in those sources. It can often cite which chunk each point came from.
What Is RAG Used For?
RAG fits almost anywhere people need answers from a specific, changing body of text:
- Customer support bots that answer from help-center articles instead of guessing.
- Internal knowledge assistants that search company wikis, policies, and handbooks.
- Research and legal work, where every claim must be traced back to a source.
- Developer tools that answer questions about a codebase or its documentation.
- Personal knowledge bases that let you "chat" with your own notes or saved articles.
The common thread: the facts live in documents, and those documents change more often than you would want to retrain a model.
RAG vs Fine-Tuning: Which Do You Need?
RAG vs fine-tuning is one of the most common questions people ask about this topic. The two are often confused, but they solve different problems.
Fine-tuning keeps training a model on your examples. It changes the model's behavior: its tone, its format, and how it handles a particular type of task.
RAG leaves the model as it is and changes the information it sees at the moment it answers.
| RAG | Fine-tuning | |
|---|---|---|
| Best for | Facts, documents, up-to-date knowledge | Style, format, specialized task behavior |
| Updating knowledge | Add or edit documents | Retrain the model |
| Shows sources | Yes, easily | Not naturally |
| Setup effort | Moderate | Higher, needs training data |
A simple rule of thumb: if the problem is "the model doesn't know this," start with RAG. If the problem is "the model doesn't act the way I need," consider fine-tuning. Many real systems use both.
RAG vs Long Context Windows
Newer models can read very long inputs, so a fair question is whether RAG is still needed. Why not paste all your documents into the prompt?
For a handful of documents, that can work well. If you're unsure how much text a model can take in at once, our guide on what a context window is explains it, along with when retrieval is the better choice.
At larger scale, RAG still has clear advantages:
- Size: a company's documents can be far larger than any context window.
- Cost and speed: sending only the relevant chunks is usually cheaper and faster than sending everything on every question.
- Focus: models can miss details buried in the middle of very long inputs. Short, relevant context is easier for them to use well.
In practice the two work together. Bigger context windows let a RAG system pass along more retrieved material at once.
Where RAG Goes Wrong (and How to Fix It)
RAG is not magic. When a RAG system gives a bad answer, the cause is usually one of these.
The right chunk was never retrieved
If the search misses the relevant text, the model can't use it. You can fix this by trying hybrid search, adding a reranker, or rewriting the question before searching.
Chunks cut ideas in half
A table split across two chunks, or a rule separated from its exception, leads to wrong answers. Try splitting along headings and sections rather than by a fixed character count, and let neighboring chunks overlap a little.
Outdated or conflicting documents
RAG faithfully repeats whatever it finds, including old policies. Keep the index in sync with your sources, and store dates or version info alongside each chunk.
The model ignores or stretches the sources
Clear instructions help, such as "only use the sources; say if you don't know." Asking the model to cite which chunk supports each claim also helps, and it makes errors easier to spot.
How to Build a Simple RAG Pipeline for Free
You don't need a big budget to try this. A basic RAG pipeline can be built entirely with free, open-source tools:
- Frameworks: LangChain and LlamaIndex handle loading, chunking, retrieval, and prompting.
- Vector database: Chroma, FAISS, or the pgvector extension for PostgreSQL are common free choices. Hosted services have free plans too: as of November 2026, Pinecone's Starter plan (up to 2 GB of storage) and Weaviate Cloud's free plan (up to 100,000 objects) are enough to learn and build a small prototype.
- Embedding and chat models: you can call a hosted API, or run open models on your own machine with a tool like Ollama.
Running everything locally keeps your documents private. If you'd like to go that route, our guide on how to run an LLM locally shows how to set up Ollama step by step.
A sensible first project: take 20 to 50 of your own documents, build the pipeline, and ask it questions you already know the answers to. Comparing its answers with what you know is the fastest way to see where retrieval works and where it needs tuning.
FAQ
What is RAG in AI?
In AI, RAG (retrieval-augmented generation) is a technique where a language model first searches a set of documents for relevant information. It then uses what it found to write its answer. This makes answers more current, more specific, and easier to verify.
Is RAG better than fine-tuning?
Neither is better overall; they do different jobs. RAG is usually the better choice for giving a model knowledge that changes, like documents and policies. Fine-tuning is better for changing how a model behaves or formats its output.
Do I need a vector database for RAG?
Not always. For a small number of documents, a simple keyword search or an in-memory index can be enough. A vector database becomes useful as your collection grows and you need fast search by meaning.
Does ChatGPT use RAG?
The base model on its own answers from what it learned in training. Features that search the web or read files you upload work in a RAG-like way: they retrieve text first, then answer from it.
Can I use RAG for free?
Yes. Open-source frameworks, free vector databases like Chroma or FAISS, and open models you run locally let you build a working RAG system at no software cost. You only need a reasonably capable computer.