RAG (retrieval-augmented generation)
In AI, RAG means fetching relevant documents at query time and placing them in the model's context, so it can answer from them.
RAG retrieves passages from a corpus and places them in the prompt, so the model answers from current, specific data rather than from what it memorised in training. The name comes from Lewis et al. (2020), who paired a retriever with a generator and showed better results on knowledge-heavy question answering than the generator alone.
How it works. Documents are split into chunks and indexed, usually as vector embeddings, often alongside a keyword index. At query time the system finds the closest chunks (vector similarity, keyword search such as BM25, or both, then optionally a reranker), inserts the top few into the context window, and asks the model to answer from them, ideally citing which passage it used.
Example. A support bot is asked "Can I get a refund after 30 days?". Instead of guessing from training data, it retrieves the two paragraphs of the company's current refund policy, puts them in the prompt, and answers with a quote and a link. When the policy changes, only the index is updated; the model is untouched.
RAG vs fine-tuning. RAG changes what the model can see at answer time; fine-tuning changes the model's weights. RAG suits facts that change, need sources, or are too large to train in. Fine-tuning suits style, format and narrow behaviour. The two are often combined.
Where it fails. The answer is only as good as retrieval: wrong chunk sizes, stale indexes or a query phrased differently from the documents lead the model to answer from the wrong passage, or to answer confidently without one.
In agents retrieval is usually one tool among several ("search the docs", "grep the repo") that the agent calls when it decides it needs it, rather than a fixed step before every answer. This is sometimes called agentic RAG.