Kolkata ·  
AI engineering
retrieval
deep · 6 min

RAG

Retrieval-augmented generation: search your documents for passages that fit the question, put them in the prompt, and have the model answer from them.

updated 27 Sept 2026

A user on our docs site asks: "How do I rotate the API key for a staging project?"

The model has never read our docs. What it knows came from pretraining, and our docs weren't in that pile, or were there in some older version. Ask it cold and it'll write a plausible answer in a confident voice. That's how hallucination happens.

So how do we get our docs into its head? We could train it further on them, which is fine-tuning. Or we could find the few paragraphs about key rotation when the question comes in, paste them into the prompt next to the question, and tell the model to answer from them. That second one is RAG, retrieval-augmented generation. Nothing about the model changes. We change what it reads on this one call.

Building it

A RAG system has one half that runs ahead of time and one that runs on every question.

Ahead of time, we build an index. Split every document into small pieces (chunking; the cuts decide what can be found later). Turn each chunk into an embedding, a list of numbers that puts similar meanings close together. Store the chunks and their embeddings somewhere searchable.

When a question comes in, we embed it the same way, find the chunks that sit closest to it (semantic search), put the best few into the prompt with the question, and let the model write.

Meaning-based search has one blind spot: exact strings, like an error code or a product ID, can get blurred. So real systems often run a keyword search like BM25 next to it, merge the two lists (hybrid search), and sometimes re-sort the top results with a second model (reranking).

What the model actually sees

After the search, the prompt for our question looks something like this (a sketch, not any real system's prompt):

Answer the question using only the documents below.
If they don't contain the answer, say you don't know.

<document source="docs/auth/api-keys.md">
...the two chunks about key rotation...
</document>
<document source="docs/projects/staging.md">
...the chunk about staging projects...
</document>

Question: How do I rotate the API key for a staging project?

That's all RAG is from the model's side: text pasted into a prompt. The model doesn't know a search happened. The tags keep documents apart from instructions (XML tags), and all of it has to fit in the context window with room left for the answer. That first instruction, answer only from these, is grounding. Pointing each claim back to its chunk is citations. And when nothing relevant comes back, the fallback is saying "I don't know".

Where it breaks

A team that built three RAG systems, for research papers, teaching and biomedical questions, listed seven places theirs failed. Let's follow our question through them in order.

The answer might not be in our docs at all. Best case, the system says it doesn't know. Or it's there, but the right chunk didn't rank high enough to come back. Or it came back but got cut when the results were trimmed to fit the prompt. Those first three are search problems. The model can't answer from a page it never saw, so no prompt tweak fixes them. That's why we measure the search step on its own (retrieval evaluation).

The last four happen after search worked. The chunk is in the prompt and the model still doesn't pull the answer out. Or it ignores the format we asked for. Or the answer is too vague, or too detailed, for what the user needed. Or it's right but leaves out part of what was in front of it.

There's one quiet search failure worth knowing by name. A chunk can make sense in its document and none on its own. Say a filing has "Revenue rose 4% on the quarter before." Which company? Which quarter? The paragraph that said so is a different chunk, so a question naming the company may never find this one. One fix is to have a model write a short line of context for each chunk before embedding it. Anthropic's tests cut the times the right chunk was missing from the top 20 by 35% that way, and by 67% with keyword search and reranking added. The details are in chunking.

The team's two lessons are worth keeping. You can only really check a RAG system once it's running on real questions. And it gets solid by fixing what you see, not by design on day one.

How much to send?

More chunks means a better chance the right one is in the prompt. It also means more for the model to wade through. Which wins?

Databricks ran over 2,000 tests on 13 models, growing the retrieved text from 2,000 to 192,000 tokens. Retrieving more did get the right passage in more often. But answer quality rose and then fell for most models. One got worse past 32,000 tokens, another past 64,000, and only a few held steady on every dataset. They also broke in different ways. One model refused more and more on copyright grounds as the context grew, from 3.7% of the time at 16,000 tokens to almost half at 64,000. Another summarized the documents instead of answering.

At the small end, Anthropic found sending the top 20 chunks beat the top 10 or top 5. So it's a number to tune on your own data, starting nearer 20 than 3. Where the best chunk lands in the prompt can matter too (lost in the middle).

Do we need it at all?

Sometimes not. If the whole knowledge base fits in the window, we can skip the search and paste everything in. Anthropic's rule of thumb in 2024 was under 200,000 tokens, about 500 pages. A Google study found sending the full text beat RAG on answer quality by about 4 to 13 percent, while RAG cost far less, and on 63% of questions both gave the exact same answer. RAG lost on questions that need several steps of reasoning, broad ones like "what is this book about?", and ones whose answer is spread across the text. When each wins is long context.

Bigger windows haven't made RAG pointless, though. The Databricks team read their results as the two helping each other: a longer window lets a RAG system send more of what it found. And when your documents are far bigger than any window, something has to pick what goes in.

(One caution on all these numbers: they come from 2023 and 2024 models. The shape of the lessons, that search sets the ceiling and more isn't always better, holds up better than any one figure.)

Where the name came from

RAG was named in a 2020 paper from Facebook AI Research. They described two kinds of memory. What a model learned in training lives in its weights, and it's hard to change, hard to inspect, and prone to making things up. A searchable index of text lives outside the model, and it can be edited, read and checked. Their system set the best scores of its day on three question-answering benchmarks, and wrote more factual text than the same model without retrieval.

One thing is different now. In that paper, the search part and the writing part were trained together. When people say RAG today they usually mean a model behind an API, untouched, plus a separate search step. So papers that compare "RAG" with something may mean either.

But the main idea carried over. When the key-rotation steps change next week, we don't retrain anything. We re-index the page, and the next person who asks gets the new answer.

Further reading