Topic 2 of 5 · 5 min read
Choosing chunk size and overlap
Goal: Choose a chunking strategy and size for a corpus and justify it with a small retrieval test rather than a default.
Chunk size is a trade-off between precision and context, and the right value for your corpus is found by testing, not by copying a default. Each chunk becomes one vector. The retriever returns whole chunks, so the chunk is the unit the model gets to read.
Check yourself · 1 of 3
What is the main cost of making chunks very large?
Why it matters
Small chunks, a sentence or two, match a specific question closely. But they often lack the surrounding context needed to answer it. Large chunks carry more context, but their vector averages many ideas, so they match specific questions less well and fill the prompt with irrelevant text. There is no universal best size. Legal clauses, API reference pages, chat logs and support articles all behave differently.
The common strategies
- Fixed-size: cut every N tokens or characters. Simple and fast, but it cuts through sentences and sections.
- Recursive: try to split on paragraph breaks first, then lines, then sentences, then words, until pieces fit the limit. A good default for plain prose.
- Sentence-based: group whole sentences up to a limit.
- Structure-aware: split on headings, list items, table boundaries or code functions, using the structure the parser kept. Best when documents have clear sections.
- Semantic: start a new chunk where the meaning shifts, measured by embedding distance between neighbouring sentences. It costs more and does not always beat recursive splitting, so test it before adopting it.
Overlap repeats the end of one chunk at the start of the next, so an idea cut at the boundary still appears whole somewhere. A small overlap helps. A large one inflates the index and returns near-duplicate chunks.
How to choose
- Write 30 to 50 real questions, each with the passage that answers it.
- Build the index with two or three candidate settings, for example recursive at 200, 400 and 800 tokens.
- For each question, check whether the right passage appears in the top 5 results (recall at 5).
- Pick the setting that scores best. Check a few failures by eye.
for size, overlap in [(200, 20), (400, 40), (800, 80)]:
index = build_index(docs, chunk_size=size, overlap=overlap)
hits = sum(gold_id in search(index, q, k=5) for q, gold_id in questions)
print(size, overlap, hits / len(questions))
What goes wrong
- Copying a default. A tutorial's 1000-character setting is applied to documents it was never tested on.
- Splitting mid-structure. A fixed-size cut lands inside a table or a numbered procedure.
- Measuring by feel. The team looks at three good examples and ships.
- Ignoring the model's budget. Large chunks times a high top-k can crowd the context window and raise cost.
Re-run the test whenever the corpus or the embedding model changes.
Examples
Same corpus, three sizes
A support team tests recursive splitting on 40 real questions.
| Chunk size | Right passage in top 5 |
|---|---|
| 200 tokens | 29 of 40 |
| 400 tokens | 34 of 40 |
| 800 tokens | 31 of 40 |
The middle size wins here. Reading the 800-token failures shows chunks that mix two unrelated topics. The team ships 400 and records the test so it can be repeated.
Splitting on headings
sections = split_on_headings(markdown, levels=("##", "###"))
chunks = []
for s in sections:
for piece in recursive_split(s.body, max_tokens=400, overlap=40):
chunks.append({"text": piece, "section": s.path})
Sections stay intact where they are short, and only long sections are split further.
Key points
- Small chunks match precisely but lose context, and large chunks keep context but dilute the vector and waste prompt space.
- Recursive splitting on paragraphs, then sentences, is a sensible default for prose; structure-aware splitting is better when sections are clear.
- Overlap protects ideas cut at a boundary, but heavy overlap bloats the index and returns near-duplicates.
- Semantic chunking costs more and is not always better, so compare it against a simpler splitter.
- Choose size with a small test set of real questions and measure whether the right passage lands in the top results.
- Repeat the test when the corpus or embedding model changes.
Common mistakes
- Using a tutorial's default size without testing it on your own documents.
- Choosing a size by reading a few examples instead of measuring recall on a question set.
- Setting a large overlap to hide bad boundaries, which duplicates content in the index.
- Using fixed-size cuts on documents that have headings, tables or numbered steps.
Check yourself · 2 of 3
A team's chunker cuts every 500 characters and a numbered procedure is split across two chunks. Which change fits best?
Check yourself · 3 of 3
You must choose between 300 and 700 token chunks for a corpus. What is the most reliable way to decide?
Practice items, not counted towards your mastery.
Start here
- Docling: document parsing for generative AI (opens in a new tab)ToolFree
An open-source parser that keeps layout, reading order and tables when converting PDFs, slides and HTML for AI pipelines.
- Unstructured documentation (opens in a new tab)DocsFree
Partitioning, element types and chunking options for PDFs, HTML, Office files and images.
- Chunking strategies for LLM applications (Pinecone) (opens in a new tab)ArticleFree
Compares fixed-size, recursive, structure-aware and semantic splitting and how size changes retrieval.
More for this topic
- Evaluating chunking strategies for retrieval (Chroma Research) (opens in a new tab)ArticleFree
Shows how to measure chunkers against a retrieval test set instead of trusting defaults.