# How Do You Debug a RAG Bot That Missed It?

> How to find the stage where a RAG bot lost a true answer: the index, the search, the prompt or the model, explained simply for your AI interview.

**In short:** Don't guess at a fix. Trace the one question that failed, stage by stage: is the text in the index, did search return it near the top, did it reach the prompt, and did the model use it? Fix the first stage that broke, then keep the question as a test.

AI and LLM interview question Q151, Retrieval-augmented generation (RAG). Intermediate, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/debug-a-rag-bot-that-missed-the-answer

## Say it in 30 seconds

When a bot misses an answer that is in the docs, I don't rewrite the prompt yet. I take that one question and trace it. First, is the right text in the index? Second, does search return it near the top? Third, does it reach the prompt? Fourth, does the model use it? The first stage that fails gets the fix, and the question becomes a test so it can't slip back in.

## Say it in 2 minutes

A RAG bot answers in two steps. First it searches your documents for pieces that look relevant. Then it writes an answer from those pieces. A miss can happen in either step, or in the handoff between them, so the first job is finding which one broke. Guessing wastes days, because a better prompt can't fix a chunk the model never saw.

I start with one real question that failed. I check whether the chunk holding the answer is in the index, then run search alone and see where that chunk ranks. Next I print the exact prompt the model received and check whether the chunk was in it. Only then do I look at the answer. In the leave policy case, the right table ranked fourth, and the bot only read the top three.

The fix depends on the stage. A table split across chunks needs whole tables kept together. An exact code, like a policy ID, needs keyword search alongside meaning search. An old document needs a filter on version, so it never competes. After each fix I add the question to a fixed test set, with the chunk that should win, so every change is measured again.

The honest limit is that a RAG system is hard to check before it meets real questions, so I keep watching live questions after launch and add every new miss to the set.

## What is debugging a RAG bot, in plain words?

RAG stands for retrieval-augmented generation. Anthropic describes it this way: "RAG is a method that retrieves relevant information from a knowledge base and appends it to the user's prompt." The bot searches your documents first, then the language model writes the answer from what search found. The bot does not keep your documents in memory. It only sees what search hands it for each question.

Picture a new claims clerk asked for one clause in a policy. The clause sits in a box in the back room that she never opened, so she says it isn't there. She didn't misread anything. She never got the box. A miss in a bot often works the same way: the right text never reached the model.

Our example is an HR policy bot for a 3,000-person logistics firm, built over its staff handbook. Debugging means finding which step lost the text: the index, the search, the prompt, or the model's reading of it.

- **Chunk:** A short piece of a document, stored and searched as one unit.
- **Index:** The store that holds every chunk together with its meaning code, called an embedding.
- **Top-k:** How many search results go on to the model, for example the top 3.
- **Prompt:** Everything the model reads before it writes one answer, including the chunks.

## How do you trace one missed answer?

Start with one real question that failed, and work out which chunk holds the right answer. Then walk the path below from left to right, and stop at the first stage that broke.

*Diagram, Trace one missed answer, stage by stage: Read the path left to right and stop at the first stage that broke. Often the right chunk never reached the top results, or was cut before the prompt. Fix that stage, then keep the question as a test.*
1. Start with the wrong answer and find the chunk that held the fact.
2. If that chunk is stored, check whether search ranks it near the top.
3. Only the top few results go on, so check what reached the prompt.
4. If it reached the prompt, check whether the answer used it.
5. The first stage that broke gets the fix, then a test.

1. **Check the index:** Search the stored text for a sentence from the right passage. If it is missing, the step that read the file or cut it into chunks lost it, so look there first.
2. **Run search alone:** Ask the search your exact question, with no model. Note the rank of the right chunk. Outside the top few, the bot never had a chance to use it.
3. **Print the prompt:** Log the exact text the model received for that question. If the chunk is missing there, a cut-off or a filter dropped it.
4. **Check the answer against the source:** If the chunk was in the prompt and the answer still went wrong, the model read it badly. Check whether another chunk contradicted it.

