AI glossary

What is retrieval augmented generation (RAG)?

Retrieval augmented generation, usually shortened to RAG, is a way of making a language model answer from your own data. Before the model writes anything, the system searches a knowledge source for the passages most relevant to the question and adds them to the prompt. The model then answers using that material rather than relying only on what it learned in training. I have built RAG pipelines in production, and this is how I explain it.

Discuss a RAG project

The one-sentence definition

RAG = search first, then generate. If someone asks you what retrieval augmented generation is, that is the short answer. A retrieval step finds relevant text from your documents or database, and a generation step asks the language model to answer the question using that text as context.

Why RAG exists

A language model is a prediction engine. As I explain in Inside the Black Box, it doesn't "know" facts; it predicts the most likely next token based on patterns from training. That creates three problems for business use:

  • It doesn't know your data. Your policies, product catalogue, contracts and internal wiki were never in its training set.
  • Its knowledge has a cut-off. Anything that changed after training is invisible to it.
  • It can hallucinate. When it doesn't have the answer, it can produce something plausible and wrong with full confidence.

Its weights are also frozen at inference time, so it doesn't learn from your prompts. RAG works around all of this by handing the model the right information at the moment it needs it.

How RAG works, step by step

A RAG system has two phases: preparing the knowledge, and answering a question.

Preparing the knowledge (ingestion)

  1. Collect the sources. Documents, web pages, database records, tickets, whatever the assistant should answer from.
  2. Clean and split them into chunks. Long documents are broken into smaller passages so each one can be matched to a question on its own.
  3. Create embeddings. Each chunk is converted into a vector, a list of numbers that captures its meaning. Chunks about similar topics end up close together in that vector space.
  4. Store the vectors. The embeddings go into a vector index, together with the original text and useful metadata such as source, date and access level.

Answering a question (retrieval and generation)

  1. Embed the question with the same embedding model.
  2. Search for the nearest chunks using vector similarity, often combined with filters on metadata.
  3. Build the prompt. The top chunks are placed into the prompt with clear instructions: answer only from this context, and say so when the answer isn't there.
  4. Generate the answer, ideally with references back to the source passages so users can check them.

If you want to understand why embeddings and context windows behave the way they do, my LLM tokenization explainer covers the token side of the picture.

RAG vs fine-tuning

People often ask whether they should fine-tune a model instead. They solve different problems:

RAGFine-tuning
What it changesWhat the model can see at answer timeHow the model behaves
Best forFacts, documents, data that changesTone, format, specialised task style
Updating knowledgeRe-index the changed documentsTrain again
TraceabilityCan cite the source passagesHard to trace where an answer came from

My rule of thumb: if the problem is "the model doesn't know our information", start with RAG. If the problem is "the model knows enough but responds in the wrong way", look at prompting first and fine-tuning after that. Many production systems end up using good prompts plus RAG and never need fine-tuning at all.

A worked example

Imagine an employee asks an internal assistant: "How many days of annual leave do I carry over?" Without retrieval, the model can only guess from general knowledge, and it may give a confident but wrong number.

With retrieval augmented generation, the flow is different. The question is embedded and matched against the HR policy documents. The search returns the two or three passages about leave carry-over. Those passages go into the prompt with an instruction to answer only from them. The model then writes a short answer and points to the policy section it used. If the policy is updated next month, you re-index that one document and the next answer reflects the change. No retraining is needed.

Common pitfalls

  • Bad chunking. Chunks that are too large dilute relevance; chunks that are too small lose meaning. Split along the natural structure of the document where you can.
  • Garbage in, garbage out. Outdated, duplicated or contradictory documents produce confident but wrong answers. Data quality matters more than model choice.
  • Ignoring permissions. If a user can't open a document, the assistant shouldn't quote it to them. Filter retrieval by access level.
  • No evaluation. Keep a set of real questions with known good answers, and check retrieval and answers against it every time you change chunking, embeddings or prompts.
  • Stuffing the context. More chunks is not always better. Irrelevant context can distract the model and raises token cost.
  • No fallback. The assistant should say "I couldn't find that" instead of guessing.

RAG in practice

I have built RAG pipelines in production with MongoDB vector search as the retrieval layer. The lessons from that work are the pitfalls above: clean sources, sensible chunks, permission filters and an evaluation set matter more than which model writes the final answer. For the model side of the picture, my post Inside the Black Box explains why grounding a prediction engine in retrieved text works.

You don't always have to build every layer yourself. OpenAI's Responses API includes a File Search tool for retrieval over uploaded files, which I cover in my Responses API migration guide. A custom pipeline gives you more control over chunking, filters and storage, and the right choice depends on your data and constraints.

Need a RAG system?

If you want an assistant that answers from your company's data, see my RAG chatbot development service or get in touch to talk about your sources and use case.

Frequently asked questions

What does RAG stand for in AI?

RAG stands for retrieval augmented generation. The system retrieves relevant information from a knowledge source and adds it to the prompt, so the language model generates its answer from that information instead of relying only on its training data.

Does RAG stop hallucinations?

It reduces them a lot but doesn't eliminate them. Grounding the model in retrieved text, instructing it to answer only from that context, and letting it say when it can't find an answer all help. You still need evaluation to catch the cases that slip through.

Is RAG better than fine-tuning?

They do different jobs. RAG is the better fit when the model needs access to your facts and documents, especially if they change often. Fine-tuning is for changing behaviour, such as tone or output format.

What do I need to build a RAG chatbot?

You need your source content, an embedding model, a vector store to search it, a language model to write answers, and an evaluation set to check quality. I have used MongoDB vector search for the retrieval layer in production.

Let's talk about your project

Software Architect, AI Native. Google Certified Generative AI Leader with 14+ years of experience.

Discuss a RAG project