Last verified

Retrieval & data AI glossary

What is RAG, retrieval-augmented generation?

RAG (retrieval-augmented generation) is a technique that retrieves passages relevant to a question from a document collection and adds them to the model's prompt, so the answer draws on that material.

At a glance

  • How it works: Documents are split and indexed, usually as embeddings; each question retrieves the closest passages, which are inserted into the prompt before the model answers.
  • Why it matters: It grounds answers in current or private data without retraining, and sends only the relevant pages rather than a whole corpus.
  • What it costs: Retrieved passages are input tokens on every query — a 4,000-token context on Claude Sonnet 5.5 bills at $2 per million — plus embedding the corpus once.
  • Related to: Embeddings, context window, fine-tuning, long-context pricing.

How RAG works

The term comes from a 2020 paper by Patrick Lewis and colleagues, Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, which described models combining pre-trained parametric and non-parametric memory for language generation — knowledge stored in the model's weights, plus knowledge fetched from an external index at query time. Today the term covers any system that retrieves first and generates second.

A typical pipeline has two halves. Indexing happens ahead of time: documents are split into chunks, each chunk is converted into an embedding vector, and the vectors go into a searchable index. Answering happens per question: the question is embedded, the nearest chunks are retrieved, and those chunks are placed in the prompt alongside the question, with an instruction to answer from them and cite them. Many systems combine vector search with keyword search, and some add a reranking step that reorders the retrieved passages before the best few reach the prompt.

What a RAG query costs

Most of the recurring cost is input tokens. Each query sends a system prompt, the retrieved passages and the question; the passages are usually the largest part. Retrieve five 800-token chunks and every query carries 4,000 tokens of context before the model writes a word. The answer itself is output tokens at the higher rate.

Indexing is cheaper but not free. Embedding models are priced per input token and at a fraction of a chat model's rate — our embeddings entry shows current rates — and the corpus is embedded once, then again only for documents that change. A vector database or search service adds its own hosting cost, outside the per-token bill.

Two levers cut the per-query bill. Retrieve fewer, better passages: precision in retrieval is a direct saving on every query. And keep the stable part — instructions, format rules — at the front of the prompt so prompt caching can bill it at the cached rate; retrieved passages differ per question and rarely cache.

RAG vs a long context window

Million-token context windows make it tempting to skip retrieval and send everything. For a corpus that fits, that is simpler — but every query then pays for the whole corpus as input. A thousand questions over a 500,000-token document set is half a billion input tokens; with retrieval, the same questions might send a few thousand tokens each. Caching narrows the gap when the corpus is identical on every call, but cached tokens still cost something, and Anthropic warns that recall degrades as context grows.

Some vendors also add a long-context surcharge past a threshold, which applies to the whole request. Retrieval keeps prompts under it. The practical rule: send everything when the material is small or every answer needs most of it; retrieve when each question needs a small part of a large or changing corpus.

Worked exampleA support assistant answering from a knowledge base

30,000 requests a month on Claude Sonnet 5.5, priced at its current list rates, checked on 2026-10-09.

5 chunks retrieved · 5K input
Input · 150M tokens @ $2.00/M$300
Output · 12M tokens @ $10.00/M$120
Blended · per 1M tokens billed$2.59
Per request$0.014
Per month$420
Whole 200K-token manual in context +2800%
Input · 6.0B tokens @ $2.00/M$12,060
Output · 12M tokens @ $10.00/M$120
Blended · per 1M tokens billed$2.02
Per request$0.406
Per month$12,180
Whole manual, 90% cached +417%
Input · 90% served from cache · 6.0B tokens @ $0.34/M$2,050
Output · 12M tokens @ $10.00/M$120
Blended · per 1M tokens billed$0.359
Per request$0.072
Per month$2,170

Thirty thousand questions a month over a 200,000-token product manual. Retrieval sends a system prompt, five passages and the question; the other columns send the entire manual every time. Caching helps the second approach, but retrieval still moves far fewer tokens.

Change the workload or the model in the LLM API cost calculator.

RAG vs fine-tuning

RAG and fine-tuning solve different problems. RAG supplies facts at query time, so updating knowledge means updating the index, and answers can cite their sources. Fine-tuning changes the model's behaviour — format, tone, a narrow skill — and bakes knowledge in only loosely. In 2026 the choice is also shaped by availability: OpenAI is winding down its fine-tuning platform and the Gemini API offers no tuning, while RAG works with any model.

Keeping a RAG system cheap

Every lever comes back to the same fact: retrieved passages are input on every query. In rough order of payoff:

  • Tune how much you retrieve. Test the number of passages and their size against answer quality; each extra passage is paid for on every question, forever.
  • Order the prompt for caching. Fixed instructions and format rules first, so they cache; retrieved passages and the question after, since they change per query.
  • Embed in bulk, once. OpenAI's Batch API accepts the embeddings endpoint at the batch discount, which suits indexing a large corpus; afterwards, re-embed only documents that changed.
  • Store shorter vectors where accuracy allows. OpenAI's dimensions parameter shortens embeddings without losing their concept-representing properties, which lowers storage and search costs in the vector database.
  • Match the model to the question. With good passages in front of it, a small model often answers well; route only the hard questions to a large one.

Measure the result as cost per answered question, including retrieval, rather than as a per-token rate.

Where RAG goes wrong

  • Retrieving too much. Ten loosely relevant chunks cost more than three good ones and can dilute the answer.
  • Bad chunking. Splitting mid-table or mid-argument retrieves fragments the model cannot use.
  • Stale index. Documents that change without re-embedding return outdated passages with full confidence.
  • No citations. Without source references in the answer, nobody can check what the model drew on.

Frequently asked.

Short answers to the questions that come up next.

Q · 01 What does RAG stand for? +
Retrieval-augmented generation: retrieve relevant documents, add them to the prompt, then generate the answer from them.
Q · 02 How much does RAG cost per query? +
Mostly the input tokens of the retrieved passages plus the output of the answer, at your model's rates; embedding the corpus is a smaller, mostly one-off cost. The worked example on this page prices a typical setup on current rates.
Q · 03 Is RAG better than fine-tuning? +
For giving a model access to facts, usually yes: the knowledge stays current, answers can cite sources, and it works with any model. Fine-tuning suits changing behaviour or format, and is now harder to get from the major APIs.
Q · 04 What is a chunk in RAG? +
A piece of a document — a few paragraphs, a section, a table — stored and retrieved as one unit. Chunk size is a cost and quality trade-off: larger chunks carry more context per retrieval but more tokens per query; smaller ones are cheaper but can split what a question needs across pieces.
Q · 05 What does a vector database add to the cost? +
A separate bill from the model's: vector databases and search services are usually priced by storage and queries rather than by tokens, and larger vectors take more storage. Embedding the documents is billed per input token by the embedding model.
Q · 06 Do I still need RAG with a million-token context window? +
Often. Sending a whole corpus on every query multiplies input cost, can cross long-context price thresholds, and recall degrades as context grows. Retrieval sends only what each question needs.