Chunking splits source documents into retrievable units and attaches metadata. Each chunk has to be self-contained enough to answer a question and specific enough to rank well.
Fixed windows versus structure-aware splits
A fixed-size window divides text into segments of a set number of tokens. It is simple, and it can slice through the middle of a table, a paragraph or a code block and strip the heading that gives the text its meaning. The retriever then returns fragments the model cannot use.
Structure-aware splitting follows headings, paragraphs and code blocks, so each chunk carries a complete thought. It needs more parsing up front. It targets a common RAG complaint: retrieved passages that are fragments.
Parent-child links and context prefixes
- Parent-child: small chunks are retrieved together with their enclosing section, which gives precise matches plus broader context.
- Contextual retrieval: a short description of the chunk's place in the document (often generated by a model) is prepended to each chunk before embedding, so an ambiguous chunk still signals its topic.
Example scenario: a technical document explains an algorithm. Fixed windows cut an explanation or code listing in half. Structure-aware chunks keep each explanation and listing whole, and retrieval improves.
Matching chunking to document type
| Document | Strategy |
|---|---|
| Uniform prose | Fixed-size chunks |
| Code, tables, mixed formatting | Structure-aware chunks |
| Precision matters and chunks must be small | Parent-child retrieval |
| Chunks unclear on their own | Contextual prefixes |
Both approaches trade effort for quality. Fixed windows cost almost nothing to build and work when the text is uniform. Structure-aware splitting costs parsing effort and pays it back in coherent chunks, which is why it is the better choice once documents contain code, tables or mixed formatting.
Metadata is what makes chunks findable. A parent link lets the system return the surrounding section when a small chunk matches, and a prepended description tells the model what the chunk is about. Without either, a chunk may lack the context to produce a meaningful answer.
Chunking as index granularity
Chunking is like choosing index granularity in a database, except that the rows are prose. A badly drawn chunk raises no error. It lowers relevance quietly, which makes the fault hard to find.
Low retrieval accuracy from chunk size
Example scenario: only 60% of queries retrieve a relevant document (illustrative). Chunks that are too large or too small lose context or fragment it. If chunks hold 500 words but the answer spans 600, retrieval can miss it.
- Measure average chunk size and where relevant context falls across chunks.
- Read retrieval logs for common queries that return irrelevant results.
- A/B test chunk sizes, for example 250 words against 500 words.
Adjust sizes to the context distribution, accepting more processing time. Retrieval quality sets the ceiling for the whole RAG system, so track retrieval accuracy and user satisfaction after each change.