Skip to content
AI Digested
GuideIntermediate4 min read

What is RAG? How AI chatbots look things up before answering

Retrieval-augmented generation, or RAG, lets an AI search documents before it answers. Learn how it works, why it reduces made-up answers, and where it still goes wrong.

First published · AI Digested

Rows of books on library shelves

Photo: Jonathan Simcoe jdsimcoe, CC0, via Wikimedia Commons

RAG, short for retrieval-augmented generation, is a way of making an AI chatbot look things up before it answers. Instead of relying only on what it absorbed during training, the system first searches a set of documents, picks the most relevant passages, and gives them to the model along with your question. The model then writes its reply using that material.

If you have ever used a chatbot that quotes your company's help pages, or one that cites web pages, you have probably used something like RAG.

Why chatbots need it

A large language model learns from a huge snapshot of text, then stops learning. Our guide to how large language models work explains that it predicts likely next words rather than looking facts up. That causes two problems:

  • It goes out of date. Anything that happened after training is unknown to it unless it is given the information.
  • It can invent things. When it does not know, it may still produce a confident answer. See why AI makes things up.

It also knows nothing about your private material, such as your firm's policies or your own notes. RAG addresses all three by supplying relevant text at the moment of the question.

How RAG works, step by step

The details vary, but the usual recipe, described by technology firms such as IBM and Nvidia, looks like this.

  1. Prepare the documents. Long documents are split into smaller pieces, often paragraphs or sections, called chunks. This keeps each piece small enough to fit into the model's context window.
  2. Turn chunks into numbers. An embedding model converts each chunk into a long list of numbers that captures its meaning. Pieces of text with similar meanings get similar numbers.
  3. Store them. The numbers go into a vector database, a store built to find similar items quickly.
  4. Search when you ask. Your question is converted into numbers the same way, and the database returns the chunks closest in meaning. This is why you can ask in different words from the document and still get a match.
  5. Write the answer. The system pastes the best chunks into the prompt, effectively telling the model: "Using these passages, answer this question." Good systems also show which sources were used.

The key point is that the model itself is unchanged. It is simply given a better "open book" to work from.

Where the idea came from

The name comes from a 2020 research paper by Patrick Lewis and colleagues, with authors from Facebook AI Research (now part of Meta), University College London and New York University, presented at the NeurIPS conference. It combined a language model with a search index of Wikipedia and reported better results than comparable models on several question-answering tests. Today the same basic pattern powers many customer-support bots, workplace assistants and chatbots that search the web.

What RAG does not fix

RAG improves answers, but it is not magic.

  • The search can miss. If the wrong chunk is retrieved, the model answers from the wrong text, or from nothing useful.
  • The model can still misread. Having the right passage does not guarantee a faithful summary.
  • Bad sources give bad answers. If the documents are out of date, wrong or biased, so is the answer.
  • Privacy still matters. If a system searches your files or emails, it handles that data. See AI and your privacy.

RAG, fine-tuning and long context

People often mix up three ways of giving an AI more knowledge:

Approach What it does Good for
RAG Fetches relevant text at question time Current or private information, with sources
Fine-tuning Trains the model further on examples Changing style, format or specialist behaviour
Long context Pastes a lot of text into one prompt One-off tasks on a single large document

They can be combined, and none replaces checking the answer.

What it means for you

  • Prefer answers with citations. A tool that shows its sources lets you verify them.
  • Click through for anything important. Health, legal and money decisions need a professional, not just a chatbot's summary.
  • If you build a bot for your own business, RAG is usually the simplest way to make it answer from your own documents. Our guide to using AI for a small business is a starting point.
  • When it gets something wrong, ask where it found it. A missing or irrelevant source is a warning sign.

Read on

Common questions

Is RAG the same as the chatbot searching the web?

It is the same idea. Web search in a chatbot is a form of retrieval: the system fetches pages, then the model writes an answer from them. RAG usually means the documents come from a company's or person's own collection, such as a help centre or a folder of PDFs.

Does RAG stop AI hallucinations?

No. It lowers the risk by giving the model real text to rely on, but the retrieval step can pick the wrong passage and the model can still misstate what it read. Check important answers against the cited source.

Is RAG the same as training or fine-tuning a model?

No. RAG does not change the model. It hands the model extra text at the moment you ask a question. Fine-tuning changes the model itself by training it further.

Sources and further reading

  1. Lewis et al. (2020): Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (arXiv)
  2. Meta AI Research: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
  3. NVIDIA glossary: What Is Retrieval-Augmented Generation (RAG)?
  4. IBM: What are RAG vector databases?

Spotted a mistake or something out of date? Tell us and we will fix it. Read how we research and write our articles.