In this guide
Retrieval-augmented generation, or RAG, is a technique where an AI system first searches a collection of documents for relevant passages and then gives those passages to a language model along with your question. The model answers using that supplied text rather than relying only on what it learned in training. This helps it use current, private or specialised information.
Why RAG exists
A large language model knows patterns from its training data. That creates three problems for everyday use:
- It does not know your private documents, such as internal policies or product manuals.
- Its knowledge stops at a training cutoff, so it can be out of date.
- It can produce confident but wrong statements, known as hallucinations.
You cannot paste an entire company knowledge base into a prompt, because of the limits of the context window. RAG solves this by finding only the few relevant passages and placing those in the prompt.
How RAG works step by step
There are two phases: preparing the knowledge, and answering questions.
Preparation (done ahead of time)
- Collect documents. Manuals, web pages, tickets, PDFs, notes.
- Clean and split them into chunks. Chunks are short passages, often a few paragraphs, so each one covers a single idea.
- Convert each chunk into an embedding. An embedding is a list of numbers representing meaning. See embeddings and vector databases.
- Store the embeddings with the original text in an index, often a vector database.
Answering (each time someone asks)
- Embed the question in the same way.
- Retrieve the chunks whose meaning is closest to the question.
- Build a prompt containing the question, the retrieved passages and instructions.
- Generate an answer from the model.
- Show sources so the reader can check.
A simplified prompt for step 3 might look like this:
Answer the question using only the passages below.
If the passages do not contain the answer, say you do not know.
Quote the passage number you used.
Passages:
[1] Returns are accepted within the period stated on the order page...
[2] Refunds are issued to the original payment method...
Question: Can I get a refund after I return an item?The instruction to say "I do not know" is important. It reduces, but does not eliminate, made-up answers.
What RAG is good for
- Question answering over internal documents, such as HR policy or product help.
- Customer support drafts grounded in your published guidance, as covered in AI in customer support.
- Research assistants that cite the pages they used.
- Any case where information changes often. You update the documents, not the model.
Where RAG goes wrong
RAG improves reliability but is not a guarantee. Typical failures:
- Poor retrieval. If the right passage is not found, the model has nothing useful to work with. This is the most common cause of bad answers.
- Bad chunking. Splitting in the wrong place can separate a rule from its exception.
- Stale or conflicting documents. The system faithfully repeats outdated text.
- Ignored context. The model may still lean on its own knowledge or misread the passages.
- Missing access control. If everyone searches one index, people might see documents they should not. Restrict retrieval by permission.
- Hostile content. A document can contain instructions aimed at the model. Treat retrieved text as untrusted, as explained in what prompt injection is.
How to improve a RAG system
- Start with good, current, well-organised source documents.
- Test retrieval on its own: for sample questions, are the right chunks returned?
- Try different chunk sizes and keep titles or headings with each chunk.
- Combine meaning-based search with keyword search, since exact terms such as product codes need keyword matching.
- Tell the model to use only the supplied text and to cite it.
- Build a test set of real questions with known good answers and rerun it after every change.
- Review failures regularly and fix the documents or settings.
RAG, fine-tuning and long context
RAG supplies knowledge at the moment of the question. Fine-tuning changes the model itself, which is better for style or format than for facts. A very large context window can sometimes replace retrieval for small collections. RAG vs fine-tuning compares the options in more detail. Tools and limits here change quickly, so check current documentation before choosing.
To go further, the QAM.AI curriculum includes a stage on building with retrieval, taught through short text lessons and exercises.
Key takeaways
- RAG retrieves relevant passages from your documents and gives them to the model with the question.
- It helps with private, specialised and current information, and supports citations.
- Retrieval quality usually decides answer quality.
- It reduces but does not remove hallucinations, so keep sources visible and test regularly.
- Control access and treat retrieved text as untrusted.
Frequently asked questions
Does RAG stop AI hallucinations?
It reduces them by grounding answers in supplied text, but it does not stop them. The model can misread passages, or retrieval can miss the right one. Asking for citations and checking them is still necessary.
Do I need a vector database for RAG?
Not always. A vector database is a common choice for meaning-based search at scale. For small collections, simple keyword search or a basic index can work, and many systems combine both.
Is RAG the same as training a model on my data?
No. RAG leaves the model unchanged and looks up documents at question time. Training or fine-tuning alters the model's internal parameters. RAG is usually easier to update and to audit.
This guide is general educational information and is reviewed periodically. AI tools change quickly, so check each vendor's current documentation before relying on specific features or limits.