How it works · 1 of 37 Core

What is RAG (retrieval-augmented generation) and how does RAG work?

Retrieval-Augmented Generation

RAG: Before answering, fetch the relevant documents and put them in front of the model, so it answers from your facts, not from memory.

Loading the animation…

Say it like this RAG means your system looks up the right pages in your documents first, then the AI answers from them — so the answer is based on your facts, and you can check its source.

Under the hood — for builders
What the model actually receives
A prompt is everything the model reads in one go; a chunk is a short piece cut from your documents. RAG only changes what goes into the prompt. For this card's question:
Two jobs, and the choices in each
Embed: turn text into a list of numbers so that similar meanings land close together. Index: where those numbers are saved for fast search. Building it runs once; the rest runs on every question:
So what: tune retrieval before buying a bigger model
In the paper that named RAG, a small model that looks things up beat one 18× bigger that only remembers. Parameters: the numbers a model learns in training (more means bigger and costlier). Exact match: the answer equals the reference answer word for word.
So what: to keep answers current, update the documents
No retraining needed. The same paper swapped only the index (Wikipedia from 2016 or 2018) and asked “Who is {position}?” about 82 world leaders who changed in between. Share answered correctly; look at the diagonal:
Wrong answer? Find which half failed
Open the chunks that were fetched for that question:
Tools
Store: pgvector (inside Postgres), FAISS (a library) or a hosted vector database. Glue: LlamaIndex, LangChain or plain code.
Watch out
Fetch only pages this user may see: filter by permission before retrieval, not after.
Sources
Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (Facebook AI, 2020; arXiv 2005.11401).

What happens in the RAG animation, step by step?

RAG on or off: RAG on

  1. A customer asks something only your company's documents know.
  2. Retrieve — your app searches your documents for the pages that match the question. The answering model isn't involved yet.
  3. Augment — your app places those pages in front of the model, right next to the question.
  4. Generate — only now does the answering model step in. It answers from those pages, and can point to its source.
  5. Same model, no retraining — just the right pages in front of it. Try RAG off to compare.
  6. How did it know "Returns" and "Warranty" relate to "refund"? Neither says the word. That's embeddings — next.

RAG on or off: RAG off

  1. Same question — but this time, no RAG.
  2. The question goes to the model alone. Your documents stay shut.
  3. It still answers — from what it learned in training: public text, where "30 days" is common. Nothing about your company.
  4. Fluent, confident — and wrong for your company. Your policy says 14 days. Flip RAG on.

What everyday example explains RAG?

An open-book exam instead of a closed-book one. Closed-book, it answers from memory (its training); open-book, it reads your pages first.

Why should a team care about RAG?

The most common way companies put AI on their own data: support bots, internal search, document Q&A.