Sub topic 4
Choosing chunk size and overlap
This chapter assumes you know what an embedding is, how vector similarity search ranks results, and what a token is. Chunk size and overlap are the two settings you will tune most often, so it helps to know what each one trades away.
What the two settings control
Chunk size is the maximum length of one chunk. Overlap is the amount of text that the end of one chunk shares with the start of the next. Both can be counted in characters, words, or tokens. Tokens are the most reliable unit, because embedding models and context windows limit tokens, not words.
The size trade-off
A small chunk covers one idea. Its embedding is specific, so a matching query scores high and the retrieved text has little noise. The cost is lost context. A chunk reading "It must be renewed every 90 days" does not say what "it" is.
A large chunk keeps context together. Its embedding blends several topics, though, so it matches many queries weakly and none strongly. It also uses more of the prompt budget for each result you pass to the generator.
| Chunk size | Retrieval precision | Context per chunk | Prompt cost per result |
|---|---|---|---|
| Small (about 100 tokens) | High | Low | Low |
| Medium (200 to 500 tokens) | Balanced | Balanced | Moderate |
| Large (800 or more tokens) | Lower | High | High |
Hard limits to respect
Two limits set an upper bound.
- The embedding model's input limit. Text past that limit is usually truncated, so the end of an oversized chunk is never embedded. Search will not find it.
- The context budget. If you retrieve
kchunks ofstokens, the prompt grows by aboutk * stokens, before the question and instructions.
What overlap does
Without overlap, a sentence that straddles a boundary is cut in two, and neither half may match a query well. Overlap repeats the end of one chunk at the start of the next, so a cut sentence still appears whole in at least one chunk.
Overlap has costs. It increases the number of chunks and the index size. It also produces near-duplicate results, which can fill your top k with the same passage. A common range is 10 to 20 percent of the chunk size. Overlap must always be smaller than the chunk size, or the window never advances.
Computing the step
With a fixed-size chunker, the window moves by size - overlap each time. This function reports how many chunks a document produces and how many tokens you will embed in total.
import math
def chunk_plan(total_tokens, size, overlap):
if overlap >= size:
raise ValueError("overlap must be smaller than size")
step = size - overlap
if total_tokens <= size:
count = 1
else:
count = 1 + math.ceil((total_tokens - size) / step)
return {
"chunks": count,
"tokens_embedded": count * size,
"inflation": round(count * size / total_tokens, 2),
}
for size, overlap in [(200, 0), (200, 40), (200, 100)]:
print(size, overlap, chunk_plan(10_000, size, overlap))For a 10,000-token document, 200-token chunks with no overlap give 50 chunks. With 40 tokens of overlap you get 63 chunks. With 100 tokens of overlap you get 99 chunks, nearly double the index. The last chunk may be shorter than size, so tokens_embedded is an upper bound.
A practical procedure
- Find the embedding model's token limit and keep chunks well under it.
- Pick a starting size by content type: a few hundred tokens for prose, and smaller for dense reference text.
- Set overlap to about 10 to 20 percent.
- Check that
k * sizefits your prompt budget. - Test two or three settings on real questions, as covered in the evaluation chapter, and keep the best one.
Exercise
A document has 6,000 tokens. You use chunks of 300 tokens with 60 tokens of overlap. What is the step, and how many chunks result? Would raising the overlap to 150 tokens help if answers keep getting cut mid-sentence, and what would it cost?
Show answer
The step is 300 - 60 = 240 tokens. The count is 1 + ceil((6000 - 300) / 240) = 1 + ceil(23.75) = 25 chunks. Raising the overlap to 150 gives a step of 150 and 1 + ceil(5700 / 150) = 39 chunks, about 56 percent more. It may recover some cut sentences, but it also adds near-duplicate results and index size. A better fix is to split on sentence or paragraph boundaries.