Retrieval-Augmented Generation (RAG)

Machine Learning
Introduced by
Lewis, Patrick et al. (Meta AI / Facebook AI Research)
Year
2020
Type
Hybrid retrieval + generation pipeline

What does it do?

Retrieval-Augmented Generation (RAG) is a natural-language-processing pipeline introduced by Lewis et al. in 2020 that supplies a text generator with evidence fetched at inference time from an external document index.

Instead of relying only on what a model stored in its parameters during training, RAG retrieves relevant passages and passes them to the generator as context, so that the answer can be tied to specific source text. In the original formulation the generator was a pre-trained seq2seq model; today it is usually a large language model (LLM), and the design aims to reduce unsupported statements and to allow knowledge to change without retraining the model.

The intuition behind the method

Imagine asking an expert a question. Instead of answering from memory alone, the expert first consults a filing cabinet of relevant documents, reads the pertinent pages, and then formulates an answer that cites those pages. RAG works exactly this way: when a question arrives, the system searches a pre-built document index for the most relevant passages, bundles those passages with the question into a prompt, and asks the language model to synthesise an answer. The result is a response anchored in real text — not in whatever the model may have guessed from its training data.

History

RAG was introduced by Lewis et al. at NeurIPS 2020 to combine parametric memory (the weights of a pre-trained seq2seq model) with non-parametric memory (a dense index of Wikipedia accessed through a pre-trained neural retriever). The paper compared two formulations: one that conditions on the same retrieved passages across the whole generated sequence, and one that can use different passages for each generated token. The authors reported that their models set the state of the art on three open-domain question answering tasks, outperforming parametric seq2seq models and task-specific retrieve-and-extract architectures, and that their generated language was more specific, diverse and factual than a parametric-only seq2seq baseline. Gao et al. (2023) surveyed the field, organising it into Naive, Advanced and Modular RAG and reviewing retrieval, generation and augmentation components together with evaluation frameworks and benchmarks.

When to use it

Choose RAG when a language model must answer questions from a private, specialised or frequently changing document collection that it was not trained on, and when answers must be traceable to source passages. It suits question answering over large document collections, enterprise knowledge bases, and legal or medical literature, where unsupported claims must be contained. It requires a vector knowledge base, an embedding model and access to an LLM. No fixed minimum corpus size is established for this method. Whether retrieval is preferable to placing the full text directly in the prompt depends on how large the collection is relative to the context window of the model and on the task, and should be judged for the specific application. Do not use RAG when there is no document collection to retrieve from.

Limitations and considerations

  • Retrieval quality is a bottleneck: if the right passages are not returned, the LLM cannot compensate.
  • Building and maintaining a vector index adds infrastructure overhead — embedding models, a vector database, and synchronisation pipelines are required.
  • The LLM can still ignore or misinterpret retrieved passages, especially when the context window is saturated with irrelevant chunks.
  • Evaluation is more complex than standard classification: faithfulness, context relevance, and answer relevance must each be measured separately.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Can reduce unsupported statements by grounding answers in retrieved evidence rather than model memory alone, but grounding does not guarantee faithfulness, which must be measured.
  • Allows domain-specific or up-to-date knowledge to be incorporated without retraining or fine-tuning the LLM.
  • Provides answer traceability: retrieved passages can be surfaced as citations, making outputs auditable.
  • The knowledge base can be updated independently of the model, keeping answers current as documents change.

Applications

  • Enterprise question answering — building internal chatbots that answer questions grounded in proprietary documents.
  • Academic literature search — retrieving and synthesising relevant passages from large scientific corpora.
  • Legal and medical information retrieval — answering queries about regulatory texts, case law, or clinical guidelines with traceable citations.
  • Customer support automation — grounding chatbot responses in product documentation and policy documents to reduce fabricated answers.
  • Educational content delivery — generating explanations anchored in authoritative textbook passages.

How it works

How it works

1

