Most businesses that try a general AI chatbot hit the same wall within a day: it writes fluent answers, but it does not know your prices, your policies, your product manuals or last month's process change. Retrieval-augmented generation, usually shortened to RAG, is the standard way to fix that. Instead of retraining a model on your data, you let the assistant look things up in your own documents at the moment a question is asked, and answer from what it finds. This guide explains how RAG works step by step, what it is good and bad at, and what to decide before you build one.
What is RAG, in plain English?
A large language model (LLM) such as GPT, Claude or Gemini has general knowledge from its training, but nothing about your company. RAG adds a search step in front of the model. When a user asks a question, the system first retrieves the most relevant passages from your documents, then gives those passages to the model together with the question and an instruction like "answer using only the sources below, and cite them". The model writes the answer; your documents supply the facts.
A useful way to think about it: the model is a skilled writer who has never worked at your company, and RAG hands that writer the right pages of the company handbook before every answer.
How RAG works, step by step
A RAG system has two halves. The first runs ahead of time to prepare your documents. The second runs every time someone asks a question.
1. Collect and clean the documents
Sources are usually a mix of PDFs, Word files, help-centre articles, Google Drive or SharePoint folders, Notion or Confluence pages, support tickets and database records. Each has to be converted to clean text. This step decides quality more than any other: scanned PDFs need OCR, tables need to keep their structure, headers and footers repeated on every page need removing, and outdated or duplicate versions need filtering out. If the source says two different things, the assistant will too.
2. Split the text into chunks
Whole documents are too long to search precisely, so the text is split into chunks, typically a few paragraphs each, often with a small overlap so a sentence is not cut off from its context. Good chunking follows the document's own structure (sections, headings, FAQ entries) rather than a fixed character count, and each chunk keeps metadata such as the document title, section, date, department and access level.
3. Turn each chunk into an embedding
An embedding model converts each chunk into a long list of numbers, a vector, that represents its meaning. Chunks about similar things end up close together, even when they use different words, so "cancel my plan" can match a passage about "ending a subscription".
4. Store the vectors in a searchable index
The vectors and their metadata go into a vector database or a vector-enabled store, such as PostgreSQL with pgvector, Pinecone, Weaviate, Qdrant or a search engine with vector support. For many business projects, pgvector inside an existing PostgreSQL database is enough and keeps the setup simple.
5. Retrieve the best passages for each question
When a question arrives, it is turned into a vector with the same embedding model, and the index returns the closest chunks. Strong systems do more than this single step:
- Hybrid search combines vector similarity with classic keyword search, which matters for product codes, part numbers, names and exact phrases that embeddings handle poorly.
- Metadata filters limit results to the right product, region, language or date range, and to documents the user is allowed to see.
- Re-ranking uses a second model to reorder the top results by how well they actually answer the question.
- Query rewriting turns a vague follow-up such as "what about for annual plans?" into a full standalone question using the conversation so far.
6. Generate a grounded answer with citations
The best few passages are placed into the prompt with the question and clear rules: answer only from the sources, say "I don't know" when the sources do not cover it, and cite which document each statement came from. Showing those citations to the user, with links to the original file, is one of the most effective ways to build trust and catch mistakes.
RAG vs fine-tuning: which do you need?
People often ask whether they should "train the AI on our documents". In most cases the answer is RAG, not fine-tuning.
- Use RAG when the goal is answering from facts that change: policies, prices, manuals, contracts, tickets. Updating knowledge is as simple as re-indexing a document, answers can cite their source, and access control can be enforced per document.
- Use fine-tuning when the goal is changing how the model behaves: a fixed output format, a house writing style, or a narrow classification task. Fine-tuning is a poor way to store facts, because the model cannot tell you where an answer came from and you have to retrain to update it.
- Combine them occasionally, for example a fine-tuned model for tone and format that still retrieves facts through RAG. Most business assistants never need this.
Very large context windows have not removed the need for RAG either. Pasting thousands of pages into every request is slow and expensive, and models still answer more reliably from a short, relevant set of passages than from a huge pile of text.
Where RAG assistants go wrong
RAG reduces made-up answers a great deal, but it does not remove them. Most failures come from a handful of causes:
- Retrieval misses. If the right passage is not retrieved, the model cannot use it. This is the most common failure, and it is fixed with better chunking, hybrid search and re-ranking, not a bigger model.
- Messy or conflicting sources. Old versions of a policy sitting next to the new one produce confident wrong answers. Clean up and date your sources.
- Tables and scanned documents. Price lists and spec sheets lose meaning when flattened to text. They often need special handling or a structured database lookup instead.
- Questions that need calculation or live data. "How many orders did we ship last week?" is not a document question. That is a job for a database query or an AI agent with tools, which can sit alongside RAG.
- Permissions. If HR files and client contracts go into one index without access rules, the assistant can leak them to the wrong person. Filters must apply at retrieval time, based on who is asking.
How to measure whether it works
Do not judge a RAG assistant by a few impressive demos. Before launch, write a test set of real questions, ideally taken from support emails or staff chat, with the correct answer and source for each. Then measure separately whether the right passages were retrieved and whether the final answer was correct, complete and cited properly. Splitting the two tells you whether to fix search or fix the prompt. Run the same test set after every change, and keep logging real questions and thumbs-up or thumbs-down feedback once users are on it.
Security and privacy checklist
- Choose an LLM provider and plan whose terms do not use your data for training, or host an open-source model yourself if your data cannot leave your servers.
- Apply document-level permissions during retrieval, not only in the user interface.
- Strip or mask personal data that the assistant does not need.
- Treat document text as untrusted input: a document can contain instructions aimed at the model, so the system prompt and tool permissions must not be overridable by retrieved content.
- Log questions and answers, with retention rules that match your privacy policy.
What does a RAG assistant cost to build?
The main cost drivers are the number and messiness of your sources, how many systems need connectors (Drive, SharePoint, a CRM, a help desk), access-control rules, where the assistant lives (website widget, Slack or Teams, inside your own app), and how much evaluation and tuning is needed. Running costs come from LLM and embedding API usage plus hosting the index, and they scale with how many questions people ask. We break down build and running costs in our guide to what an AI chatbot costs in 2026, and you can get a rough range for the full product with our app cost calculator.
A sensible way to start
- Pick one use case with a clear owner, such as answering staff questions about internal procedures or customer questions about one product line.
- Gather and clean the documents for that use case only.
- Write 50 to 100 real test questions with correct answers.
- Build a first version, measure it against the test set, and improve retrieval until results are good enough for the risk involved.
- Launch to a small group, with citations visible and a way to flag bad answers, then expand sources and users.
How we help
Our AI work includes an AI image generation app and a Unity business assistant avatar that talks with customers. For RAG projects we handle the whole pipeline: document ingestion and cleaning, embeddings and vector search, the LLM layer, permissions, evaluation and the interface. See our AI and LLM integration and AI chatbot development services, or generative AI development if the assistant is part of a larger product. Our backends are usually built with Python and FastAPI.
Questions about RAG
Is RAG the same as training an AI on my documents?
No. RAG leaves the model unchanged and searches your documents each time a question is asked, then gives the relevant passages to the model. That makes updates instant, lets answers cite sources, and allows per-document access control, which training cannot do.
Can a RAG assistant still make things up?
It can, but much less often than a general chatbot. Most wrong answers come from the right passage not being retrieved or from conflicting source documents. Clean sources, hybrid search, re-ranking, citations and an instruction to say "I don't know" keep errors low.
Can my documents stay private?
Yes. You can use an LLM provider plan that does not train on your data, keep the vector index on your own servers, apply permissions at retrieval time, or run an open-source model fully on your own infrastructure if data must never leave it.
Plan your company AI assistant
Tell us which documents you want the assistant to use, who will ask it questions, and where it should live. We reply within one business day with questions, a suggested architecture and a milestone estimate at $50 per hour. Start a project, or hire us on Freelancer, where we are a Preferred Freelancer with a 4.9 average from 356 reviews and clients pay 0% commission through this link.