gor.bio wiki

Retrieval-Augmented Generation (RAG) Explained

RAG gives a language model relevant documents at question time by searching a knowledge base first. Learn how it works, why it helps, and where it fails.

Category: Natural Language Processing · Created: 2026-10-04 · Updated: 2026-10-04 · 3 min read

A library card catalog: finding the right card before opening the book, the idea behind retrieval
A library card catalog: finding the right card before opening the book, the idea behind retrieval · Image: TBurmeister (WMF), CC BY-SA 4.0, via Wikimedia Commons.

Retrieval-augmented generation, or RAG, is a technique in which a system looks up relevant information from an external knowledge source and hands it to a large language model together with the user's question, so the answer is grounded in that material rather than in the model's memory alone. The term comes from a 2020 paper by Patrick Lewis and colleagues at Facebook AI Research, University College London, and New York University. RAG is now the standard way to make a model answer questions about private, specialised, or recent documents without retraining it.

Why does RAG exist?

Language models have built-in limits. Their knowledge stops at the training cutoff. They have never seen your company wiki, contracts, or support tickets. They can hallucinate, producing fluent text that is wrong, and they cannot point to a source. Retraining or fine-tuning a model every time a document changes is slow and expensive, and fine-tuning is a poor way to teach reliable facts. RAG moves the knowledge out of the model's weights and into a searchable store that can be updated in minutes.

How a RAG pipeline works

There are two phases. In the indexing phase, done ahead of time:

  1. Collect the documents.
  2. Split them into chunks, typically a few hundred tokens each, often with some overlap.
  3. Convert each chunk into an embedding, a vector of numbers produced by an embedding model, so that text with similar meaning lands close together in vector space. Word embeddings are the ancestor of this idea.
  4. Store the vectors, with the original text and metadata, in an index that supports fast approximate nearest-neighbour search.

In the query phase, done for every question:

  1. Embed the question with the same model.
  2. Retrieve the top few chunks whose vectors are most similar, usually measured with cosine similarity.
  3. Build a prompt containing instructions, the retrieved chunks, and the question, using the techniques of prompt engineering.
  4. Let the model, a transformer, write the answer, ideally citing which chunks it used.

How is retrieval quality improved?

Most RAG failures are retrieval failures: if the right passage never reaches the model, the model cannot use it. Common fixes include:

Teams evaluate retrieval separately from generation, for example by checking how often the correct passage appears in the top results for a set of test questions.

RAG vs fine-tuning vs long context

ApproachBest forWeakness
RAGFresh, private, or large knowledge with citationsQuality depends on retrieval; more moving parts
Fine-tuningStyle, format, and domain behaviourPoor at adding reliable facts; needs retraining
Long contextSmall document sets that fit in the windowCost and delay grow with length; details can be missed

The approaches combine well: a fine-tuned model can follow an organisation's format while RAG supplies the facts.

Limits and risks

RAG reduces hallucination but does not remove it. A model may ignore the supplied text or trust a wrong chunk. Studies of long prompts, such as the 2023 "lost in the middle" paper, found that models use information at the start and end of a prompt better than information buried in the middle, so stuffing in more chunks is not always better. Retrieved text can also contain instructions that hijack the model, a risk called prompt injection, and the retrieval step must enforce the same access controls as the source documents, or private data may leak into answers (see data privacy and AI).

A library card catalog is a good picture of the idea: finding the right card first makes the book worth opening, and a RAG system is the same trick performed on text.

Tags

large language models natural language processing retrieval-augmented generation vector search word embeddings

Related articles

This text may be freely copied, modified, and reused. See Content Reuse.