# What Is RAG?

> How RAG works, why teams use it instead of retraining, and where it fails, explained simply for your AI interview.

**In short:** RAG, or retrieval-augmented generation, means the app searches a knowledge base first and gives the best matching passages to the AI model with the question. It adds private or up-to-date knowledge without retraining the model, and the answer can show its sources.

AI and LLM interview question Q111, Retrieval-augmented generation (RAG). Beginner, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-rag

## Say it in 30 seconds

RAG stands for retrieval-augmented generation. Before the model answers, my app searches a knowledge base for the passages that match the question, and passes them to the model with the question. The model answers from those passages, and the app can show which ones it used. We use it because the model has never seen our private documents, and retraining it for every policy change is slow and costly. Search is often where it goes wrong, so I test it on its own.

## Say it in 2 minutes

RAG stands for retrieval-augmented generation. Before the model answers, the app searches a knowledge base for the few passages that match the question, and sends those passages along with it. The model then answers from what it was given, and the app can show which passages it used.

We use it for three reasons. A model only knows what it learned in training, which stops at a cut-off date, and it has never read our private files. Retraining it each time a fee or policy changes is slow and costly. Updating an index is quick, and the answer can point back to its source so a person can check it.

Search is often the weak point. If the right passage never reaches the prompt, the model guesses or answers from memory. So I test the search on its own, with real questions and the passage each one needs. I also filter by the user's permissions and by document date, so last year's fee table can't outrank this year's just because its words match better.

Long context doesn't remove the need. In 2024 Anthropic wrote that a knowledge base under 200,000 tokens can go into the prompt whole. Bigger than that, search is the practical way to pick what the model reads. Anthropic's post pairs meaning search with keyword search, because embedding models can miss crucial exact matches.

## What is RAG, in plain words?

A language model answers from what it learned in training. That knowledge stops at a cut-off date, and the model has never read your company's private files. RAG closes that gap by adding a search step before the answer.

Microsoft describes RAG as grounding responses in your proprietary content. That means the answer is tied to specific text the model was handed, so a person can check it.

Picture a new support agent on day one. They don't memorise the whole policy manual. They look up the right page before they reply to the customer. RAG makes the app do that look-up for the model, every time someone asks. Take a bank's chat assistant: a customer asks about the late fee on their card, and the app pulls the fee table before the model writes its reply.

- **Chunk:** A short piece of a document, stored and searched as one unit.
- **Embedding:** A list of numbers that stands for the meaning of a piece of text. Similar meanings get similar numbers.
- **Vector index:** A store of embeddings that can quickly find the ones closest to a question.
- **Grounding:** Tying an answer to specific text the model was given, so a person can check it.
- **Retrieval:** Finding the stored passages that match the question.

## How does RAG work, step by step?

RAG runs in two halves. The first half happens ahead of time, when the documents are loaded. The second half runs on every question.

*Diagram, How RAG answers a question: The top group runs ahead of time: documents are cut into chunks and stored as embeddings. The lower group runs on each question: the app finds the closest chunks, adds them to the prompt, and the model answers from them.*
1. Each document is split into short pieces.
2. Each piece is turned into an embedding and stored.
3. The question is turned into numbers too, then searched.
4. The search compares the question with every stored piece.
5. The few closest pieces go into the prompt.
6. The prompt, with those pieces, goes to the model.
7. The model writes the answer and points to its sources.

1. **Cut the documents into chunks:** Split each policy or manual into short pieces. Each piece should hold one idea, so the search can return just the part that matters.
2. **Turn each chunk into an embedding:** An embedding model converts each chunk into a list of numbers that encode its meaning. The app stores them in a vector index.
3. **Embed the question:** The same embedding model that encoded the chunks converts the user's question into numbers, so the two can be compared.
4. **Retrieve the closest chunks:** The index returns the few chunks whose meaning is closest to the question.
5. **Build the prompt:** The app adds those chunks to the prompt, with an instruction to answer only from them and to say which one was used.
6. **The model writes the answer:** The model replies from the chunks it was given. The app can show which chunks it used.

Meaning search can miss exact words, such as a product code or a clause number. Anthropic notes that BM25, a keyword method, is particularly effective for queries with unique identifiers or technical terms. Anthropic's post pairs the two searches for this reason.

