# Retrieval-augmented generation (RAG)

> Retrieval-augmented generation is a way of answering questions with a language model that first looks up relevant passages in your own documents, then writes an answer grounded in what it found.

Also called: RAG, retrieval augmented generation, document Q&A. Category: Data and retrieval. Source: https://aibuildindex.com/glossary/retrieval-augmented-generation

## In the index

- Search over internal documents: median $2,150, middle half $300 to $8,800, 918 priced briefs (https://aibuildindex.com/cost/document-search-rag)
- LangChain: median $3,000, middle half $400 to $12,000, 695 priced briefs (https://aibuildindex.com/tools/langchain)

A language model only knows what it saw in training. Retrieval-augmented generation, usually shortened to RAG, closes that gap without retraining: at question time the system searches a store of your documents, passes the most relevant passages to the model, and asks it to answer from them. It is the most common way businesses put an AI assistant over contracts, manuals, policies or support history.

## How it works

A RAG system has two halves. The indexing half runs ahead of time: documents are split into passages ([chunking](https://aibuildindex.com/glossary/chunking)), each passage is turned into an [embedding](https://aibuildindex.com/glossary/embedding), and the embeddings are stored in a [vector database](https://aibuildindex.com/glossary/vector-database). The answering half runs per question: the question is embedded, the closest passages are retrieved, often re-ordered by a [reranking](https://aibuildindex.com/glossary/reranking) model, and handed to the language model with an instruction to answer only from them and cite where each claim came from.

Most of the engineering effort goes into the indexing half. Messy PDFs, scanned pages, tables and documents that change every week decide whether retrieval finds the right passage. When a RAG system gives a wrong answer, the cause is more often a bad retrieval than a bad model.

## What it means for cost

RAG builds price higher than a basic chatbot because the work is mostly data work: cleaning and splitting documents, choosing what to index, testing retrieval quality on real questions, and keeping the index fresh as documents change. Three things move a quote the most:

- **Document mess.** Clean text is cheap to index; scanned files, tables and mixed languages need [OCR](https://aibuildindex.com/glossary/ocr) and custom parsing.
- **Permissions.** If different users may see different documents, retrieval has to respect [access control](https://aibuildindex.com/glossary/access-control), which adds real design and testing time.
- **Proof of accuracy.** A system that has to be trusted needs an evaluation set of real questions with known answers. See [evals](https://aibuildindex.com/glossary/evals).

Running costs scale with the number of questions and the size of the passages sent to the model, so long contexts make every answer more expensive. The live figures for RAG builds are on the [document search and RAG cost page](https://aibuildindex.com/cost/document-search-rag).

## When it is the wrong tool

RAG answers questions from text. It is a poor fit when the answer needs exact arithmetic over a database, which is a job for [text-to-SQL](https://aibuildindex.com/glossary/text-to-sql), or when the model needs a new skill or tone rather than new facts, where [fine-tuning](https://aibuildindex.com/glossary/fine-tuning) can be the better choice.

## Common questions

### Is RAG the same as fine-tuning?

No. RAG gives the model new information at question time and leaves the model unchanged. Fine-tuning changes the model's weights to teach it a behaviour or style. Most business assistants over company documents use RAG, because documents change and retraining for each change is slow and costly.

### Does RAG stop hallucinations?

It reduces them but does not remove them. Grounding answers in retrieved passages and asking for citations makes errors easier to spot, but a model can still misread a passage or answer when retrieval found nothing relevant. A good build tells the user when it has no source.

## Related terms

- [Vector database](https://aibuildindex.com/glossary/vector-database): A vector database is a data store built to save embeddings, the numeric representations of text, images or audio, and to find the stored items most similar in meaning to a query, which makes it the usual search layer behind RAG systems.
- [Embedding](https://aibuildindex.com/glossary/embedding): An embedding is a list of numbers that represents the meaning of a piece of text, an image or another item, so that items with similar meanings end up close together and can be compared, searched or grouped by software.
- [Chunking](https://aibuildindex.com/glossary/chunking): Chunking is the step in a retrieval system that splits long documents into smaller passages before they are embedded and indexed, so that a search can return the specific section that answers a question rather than a whole file.
- [Reranking](https://aibuildindex.com/glossary/reranking): Reranking is a second retrieval step in which a more precise model rescores the top results from a first, faster search and reorders them by relevance to the query, so the best passages reach the user or language model first.
- [Semantic search](https://aibuildindex.com/glossary/semantic-search): Semantic search is search that matches by meaning rather than exact words, typically by comparing embeddings, so a query such as "how do I get my money back" can find a page titled "Refund policy".
- [Grounding](https://aibuildindex.com/glossary/grounding): Grounding is the practice of tying an AI model's answers to specific, trusted sources, such as company documents, database records or search results, supplied at request time, so that responses can be checked and are less likely to be invented.
