(Note · · 6 min read)
Your RAG system retrieves the wrong paragraph
The answer is wrong, so the instinct is to change the prompt or the model. But if the retrieval step handed over three paragraphs that do not contain the answer, no prompt will recover it. The model answered the question it was given, and it was given the wrong material.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Check retrieval before touching the prompt
For any failing question, log what was retrieved. Then read it and ask whether a careful person could have answered correctly from that text alone. If not, the problem is upstream of the model and the prompt is not where to spend the afternoon.
This one habit changes how the work goes. It separates a retrieval failure from a generation failure, and those have nothing in common except the symptom.
Chunk size trades precision against context
Small chunks match a query precisely and often lack the surrounding context needed to be useful. Large chunks carry context and dilute the match, so the part that matters is averaged out with material that does not.
There is no universal size — it depends on how your source material is written. On a product assistant I built over a knowledge base of 2,446 articles plus a catalogue, the ingestion pipeline chunks at 2,000 characters, which suits documents where a single topic runs for a few paragraphs. Short reference entries want something smaller.
Split on structure, not on a character count
Cutting every two thousand characters regardless of content splits sentences, tables and lists in half, and a half-table retrieves badly and reads worse. Split on the document's own boundaries first — headings, sections, articles — and only fall back to length within a section that is too long.
Overlap a little between adjacent chunks so a passage spanning a boundary still appears whole somewhere. Too much overlap fills the results with near-duplicates and crowds out other material.
Carry the context into the chunk
A chunk taken from the middle of a document loses what it is about. Prepend the title and section heading to the text that gets embedded, so a paragraph beginning with "this should not be used" still carries which product it is discussing.
Keep structured metadata alongside — source, section, date, product, language — and use it to filter before searching. Filtering by product and then searching semantically is far more reliable than hoping the embedding distinguishes two similarly worded articles about different items.
Retrieve more, then narrow
Vector similarity is good at finding candidates and imprecise about ranking them. Fetch more candidates than you need, then re-rank and keep the best few. That two-stage shape usually improves answers more than any amount of prompt editing.
Combining keyword search with vector search covers each other's weaknesses: exact terms, product codes and names are where embeddings are weakest and keyword matching is strongest.
Keep what the answer was built from
Store the retrieved chunks with the answer. That is what makes a wrong answer diagnosable later, and it is what lets you show a source to the user — which is both an honesty measure and a way for a reviewer to spot that the retrieval was off.
Caching helps the repeat case: an in-memory cache with an expiry in front of the vector search cuts the latency of questions asked many times, and the questions real users ask repeat far more than you would expect.
Common questions
Why does my RAG chatbot give wrong answers?
Usually because retrieval handed the model text that does not contain the answer. Log what was retrieved for a failing question and ask whether a careful person could answer from that text alone — if not, the prompt is not the problem.
What chunk size should I use for RAG?
It depends on how the source is written: small chunks match precisely but lose context, large ones carry context but dilute the match. A knowledge base of article-length documents suits around 2,000 characters; short reference entries want less.
Should chunks be split by character count?
Split on the document's own structure first — headings, sections, articles — falling back to length only within an over-long section. A fixed count cuts sentences and tables in half, and half a table retrieves badly and reads worse.
How do I make retrieval more reliable?
Prepend title and section headings to the embedded text so a chunk carries what it is about, keep metadata for filtering before the search, fetch more candidates than you need and re-rank, and combine keyword with vector search for exact names and codes.