Here is the search step in plain Python. Word overlap stands in for embeddings, so the code runs with no libraries.

```python
# A simplified retriever for one bank question. Real RAG compares embeddings; word overlap
# stands in for them here so the code runs with no libraries.
CHUNKS = {
    "fees-oct-2026": "Late fee on the Silver card is 29 dollars per missed payment.",
    "fees-2025": "Late fee on the Silver card is 25 dollars per missed payment.",
    "travel-cover": "Travel insurance covers trips up to 60 days abroad.",
}

def words(text):
    return set(text.lower().replace("?", "").replace(".", "").split())

def retrieve(question, k=2):
    q = words(question)
    ranked = sorted(CHUNKS, key=lambda name: len(q & words(CHUNKS[name])), reverse=True)
    return ranked[:k]

question = "What is the late fee on my Silver card?"
for name in retrieve(question):
    print(name, "->", CHUNKS[name])
```

Output:

```text
fees-oct-2026 -> Late fee on the Silver card is 29 dollars per missed payment.
fees-2025 -> Late fee on the Silver card is 25 dollars per missed payment.
```

Both fee notes match the question equally well, and word matching can't tell which one is newer. So the app must filter by date or version before it ranks anything. Otherwise last year's fee can win. Here the 2026 note wins only because it is listed first, so that part is luck.

## Why use RAG instead of retraining the model?

Google's guide puts the core problem plainly, and AWS describes the fix as a way to add data without retraining.

> "LLMs are limited to their pre-trained data. This leads to outdated and potentially inaccurate responses." (Google Cloud)
>
> "RAG extends the already powerful capabilities of LLMs to specific domains or an organization's internal knowledge base, all without the need to retrain the model." (AWS)

- **Fresh: New data, no retraining.** Update the documents and the index. The model is untouched, so a new fee table needs no retraining.
- **Private: Your files stay yours.** The documents sit in your own index. The model itself is not changed, and access can be checked per person.
- **Traceable: Show the source.** The answer can point to the passages it used, so a person can check it against the original text.

Google says that giving the model facts in the prompt can mitigate hallucinations, the made-up answers a model writes with confidence. Mitigate means lower the risk, so the answer still needs checking.

The 2020 paper explored a fine-tuning recipe (extra training on a set of examples) for RAG. Anthropic describes the simpler form, where the retrieved text is appended to the user's prompt.

A worked case. A mid-size bank (a made-up example) runs a chat assistant over about 1,400 pages of fee and policy documents. Fees change each quarter. A customer asks what the late fee is on their Silver card.

Without retrieval, the model answers from memory and may quote an old fee, which costs the bank a complaint and a refund. With RAG, the search returns two fee notes, the 2026 one and the 2025 one, and both match the words equally. The fix is a filter: keep only the fee table whose effective date is current, before the chunks are ranked. The cost is real. Every chunk needs a date tag, and someone must keep those tags right each time a new table is published.

## When is RAG the wrong tool, and where does it fail?

Small sets can skip search altogether. In 2024 Anthropic wrote that a knowledge base under 200,000 tokens (small pieces of text the model counts) can go into the prompt whole. Past that size, search is the practical choice.

Search also fails quietly. Anthropic says traditional RAG removes context when it encodes information, so the right passage may never come back. Here are the main failure points.

> "If your knowledge base is smaller than 200,000 tokens (about 500 pages of material), you can just include the entire knowledge base in the prompt that you give the model, with no need for RAG or similar methods." (Anthropic)
>
> "The problem is that traditional RAG solutions remove context when encoding information, which often results in the system failing to retrieve the relevant information from the knowledge base." (Anthropic)

- **Bad chunks:** A rule is split across two chunks, so neither one holds the full answer.
- **Missed retrieval:** The right passage exists but is not in the top results, so the model never sees it.
- **Stale index:** A new policy was never embedded, so the search still finds last quarter's text. AWS advises updating the documents and their embeddings, asynchronously, to keep retrieval current.
- **Ignored context:** The passage reached the prompt, and the model answered from memory anyway. Tell it to answer only from the passages, and test that it does.

Access is the other gap. AWS notes that developers can restrict retrieval to different authorization levels, so each person only gets passages they are allowed to read.