Here is the worked case. An employee asks, "How many weeks of paid parental leave do UK staff get?" The bot says 12 weeks. The 2026 handbook says 26 weeks, in a table under section 14. The 2024 handbook also says 12 and is still in the index, so both versions are there. The numbers are made up for this example.

Search alone shows the fault. The old 2024 chunk ranks first, the right table ranks fourth, and the bot sends only the top three to the model. So the model never had the 26 weeks to read.

```python
# A made-up search result for one question. Did the right chunk make the top 3?
QUESTION = "How many weeks of paid parental leave do UK staff get?"
RIGHT = "handbook-2026-14b"
RESULTS = [
    ("handbook-2024-09a", 0.83),
    ("handbook-2026-12c", 0.81),
    ("handbook-2026-03a", 0.78),
    ("handbook-2026-14b", 0.74),
    ("handbook-2026-14a", 0.70),
]
TOP_K = 3

def trace(question, results, right, k):
    print("Question:", question)
    ids = [chunk for chunk, _ in results]
    rank = ids.index(right) + 1 if right in ids else None
    print("Right chunk rank:", rank)
    for chunk, score in results[:k]:
        old = " (2024, old)" if "2024" in chunk else ""
        print(f"  {score:.2f} {chunk}{old}")
    if rank is None or rank > k:
        print("Verdict: not sent to the model, so it never saw the answer")

trace(QUESTION, RESULTS, RIGHT, TOP_K)
```

Output:

```text
Question: How many weeks of paid parental leave do UK staff get?
Right chunk rank: 4
  0.83 handbook-2024-09a (2024, old)
  0.81 handbook-2026-12c
  0.78 handbook-2026-03a
Verdict: not sent to the model, so it never saw the answer
```

Read the trace before you touch the prompt. The scores are made up. The habit is real: look at the ranks and what was sent first.

## What usually causes a miss, and how is it fixed?

Misses can come from a few common causes, and each one has a fix at the stage where it starts. Chunking alone can change the result. Anthropic notes that "the choice of chunk size, chunk boundary, and chunk overlap can affect retrieval performance."

*Diagram, Three common causes, each with a fix: Three common causes in a document bot, each shown next to the fix that targets it. Read each arrow as one pair of cause and fix. Fix the cause where it starts, then measure again.*
1. A table cut in two loses its heading, so the number has no context. Keep each table in one chunk.
2. Meaning search can skip an exact code. Keyword search matches the text, and the two lists are merged.
3. An old handbook competes on equal terms. A version filter keeps it out of the search.

- **Meaning only: Misses exact codes.** A policy ID like HR-PL-26 means little to a meaning search, so it can rank low.
- **With keyword search: Finds the exact code.** A keyword index finds the exact text, and its results are merged with the meaning results.

> "While embedding models excel at capturing semantic relationships, they can miss crucial exact matches." (Anthropic, on contextual retrieval)
>
> "significantly degrades when models must access relevant information in the middle of long contexts" (The Lost in the Middle paper, 2023)

The second card is about the prompt. When many chunks go in, the right one can sit in the middle, where models use it less well. Put the strongest chunks first, and keep the count small enough that the right one is easy to find.

Query wording matters too. OpenAI's file search can rewrite a question before it searches: "Certain query styles yield better results, so we've provided a setting to automatically rewrite your queries for optimal performance."

Old versions are a separate cause. A search over two handbooks has no reason to prefer the new one, unless you filter by version or effective date. Then the old text never competes.

> **Tip:** Check the rank before you raise the number of chunks. A bigger top-k can help, but it also gives the model more text to read, and the right answer can still get lost in it.

## How do you stop the same miss coming back?

Turn the failed question into a test. Keep a set of real questions, each with the chunk that must win. Run the set after every change to the chunker, the embedding model, the filters or the prompt.

