RAG CHUNKING
The bot quoted the right table and gave the wrong fee
Owoleaf's support assistant splits its 'Fees and limits' help-centre page every 45 words, with 10 words of overlap, and sends the 2 chunks that best match the question's words. A customer asks: 'What is the fee to send ₦60,000 to a bank account?' What does the bot's context contain?
A RAG system answers from chunks, not pages. A chunk is the smallest thing retrieval can return, so a fact cut in two can only come back in halves. Fixed-size chunking cuts every N words or tokens wherever that falls: between a table’s header and its rows, between a rule and its “except”.
The retriever ranks each chunk by how well it matches the question. The half with the question’s words wins. The half with the answer often has none of them. The bot then answers from what it was given, and the answer cites a real chunk.
# 30 words a chunk, wherever that falls
words = page.split()
chunks = [' '.join(words[i:i+30]) for i in range(0, len(words), 30)]# split on structure; repeat what a piece needs to stand alone
for section in markdown_sections(page): # heading path: 'Fees and limits > Sending money'
for block in section.blocks:
if block.kind == 'table':
for row in block.rows: # one row per chunk, header repeated
emit(section.path, block.header, row)
else:
emit(section.path, block.text) # a paragraph stays whole: rule + exception
# or: index sentences and rows, send the parent section Watch the first invariant. In FIXED SIZE it turns red on the first question: the context supports ₦25, and the right answer is ₦50. The second question gets the 5-day rule without its exception for cards issued outside Nigeria. FIXED SIZE, LUCKY gets both right with one setting. BIG CHUNKS gets both facts in at 178 words a question and breaks the budget. STRUCTURE-AWARE keeps the table whole under its heading path, and PARENT DOCUMENT matches single sentences and rows, then sends their whole section. Both stay green at 88 words or less.
What is simplified: the retriever counts shared words, weighted by how rare each word is, where production uses embeddings or a hybrid, often searched through an approximate index that can lose recall of its own (HNSW recall). Chunks are counted in words, not tokens; tokenisation shows why the two differ. “Context supports” reads only the chunks the bot was sent; there is no model. Embeddings do not fix this case: they do not compare amounts either. The fees, limits and refund times are illustrative.
Keep fixed-size chunking. Find a CHUNK SIZE and OVERLAP at which both answers are grounded and neither question sends more than 120 words of context. Note how many settings around yours fail.
GOAL: both answers grounded, ≤ 120 words each
This is the lucky setting: 45 words, overlap 5, both answers grounded. Insert one edit to the help-centre page so that the bot gives at least one ungrounded answer. You may not touch the chunker.
GOAL: at least one answer ungrounded
Write chunkTable(path, header, rows, maxRows) for a table too big for one chunk. path is the heading path (a string), header the header line, rows an array of row lines. Return an array of chunks, each a string of lines joined with \n. Rules the grader checks: every chunk contains the path line and the header line; every row appears, unchanged, in exactly one chunk; every chunk has between 1 and maxRows rows. No rows: return [].
The trade-off on size is real. Small chunks match precisely and leave room for more of them; big chunks keep facts whole and cost context. Parent-document retrieval separates the two: match small, send big. The RAG done properly chapter builds a structural chunker with heading paths, measures recall at k next to the characters delivered, and adds hybrid search and reranking. For a page this small you could send the whole page; the long context vs retrieval chapter measures when that beats retrieval.
Document Q&A and support bots over help centres, policies, contracts, price lists and spec sheets: anything with tables, numbered conditions or 'except' clauses. Symptoms: confident answers with a valid citation that are off by one row or miss an exception, and a recall score that rises only when the chunks get bigger. See the RAG done properly chapter for the full pipeline.
“Our RAG bot gives wrong answers with correct citations. How do you debug it?” Look at the retrieved chunks for the failing questions before touching the prompt. Name the boundary problem (tables and exceptions split), then the fixes in order: structure-aware chunking with heading paths, header rows repeated when a table is split, parent-document retrieval, then overlap as a cheap patch. Say how you would prove it: recall at k on a question set, reported with the tokens delivered so bigger chunks cannot win for free.
USED IN
Chapters that build on this lesson.
- Shipping a retrieval or agent featureAI Engineer