> **Tip:** Asked how to stop a bot from quoting a restricted file? Say you filter the search by the user's permissions before the model sees any text. A rule in the prompt can be talked around, so the filter belongs in code.

### How would you test a RAG bot before launch?

1. **Build a test set:** Collect real questions from your users, each with the answer and the passage that holds it.
2. **Test the search alone:** For each question, is the right passage in the top results? Score that before you look at any answer.
3. **Test the answer alone:** Does the answer stick to the passages it was given, and does it cite them?
4. **Re-run after every change:** Test again whenever you change the chunk size, the embedding model, the documents or the prompt.

Keep the two checks apart. If an answer is wrong, you then know whether the search missed or the model ignored what it got.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: the app searches first, then the model answers from what it found.
- [ ] Why use it: fresh and private knowledge, without retraining, and sources to show.
- [ ] The two halves: prepare the documents ahead of time, then search on each question.
- [ ] Where it fails: bad chunks, missed retrieval, a stale index, ignored context.
- [ ] Access control in code, as a filter before the model sees any text.
- [ ] Testing the search and the answer separately.

**Common traps**

- Trap: "RAG is just a vector database." Better: A vector database is one part. RAG is the whole loop: chunk, search, add to the prompt, answer, and check.
- Trap: "RAG means the model learns our documents." Better: No. The model is unchanged. The app adds the matching text to the prompt at question time.
- Trap: "A bigger context window makes RAG obsolete." Better: Small sets can go in the prompt. A context window, the most text the model can read at once, is limited. Large, changing sets still need search, and the search still needs testing.

## Can you spot the problem?

**Spot the bug:** A bank's assistant keeps quoting last year's late fee. Which line is the cause?

A. chunks = split_into_pieces(fee_docs_2026, size=400)
B. index = build_index(fee_docs_2025)
C. answer = model.reply(question, passages=top_three)
D. prompt = 'Answer only from the passages below.'

**Answer:** B. The index was built from the 2025 fee documents and never rebuilt, so search only sees the old fees. Rebuild or update the index whenever a fee table changes, and tag each chunk with its date.

**Quick check:** A support bot must show each customer only their own account terms, and fee tables change every quarter. Which fix is right?

A. Add a line to the system prompt saying not to reveal other customers' terms
B. Filter the search results by the user's account and date before the model sees any text
C. Switch to a bigger model that knows who is asking
D. Retrain the model on the fee tables every quarter

**Answer:** B. The filter runs in code, before any text reaches the model, so a clever message can't open another customer's file. A prompt rule can be talked around, and a bigger model or retraining does not check who is asking.

## Can you answer these from memory?

**Flash cards**

- **What does RAG stand for?** Retrieval-augmented generation: search first, then the model answers from what it found.
- **Why not just retrain the model?** Retraining is slow and costly. Updating an index is quicker when a fee or policy changes.
- **What is an embedding?** A list of numbers that stands for the meaning of a piece of text.
- **What is a chunk?** A short piece of a document, stored and searched as one unit.
- **Where does a RAG answer often go wrong?** Often in retrieval: the right passage never reaches the prompt, so the answer is wrong.
- **Where do permissions go?** In code, as a filter on the search, before the model sees any text.

## Likely follow-up questions

- Q112 Walk through a RAG pipeline end to end. https://designervenkat.online/interview/ai/rag-pipeline-end-to-end
- Q113 What is chunking, and why does size matter? https://designervenkat.online/interview/ai/what-is-chunking-in-rag
- Q133 What are common RAG failure modes? https://designervenkat.online/interview/ai/common-rag-failure-modes
- Q138 RAG vs long context: which to use? https://designervenkat.online/interview/ai/rag-vs-long-context
- Q140 How do you enforce access control in RAG? https://designervenkat.online/interview/ai/how-do-you-enforce-access-control-in-rag
- Q281 Fine-tune vs RAG vs prompting: when to use which? https://designervenkat.online/interview/ai/fine-tuning-vs-rag-vs-prompting

## Learn it in depth

- [Retrieval-augmented generation](https://designervenkat.online/learn/ai-engineering/rags)
- [Where RAG fits, and where not](https://designervenkat.online/learn/ai-engineering/rag-usecases)
- [RAG vs Fine-tuning](https://designervenkat.online/learn/ai-engineering/rag-vs-fine-tuning)