Test search and answers apart. A search test asks whether the right chunk reached the top few. An answer test gives the model the right chunk and asks whether it answered correctly. A failing search test points at search. A failing answer test points at the prompt or the model.

Anthropic reported in September 2024 that its contextual retrieval method "can reduce the number of failed retrievals by 49% and, when combined with reranking, by 67%." Those are one company's numbers on its own data, so measure your own.

Barnett and his co-authors put it bluntly: "validation of a RAG system is only feasible during operation." So real questions from users belong in the test set too, and every new miss from live use should be added.

> **Tip:** Run the evals after every fix, even a small one, and keep the whole set.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A method, not a guess: trace one failed question.
- [ ] The stages in order: index, search rank, prompt, model.
- [ ] Rank and prompt checks before touching the model.
- [ ] Version and date filters for old documents.
- [ ] A regression test set, split into search tests and answer tests.
- [ ] Honest limits: some misses need the documents fixed, not the bot.

**Common traps**

- Trap: "Make the prompt stricter." Better: A stricter prompt can't fix a chunk the model never saw. Check the stages first.
- Trap: "Switch to a bigger model." Better: A bigger model reads the same missing text. Find out which stage lost it.
- Trap: "The embeddings are bad, so we need a new database." Better: Often the chunk was cut or filtered out. Check the stage before you replace anything.

## Can you spot where the miss happened?

**Spot the stage:** The fee bot says £25. The 2026 table says £30. Which trace line shows where it went wrong?

A. Index: the 2026 fee table is stored in 14 chunks.
B. Prompt: only fees-2024-p2 was sent, and no 2026 chunk was.
C. Model: the answer is polite and under 40 words.
D. Latency: search took 0.4 seconds.

**Answer:** B. The table is stored, so the index is fine. The old 2024 chunk reached the prompt and the 2026 table did not. Check the version filter and the ranking before you blame the model.

**Quick check:** The right chunk ranks 9th of 50 results, and the bot sends only the top 5 to the model. What is the first change to try?

A. A bigger language model
B. A longer system prompt
C. Rerank the 50 results, then keep the best 5
D. A lower temperature, which is a setting for randomness

**Answer:** C. A reranker reads each result against the question and reorders them, so a chunk at 9th can move into the top 5. A bigger model or a longer prompt can't read a chunk that was never sent.

## Can you answer these from memory?

**Flash cards**

- **What is the first thing to check?** Whether the chunk with the answer is stored in the index at all.
- **Why run search alone?** It shows the rank of the right chunk, without the model hiding the cause.
- **What is top-k?** How many search results go to the model, such as the top 3.
- **Why print the prompt?** It shows exactly what the model read, which may differ from what you expect.
- **Two kinds of RAG test?** Search tests check the rank. Answer tests check the answer, given the right chunk.
- **Best first fix for old documents?** A filter on version or date, so old chunks never compete with new ones.

## Likely follow-up questions

- Q131 Retrieval metrics: recall@k, MRR, nDCG? https://designervenkat.online/interview/ai/retrieval-metrics-recall-at-k-mrr-ndcg
- Q133 What are common RAG failure modes? https://designervenkat.online/interview/ai/common-rag-failure-modes
- Q126 Rerankers: cross-encoder vs bi-encoder? https://designervenkat.online/interview/ai/cross-encoder-vs-bi-encoder-rerankers
- Q129 What is metadata filtering? https://designervenkat.online/interview/ai/what-is-metadata-filtering
- Q132 Generation metrics: faithfulness, answer relevance? https://designervenkat.online/interview/ai/rag-generation-metrics-faithfulness-answer-relevance
- Q152 How do you handle conflicting sources? (full answer coming)

## Learn it in depth

- [From question to context](https://designervenkat.online/learn/ai-engineering/retrieval-process)
- [Chunking](https://designervenkat.online/learn/ai-engineering/chunking)