All source documents are split into chunks, converted into dense vector representations (embeddings) using a chosen embedding model, and stored in a vector database. This step is performed once and repeated as the knowledge base changes, producing a searchable index of the document collection.

2

When a query arrives, the same embedding model encodes the query into a vector and the vector database returns the top-k document chunks whose embeddings are most similar, typically measured by cosine similarity or dot product. The number of retrieved chunks and any similarity threshold are key parameters that control the trade-off between recall and relevance.

3

The retrieved chunks are placed together with the original query to form an augmented prompt. The structure of this prompt, including how many chunks are included, whether they carry source metadata, and how they are formatted, directly shapes the quality of the final answer.

4

The augmented prompt is passed to a language model, which writes a response using both the retrieved evidence and its own parametric knowledge. The model is typically instructed to stay faithful to the retrieved context, but this instruction does not guarantee that it will do so, which is why generated answers must be checked against the passages.

5

Generated answers are assessed for faithfulness (does the answer agree with the retrieved passages?), answer relevance (does it address the query?), and context relevance (were the right passages retrieved?). Faithfulness and context relevance are measured with dedicated RAG evaluation metrics, such as those implemented in RAGAS, or through human annotation of query-answer pairs. BLEU and BERTScore compare generated text with a reference answer and do not establish whether an answer is grounded in the retrieved passages, so they should not be used as faithfulness measures. Separating retrieval errors from generation errors guides decisions about chunk size, retrieval depth and prompt design.

Common pitfalls

  • Chunk size matters: chunks that are too small lose surrounding context; chunks that are too large dilute the signal and may exceed context-window limits. Tune chunk size empirically for the specific corpus.
  • Retrieval without re-ranking can surface redundant or marginally relevant passages — a re-ranker or cross-encoder step improves precision when retrieval recall is already adequate.
  • Using a generic embedding model on a specialised corpus (e.g., clinical text, legal documents) often degrades retrieval; prefer a domain-adapted embedding model when one is available.
  • Never assume that retrieved passages are correct — source documents may contain errors or outdated information. RAG traces answers to sources but does not verify the sources themselves.
  • Ignoring evaluation: deploy without measuring faithfulness and the pipeline may look correct while silently generating answers that contradict the retrieved evidence.

Frequently asked questions

How is RAG different from fine-tuning an LLM?

Fine-tuning bakes knowledge into the model weights during an additional training pass; the knowledge is implicit and cannot be easily updated. RAG keeps knowledge external in a vector database and retrieves it at inference time, making the knowledge base straightforward to update, expand, or audit without touching the model. Fine-tuning is better for adapting the model's style or task format; RAG is better for keeping factual content current and traceable.

What is the role of the embedding model in RAG?

The embedding model converts both documents and queries into dense vectors in a shared semantic space so that similar meanings map to nearby vectors. The quality of retrieval, and therefore the quality of the final answer, depends heavily on how well the embedding model captures the semantics of the target domain. A generic embedding model may underperform on specialised corpora; domain-adapted models should be preferred when available.

How do I evaluate whether my RAG pipeline is working well?

Evaluation should cover three dimensions: context relevance (did retrieval return the right passages?), faithfulness (does the generated answer agree with the retrieved passages?), and answer relevance (does the answer address the question?). Frameworks such as RAGAS operationalise these metrics automatically. Human annotation of a sample of query-answer pairs provides a reliable ground truth.

How many documents does RAG need to be useful?

No fixed minimum number of documents is established for RAG. Whether retrieval is worthwhile depends on how the collection compares with what the model's context window can hold, and on whether selective retrieval is needed to find the relevant passages. RAG becomes most useful as the collection grows so large that a general LLM cannot cover it reliably and selective retrieval is required.

Related methods

Sources

  1. Lewis, P. et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems (NeurIPS), 33, 9459-9474.

    Open DOI reference
  2. Gao, Y. et al. (2023). Retrieval-Augmented Generation for Large Language Models: A Survey. arXiv preprint.

    Open DOI reference