Sub topic 5
Fixed-size chunking with overlap
Fixed-size chunking cuts a document into pieces of a set length. It is the simplest strategy and the baseline that other strategies are measured against. This chapter assumes you know basic Python, what an embedding is, and what a token is.
How it works
You pick a chunk size and an overlap. The splitter moves a window across the text. Each new chunk starts size - overlap units after the previous one, so neighboring chunks share overlap units.
The overlap helps when a sentence is cut at a boundary. The sentence is split across two chunks, but the next chunk repeats the end of the previous one, so the sentence appears whole in at least one of them.
The implementation
This version counts words. Words are a rough stand-in for tokens, which is fine for learning and for a first baseline.
def chunk_words(text, size=200, overlap=40):
if overlap >= size:
raise ValueError("overlap must be smaller than size")
words = text.split()
step = size - overlap
chunks = []
for start in range(0, len(words), step):
chunks.append(" ".join(words[start:start + size]))
if start + size >= len(words):
break
return chunksThree details matter here:
- The
overlap >= sizecheck prevents a step of zero or less. Without it, the loop would never advance orrangewould fail. - The
breakstops once a chunk reaches the end of the text. Without it, the loop would add a final chunk made only of text already covered. " ".join(...)rebuilds each chunk with single spaces. Original line breaks and indentation are lost.
Try it on a small input:
text = " ".join(f"w{i}" for i in range(20))
for chunk in chunk_words(text, size=8, overlap=3):
print(chunk)The output shows chunks starting at w0, w5, w10, and w15. The last chunk is shorter than the others, and w5 through w7 appear in two chunks.
Choosing the numbers
A common starting point is 200 to 500 tokens per chunk, with overlap of 10 to 20 percent of the size. Treat these as starting values to test, not rules. The chapter on measuring with recall@k shows how to compare settings.
- Smaller chunks give more precise matches but carry less context.
- Larger chunks carry more context but mix several topics into one embedding, which blurs the match.
- More overlap protects boundaries but raises the number of chunks, the storage cost, and the chance that near-duplicate chunks fill your top results.
The size must also stay under your embedding model's input limit. Count in the same unit the model uses. Word counts can understate token counts, especially for code, URLs, and non-English text. If you need to be exact, count tokens with the model's own tokenizer.
Common errors
- Cutting mid-sentence anyway. Overlap reduces the damage but does not remove it. A sentence longer than the overlap can still be split with neither half whole.
- Splitting structure. Fixed windows ignore headings, tables, and code blocks. A table row can end up in a different chunk from its header.
- Too much overlap. At 50 percent overlap, you store about twice the text and retrieve many near-copies of the same passage.
- Mixing units. Setting
size=512in words when the model limit is 512 tokens can silently truncate chunks at embedding time.
When to use it
Fixed-size chunking works well for uniform plain text such as transcripts, articles, and logs. It is also a good first baseline because it is predictable and cheap. Move to recursive or structure-aware splitting when you see chunks that break paragraphs, tables, or code, which the next chapters cover.
Exercise
You call chunk_words(text, size=100, overlap=25) on a text of 400 words. At which word indexes do the chunks start, and how many chunks are produced?
Show answer
The step is 100 - 25 = 75, so chunks start at words 0, 75, 150, 225, and 300. The chunk starting at 300 covers words 300 to 399 and reaches the end, so the loop breaks there. That gives 5 chunks. The chunk starting at 225 covers words 225 to 324, and it overlaps the next chunk on words 300 to 324.
Exercise
Why does chunk_words raise an error when overlap is greater than or equal to size?
Show answer
The step between chunk starts is size - overlap. If overlap is equal to size, the step is zero, so the window never advances. If overlap is greater than size, the step is negative, which would also break the loop. The check rejects both cases.