Retrieval that works: chunking the documents that matter

Most retrieval failures are decided before a single question is asked, at the moment a document is cut into pieces and labelled.

A laptop showing a document split into chunks, each labelled with its section path, document type and effective date

Imagine an animal feeds distributor that sets up a document assistant for its sales team. They load the price lists, the credit policy, the delivery schedule and a dozen supplier agreements. In the first week a salesperson asks for the price of a 50 kg bag of dairy meal and gets KES 1,150, last year's figure. Another asks about delivery to Nyahururu and gets the right day but the wrong cut-off time, because the time sat in the next row of a table that had been cut in half.

Both answers were confidently worded. Both came from the documents. And in both cases the model did exactly what it was asked: answer from the text it was given. The problem was the text it was given.

We covered the idea of grounding answers in your own documents in putting AI on your documents. This post is the unglamorous engineering underneath: how to cut documents up, and what to label each piece with.

Chunk by structure, not by character count#

The default in many tutorials is to split text every N characters with some overlap. That is fine for novels. Business documents have structure, and that structure carries meaning.

Rules we follow:

  • Split on headings first. A section of a credit policy titled "Credit limits for new customers" should stay one chunk if it fits. Only split further when a section is too long, and then split at paragraph boundaries.
  • Never split a table row. A price list row is one fact: product, pack size, price. Cut it and you get a price with no product. For long tables, chunk by groups of rows and repeat the header row at the top of every chunk.
  • Keep lists with their lead-in. "The following customers are not eligible for credit:" is useless without the list, and the list is ambiguous without the line above it.
  • Keep numbered clauses whole. In agreements, clause 7.3 should not be split from its sub-clauses.

Extracting structure from PDFs is the hard part. Text-based PDFs keep enough of it to work with. Scanned documents need reading first, and tables in scans are where most of the errors come from, so check them by eye.

Carry the heading path into every chunk#

A chunk that says "Payment is due within 14 days" means different things in the distributor section and the retail section of the same policy. Prefix every chunk with its path:

Credit policy 2026 > Distributors > Payment terms

That prefix goes into the text that is embedded, so the search can match on it, and it is shown to the model, so the answer can say where it came from.

Metadata decides what is even allowed to match#

Semantic search answers "which text is closest in meaning?". It cannot answer "which text is still valid?". Last year's price list is very close in meaning to this year's, which is precisely the problem.

Store structured fields with each chunk and filter on them before the similarity search:

FieldExampleUsed for
doc_typeprice_list, policy, agreementRoute questions to the right kind of document
effective_from / effective_to2026-07-01 / emptyExclude superseded versions
statuscurrent, superseded, draftDrafts never answer customers
partysupplier or customer nameAgreements specific to one party
source, pagefile name, page numberCitations a person can check

When a new price list is loaded, the old one is not deleted: it is marked superseded with an end date. Then "what did we charge in March?" still works, and "what is the price?" only sees the current list.

Test retrieval separately from answers#

When an answer is wrong, find out whether the right chunk was retrieved at all. Keep a set of real questions, each with the chunk that should answer it, and measure how often that chunk appears in the top few results. If retrieval is right and the answer is wrong, look at the prompt. If retrieval is wrong, no prompt will save it.

Run that set every time you change chunk sizes, embedding models or filters. It is the same discipline as regression tests for AI answers, applied one layer down.

What to do this week#

Take the ten questions your users ask most and, for each, look at the chunks your system actually retrieved, not only the final answer. Note every case where the right fact was split across chunks, or where a superseded document ranked first. Those two failure types will tell you whether to fix chunking or metadata first.

All posts

Want this working in your business?

We build the AI systems we write about - for retail, clinics, workshops and property businesses across Africa. Tell us what you run and we will be honest about where it helps.