---
title: "RAG"
type: "glossary term"
url: "https://aifordevs.org/glossary#rag"
markdown: "https://aifordevs.org/glossary/rag.md"
publisher: "AI for Devs"
license: "https://aifordevs.org/reuse"
attribution: "Credit AI for Devs and link to https://aifordevs.org/glossary#rag"
also_known_as: "Retrieval-augmented generation"
---

# RAG (Retrieval-augmented generation)

RAG (retrieval-augmented generation) is a technique that fetches relevant documents at request time and puts them in the prompt so the model answers from them. The term comes from Lewis et al. (2020).

A typical pipeline chunks documents, embeds them, retrieves the nearest passages for each query, and often reranks them before the model call. If retrieval misses the right passage, the model cannot answer from it, so measure retrieval and generation separately. Use RAG for facts that change or need citations, and fine-tuning for format and style.

## Example

Five retrieved chunks of 400 tokens each add 2,000 input tokens to every request, which is the number to budget against your context window and price.

## Source

[Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020)](https://arxiv.org/abs/2005.11401)

## Related terms

- [Embedding](https://aifordevs.org/glossary#embedding)
- [Vector database](https://aifordevs.org/glossary#vector-database)
- [Chunking](https://aifordevs.org/glossary#chunking)
- [Reranking](https://aifordevs.org/glossary#reranking)
- [Hybrid search](https://aifordevs.org/glossary#hybrid-search)
- [Context engineering](https://aifordevs.org/glossary#context-engineering)
- [Hallucination](https://aifordevs.org/glossary#hallucination)
- [Fine-tuning](https://aifordevs.org/glossary#fine-tuning)

## Read the article

[RAG vs fine tuning: which failures each one can fix](https://aifordevs.org/articles/rag-vs-fine-tuning)

## Cite this term

AI for Devs. "RAG." https://aifordevs.org/glossary#rag

- Page: https://aifordevs.org/glossary#rag
- Markdown: https://aifordevs.org/glossary/rag.md
- Reuse: Quote or summarize with a link to this page. Terms: https://aifordevs.org/reuse
- Publisher: AI for Devs
