Why does my RAG chatbot hallucinate? The exact fixes
A retrieval-grounded chatbot making things up is almost never a model problem. It is a retrieval, prompt, or data issue. Here is how to diagnose and fix each one.
A RAG chatbot hallucinates because retrieval failed, or the prompt did not bind the model to the retrieved text, or the source data is contradictory. It is almost never a model problem. Switching to a larger model makes the hallucinations sound more authoritative — it does not stop them.
Every query passes through embedding, vector search, chunk retrieval, context assembly, and generation. A failure at any stage cascades to the output. The model at the end is just the messenger. Here are the six structural failures we actually fix, and how to tell which one you have.
The retrieval returned nothing — and the model answered anyway
This is the most common failure in a newly built RAG system. The embedding-based search finds no chunks above the similarity threshold. The application logic, rather than stopping, passes the user's question with an empty context block to the model. The model does exactly what it was trained to do: predict the next likely tokens. It answers from its parametric memory, inventing an authoritative-sounding response from nothing.
Diagnosis. Check the exact JSON sent to the model. If the context array is empty and the output contains a confident factual claim, you have a missing refusal path.
Fix. Add an explicit refusal branch before you call the generation model. If retrieval returns zero chunks above your threshold, return a static decline string and skip the LLM call entirely. This eliminates the hallucination and saves the cost of a generation call. If you must preserve conversational continuity, enforce the refusal in the system prompt with language like: "If no source documents are provided, output exactly: 'I don't have that information.'"
RAG execution trace — empty retrieval
User query → vector search: 0 results → empty context →
model predicts next tokens → answer invented from memory
The retrieval returned the wrong chunks
The search runs, finds text, but it is the wrong text. This happens most often because of bad chunking strategy or embedding mismatch.
Naive chunking splits documents by token count or character limit. If a markdown table is split halfway through, the retrieval system returns a fragment without the header row that gives the numbers meaning. The model receives an incomplete thought and invents the rest.
Embedding mismatch is subtler. A high-dimensional semantic embedding groups documents by thematic similarity. If a user asks for an exact alphanumeric part number, semantic search might return a different part with similar descriptive language. The model reads the wrong document and confidently states the wrong specification.
Diagnosis. Read the retrieved chunks in the trace — not the model output, just the chunks. Ask whether a human could answer the question using only that text. If not, neither can the model.
Fix. Chunk at semantic boundaries: heading breaks, paragraph breaks, code block edges. Never bisect tables. Then add a reranking step. Vector search is built for recall, not precision. Take the top-N results from your vector database and pass them through a cross-encoder, which scores the user's exact query against each chunk's full text simultaneously. A cross-encoder eliminates most embedding mismatches. Any chunk the cross-encoder scores poorly gets dropped before it reaches the generation model.
Two-stage retrieval
Phase Goal Mechanism
Vector search High recall Compare pre-computed vectors
Cross-encoder High precision Score query + chunk text together
We use Spring Boot + pgvector for the retrieval layer on most builds. In EvidenceVault — our SOC 2 audit-evidence tool — the live analyzer maps 12 auditor requests to SOC 2 control criteria automatically. Mapping a loosely worded auditor question to one specific control criterion is exactly where wrong-chunk retrieval shows up first, so it is where we test retrieval hardest.
The prompt failed to bind the model to the context
The retrieval works. The correct paragraph is in the context window. The model reads it, recognises the topic from its training data, and extends the answer beyond what the text actually says. This looks like a hallucination because the user assumed the output came entirely from their documents. Half of it did. The other half was the model extrapolating from internet-scale pretraining.
This is especially dangerous because the grounded half makes the invented half sound credible.
Diagnosis. The execution trace shows the correct source text in context. The output contains facts logically adjacent to the text but not present in it.
Fix. Write a restrictive system prompt that explicitly licences the model to refuse. Without permission to say it does not know, a generative model will almost always guess.
You are an answering system. You will receive a user question and a set of source documents.
Your task is to answer the question using ONLY the facts present in the source documents.
If the source documents do not contain the answer, output exactly: "I do not know."
Do not combine the source documents with your own knowledge.
<documents>
{context}
</documents>
<question>
{question}
</question>
Test the binding by deliberately passing a context block that contradicts known reality and asking a question about it. If the model answers with the fake facts, the binding is tight.
The source data is stale or contradictory
In any mature engineering organisation, documentation diverges. A marketing page from 18 months ago says the concurrency limit is 50. A technical spec from last month says 20. Both are in the search index.
When a user asks about concurrency limits, the vector search does exactly what it should: it retrieves both documents. The model reads a claim of 50 and a claim of 20. It cannot know which document supersedes the other. It tries to reconcile them. It might state that the limit is 35, or that it is 20 for standard users and 50 for administrators. That is not hallucination in the usual sense — it is the model failing to resolve a merge conflict in your data.
Diagnosis. Inspect the retrieved chunks and find that they contradict each other. The model output reflects an attempted synthesis.
Fix. Version your documents and attach timestamps or validity flags at ingestion time. Pass those metadata fields to the retrieval engine as filters — only documents tagged status: current come back. Inject document timestamps directly into the context payload and instruct the model to prefer the most recent source when sources disagree. You cannot solve a data contradiction with a better prompt. The fix is data hygiene upstream of the search.
The model answered from its parametric memory
A user asks about configuring a specific internal service. The model has seen thousands of tutorials on that service in its pretraining data. It ignores the thin internal documentation in the retrieved context and answers from memory. The answer might be entirely correct in the abstract — and completely wrong for your specific infrastructure. It describes the default configuration path; your platform uses a custom one.
Diagnosis. The retrieved context is thin or only tangentially related to the topic. The output is highly detailed, confident, and reads like a generic tutorial.
Fix. Add a negative constraint to the system prompt. Explicitly forbid fallback to general knowledge: "If a specific configuration is not described in the provided documents, state that the internal configuration is unknown. Do not provide a generic configuration." The more prevalent a topic is on the public internet, the harder it is to bind a model strictly to your private documentation of that same topic. The binding must be explicit.
The context window buried the relevant chunk
Large context windows sound like a solution. They are sometimes a problem. Models do not attend to all parts of a long context equally. The attention mechanism pays disproportionate attention to text at the very beginning and very end of the prompt. If the single paragraph containing the correct answer is chunk number 27 in a 50-chunk payload, the model might read past it and invent the answer.
Diagnosis. The trace shows the correct information was retrieved and passed to the model. The prompt was strict. The data was clean. The relevant chunk was buried in the middle of a massive context payload.
Fix. Use precision, not volume. Rerank the retrieved results and aggressively truncate at the top 5 to 10 chunks. If you genuinely must pass many chunks, sequence them deliberately to match the attention curve: place the highest-scoring chunks at the very start of the context block, the next-highest at the very end, and bury the marginal ones in the middle.
You cannot fix what you do not count
Every fix described above requires knowing which failure you actually have. You cannot determine that by reading source code or by reacting to user complaints. You need a fixed eval set: representative questions, expected answers, automated grading, and a run record tied to every commit. Once you have a hallucination rate against a static dataset, you can see regressions immediately, trace failures to their exact cause, and apply the right fix rather than guessing.
We ship an eval set from the client's own questions as part of every AI reliability evals engagement, with a stated error rate at handover. That number is the baseline we manage against — not a feeling about whether the demo looked good.