Chunking Strategies
How you split documents decides what can be retrieved; learn size, overlap, structure-aware splitting and context enrichment.
Intermediate · about 4 hours · difficulty 3/5
Learn first: Text Embeddings
You rarely embed whole documents. A 40-page PDF becomes one blurry vector that matches everything a little and nothing well. Instead you split it into chunks and embed each one. Chunking is one of the highest-leverage and most underrated decisions in RAG.
Common strategies
- Fixed size with overlap: for example a few hundred tokens with some overlap. Simple, a solid baseline.
- Recursive / structure-aware: split on headings, then paragraphs, then sentences, keeping natural boundaries.
- Document-type specific: Markdown by heading, code by function or class, tables kept whole, chat logs by conversation.
- Semantic chunking: split where the embedding similarity between sentences drops. Sometimes better, often not worth the complexity.
Enriching chunks
- Store metadata with every chunk: document ID, title, section path, page number, date, access permissions.
- Prepend a short context header (document title and section) to the text before embedding. Approaches like contextual retrieval go further and use an LLM to write a sentence situating each chunk in its document.
- Keep links to neighbouring chunks so you can expand context at answer time (small-to-big retrieval).
Pitfalls
Chunks too small lose meaning ("it increased by 12%" with no subject); chunks too large dilute relevance and waste context. There is no universal best size; evaluate with your own questions.
Good enough to move on
You can chunk a set of mixed documents with a structure-aware splitter, attach metadata, and compare two chunk sizes on retrieval quality.
Key concepts
- Fixed-size chunks with overlap
- Recursive and structure-aware splitting
- Chunk metadata
- Contextual chunk headers
- Small-to-big / parent document retrieval
- Semantic chunking
Mini project
Chunk a set of 20 Markdown or PDF documents two ways (fixed-size and heading-aware), store both with metadata, and compare which strategy retrieves the right passage for 25 test questions.
Check yourself
A chunk reads 'Revenue grew 12% over this period.' Why is it hard to retrieve, and what two fixes would help?
Free resources
- Introducing Contextual Retrieval (Anthropic)
- LangChain text splitters (LangChain)