sodcalculator

10 Tech How-To

How to Debug a RAG App That Gives Wrong Answers

Guide 10-40400 · Updated · Step by step

When a retrieval-augmented generation (RAG) app gives an incorrect answer, the cause may be retrieval, source data, prompt construction, or generation. The fastest way to find it is to trace one failed question through each stage, inspect the evidence the model received, and change one component at a time. You’ll need access to the app’s logs or a way to run a question with its retrieved passages visible.

What You’ll Need

  • A specific question that produced a wrong, incomplete, or unsupported answer, plus the answer you expected and its source.
  • Access to the indexed documents, retrieval results, prompt, model response, and relevant configuration or logs. Hide secrets and personal data when sharing traces.
  • A repeatable test environment or a small set of known questions to check changes without relying on a single example.

Step-by-Step

  1. Record the failure precisely. Save the exact user question, the full answer, when it occurred, and the expected answer with a citation to the source. Note whether the answer is factually wrong, missing a detail, outdated, or unsupported. Avoid rewriting the question yet: the original wording is part of the test.
  2. Replay the request and capture a trace. Run the same question under the same settings if possible. Log the query after any rewriting, the retrieved document IDs and text, relevance scores if available, the final prompt or message sequence, and the model output. Record the index version, filters, and model settings. If identical runs differ, note that generation may be nondeterministic; compare the evidence and prompt before concluding the retrieval system changed.
  3. Check that the right information is in the source data. Open the retrieved passages and the original document. Confirm that the fact exists, is current, and was extracted correctly. Look for missing pages, OCR errors, broken tables, duplicated versions, stale copies, or confusing metadata. If the source itself is wrong or absent, fix or add it before tuning the model.
  4. Test retrieval independently of answer generation. Search the index using the original question and a few natural paraphrases. Is the passage containing the answer among the results? If not, inspect document parsing, chunk boundaries, embeddings, metadata filters, and index freshness. A fact split across chunks may be hard to retrieve; try a different chunk size or overlap, then rebuild and evaluate the index. Do not assume that increasing the number of retrieved chunks fixes poor ranking.
  5. Inspect ranking, filters, and query rewriting. Check whether irrelevant passages rank above the useful one or a filter excludes it. Compare the raw question with any rewritten search query to see whether a key name, date, or constraint was lost. Temporarily remove filters or rewriting in a controlled test. If the right passage appears only after a change, determine which setting caused the miss and keep a regression test for it.
  6. Verify the context actually reaches the model. Inspect the assembled prompt, not just the search results. Confirm that useful passages were not dropped by token limits, truncated, placed in the wrong message, or obscured by repeated low-value context. Check that source labels and document boundaries are clear. If the evidence is absent from the prompt, fix context selection or packing before changing generation instructions.
  7. Test whether the prompt supports a grounded answer. Look for conflicting instructions, unclear questions, or wording that encourages an answer even when evidence is missing. State that the response should use the supplied context, distinguish evidence from inference, and say when the context does not answer the question. Ask for citations tied to passage IDs or source locations, then verify every citation manually; a citation is not proof that a claim is supported.
  8. Change generation settings only after evidence checks. With the same retrieved context and prompt, try a controlled setting or prompt change. Lower randomness when repeatability matters, but remember that it cannot repair bad evidence. Avoid changing the model, chunking, retrieval count, and prompt simultaneously: otherwise you will not know what helped.
  9. Build a small evaluation set and compare. Add the failed question and similar cases with expected facts and relevant sources. Test retrieval separately (did it find the needed evidence?) and generation (did the answer faithfully use that evidence?). Compare before and after changes, including unanswered questions and edge cases. Keep a change only if it improves the target case without causing regressions.

Common Mistakes

  • Tuning the prompt before checking retrieval. A model cannot reliably use a passage it never received.
  • Adding more context indiscriminately. More passages can bury the relevant evidence, create conflicts, or exceed the context limit.
  • Trusting similarity scores as truth. Scores are ranking signals, not guarantees that a passage answers the question.
  • Reindexing without checking ingestion. Rebuilding an index will not correct a source that is missing, stale, or parsed incorrectly.
  • Testing only one question. A fix can help one phrasing while harming others. Preserve representative questions as regression cases.

Tips

  • Use a trace that links each answer claim to the passage that supports it. This makes unsupported claims easier to spot.
  • Keep retrieval and generation diagnostics separate: first ask whether the evidence was found, then whether the answer used it correctly.
  • Include source dates and document versions when freshness matters, and check that metadata filters use the intended values.
  • For sensitive logs, redact personal information, credentials, and confidential source text according to your organization’s data-handling rules.
  • Make one change per experiment and record the configuration. Reproducibility is more useful than a collection of untracked tweaks.

Troubleshooting

The correct passage is not retrieved: Confirm it was ingested and indexed, then check parsing, chunking, filters, and index freshness. Test a direct search for a distinctive phrase from the source. If that works but the question does not, investigate query formulation and ranking.

The passage is retrieved but absent from the prompt: Inspect context assembly, token budgeting, deduplication, and truncation. Reduce low-value context or adjust selection so the key evidence is retained.

The prompt contains good evidence but the answer is wrong: Check for contradictions among passages and ambiguous instructions. Ask for a concise answer grounded in cited evidence, or an explicit statement that the evidence is insufficient. Compare a controlled generation run before considering a model change.

The answer changes between runs: Capture each run’s prompt and retrieved context. If those are stable, generation variability may be responsible; use repeatable settings where available and evaluate multiple runs for important cases.

A fix breaks previously correct answers: Restore the last known configuration, identify the change, and run the full test set. Keep separate tests for different document types, filters, and question styles rather than judging by one example.

Frequently Asked Questions

How can I tell whether retrieval or generation is at fault?
Check whether the correct, sufficiently complete evidence appears in the final prompt. If not, investigate ingestion and retrieval; if it does, inspect prompt instructions and how the answer uses that evidence.

Should I increase the number of retrieved chunks?
Only if testing shows useful evidence is being missed just below the cutoff. More chunks can add noise, so compare ranking and answer quality with a representative evaluation set.

Will a better model fix wrong RAG answers?
Not if the source is missing, stale, or excluded from context. Establish that the evidence reaches the model before testing a different model.

Why are citations present but the answer is still wrong?
A citation may point to a passage that does not support the associated claim. Check each claim against the cited text and ensure citations identify the correct source location.

How many test questions do I need?
Start with the failure and add representative variations, including cases with missing or conflicting evidence. Expand the set as new failures appear; there is no universal minimum.

When should I rebuild the index?
Rebuild or update it after correcting ingestion, source content, chunking, or embedding configuration. First verify that the change addresses the cause, since rebuilding alone does not fix faulty inputs.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →