Fixed-size Chunking
Fixed-size chunking splits text into pieces using a chosen size, such as a number of characters or tokens. It is simple, predictable, and useful as a baseline for RAG systems.
By the end of this lesson, you will be able to:
- Explain how fixed-size chunking works.
- Distinguish character-based and token-based chunk sizes.
- Understand why chunk overlap is often used with fixed-size chunks.
- Implement simple fixed-size chunking with Python.
- Understand the trade-off between chunk size, context, precision, and retrieval cost.
- Choose and evaluate a practical starting size instead of assuming one size fits every dataset.
What Is Fixed-size Chunking?
Fixed-size chunking divides text into pieces according to a predetermined size. The size might be measured in characters, words, or tokens.
For example, if you choose 500 characters, the chunker repeatedly takes up to 500 characters from the source text until the document is consumed. The method does not first try to understand whether a sentence or section has finished.
How It Works
The basic algorithm is straightforward: choose a size, start at the beginning of the text, take that many units, move forward, and repeat.
One long document
βGenerative AI uses models to understand and generate content. Retrieval systems can add external knowledgeβ¦β
Fixed windows
Chunk 1 = first window, Chunk 2 = next window, Chunk 3 = next window, and so on.
If the document has 2,400 characters and the chosen size is 600 characters, a simple non-overlapping implementation produces up to four chunks.
Character-based Chunking
Character-based chunking is the easiest version to understand. The chunk size is a number of characters.
text = "Chunking splits long documents into smaller pieces for retrieval."
chunk_size = 20
chunks = [
text[i:i + chunk_size]
for i in range(0, len(text), chunk_size)
]
for number, chunk in enumerate(chunks, start=1):
print(f"Chunk {number}: {chunk}")Character Size Does Not Mean Token Size
A character count and a token count are different measurements. Language models process text as tokens, while a character-based chunker counts characters.
Token-based Fixed-size Chunking
Instead of counting characters, a system can tokenize the text and create chunks containing a maximum number of tokens. This is useful when you need tighter control over model context limits.
Python: Fixed-size Words
Here is a simple word-based version. It is useful for learning the algorithm, but production systems should consider the tokenizer and document structure.
text = "Python makes fixed size chunking easy to understand."
words = text.split()
chunk_size = 4
chunks = [
" ".join(words[i:i + chunk_size])
for i in range(0, len(words), chunk_size)
]
for chunk in chunks:
print(chunk)This version groups four words at a time. Notice that it can still cut across a sentence or logical section.
The Problem With Hard Boundaries
A fixed window can end in the middle of a sentence, list item, code example, or explanation.
Chunk 1: βThe refund is available within 30 days if the product is returnedβ¦β
Chunk 2: ββ¦unused and in its original packaging.β
A later chunking strategy can keep the complete condition together instead of splitting the idea at an arbitrary position.
Fixed-size chunking is therefore simple rather than universally intelligent. It can be a strong baseline, but it should be tested against your actual documents and questions.
Why Use Overlap?
Overlap repeats some content between neighboring chunks. The goal is to reduce the chance that an important idea is split exactly at a chunk boundary.
Chunk 2: 501β1000
Chunk 2: 401β900
Python: Fixed Size With Overlap
A simple character-based implementation can move forward by chunk_size - overlap instead of the full chunk size.
text = "Fixed size chunking can use overlap to preserve nearby context."
chunk_size = 30
overlap = 8
step = chunk_size - overlap
chunks = [
text[i:i + chunk_size]
for i in range(0, len(text), step)
]
for number, chunk in enumerate(chunks, start=1):
print(f"Chunk {number}: {chunk}")Choosing a Chunk Size
There is no universally correct fixed size. A useful starting point depends on the document type, question style, embedding model, reranker, context window, and retrieval evaluation.
Focused
Can improve precision, but related information may be split across multiple chunks.
Balanced
Often a practical baseline because it keeps more context without making every result huge.
Context-rich
Preserves more surrounding information, but can add unrelated text to retrieved context.
- How long are the useful answers?
- Are questions usually about one paragraph or a whole section?
- How much context can the generator handle?
- Several chunk sizes
- Different overlap values
- Real user questions
Fixed-size Chunking in RAG
In a RAG pipeline, fixed-size chunks are usually created during ingestion. Each chunk can then be embedded, stored, and retrieved later.
If the warranty sentence is split between two chunks, retrieval may need to return both pieces for the model to see the complete answer. Overlap can reduce this boundary problem.
Metadata for Fixed-size Chunks
Chunking should not discard the identity of the original source. Store useful metadata with every chunk so retrieval results can be traced back to the document.
When Fixed-size Chunking Works Well
When documents have relatively consistent formatting, fixed windows can provide a simple and repeatable baseline.
It is easy to implement and gives you something measurable before adding more sophisticated chunking logic.
Content such as repeated records or consistently structured descriptions may tolerate fixed boundaries well.
It becomes less attractive when document structure carries important meaning, such as headings, tables, procedures, or tightly connected sections.
Common Mistakes
Different datasets and question patterns can need different chunk sizes.
Too much repetition increases storage and processing without necessarily improving retrieval.
β500β is meaningless unless you know whether it means characters, words, or tokens.
A fixed window can split important ideas, headings, lists, and examples.
Without source information, displaying citations and tracing results becomes harder.
Visual inspection is not enough. Test real questions and retrieved chunks.
How to Evaluate Fixed-size Chunking
Compare several configurations instead of guessing. For example, test multiple chunk sizes with and without overlap against a representative question set.
Check whether top results actually discuss the requested topic.
Check whether needed information was split across chunks.
Check whether retrieved chunks contain too much unrelated context.
Fixed-size vs Other Chunking Methods
Predictable windows
Simple and consistent, but boundaries may cut through meaning.
Meaningful boundaries
Methods such as sentence, recursive, or semantic chunking try to preserve document meaning more carefully.
Fixed-size chunking is valuable because it is easy to understand, implement, benchmark, and improve. Later lessons will explore alternatives that make boundary decisions more context-aware.
Build a Fixed-size Chunker
Take a paragraph of documentation and design two configurations.
- Create a simple fixed-size chunker using Python.
- Test one configuration without overlap.
- Test a second configuration with a small overlap.
- Ask five questions about the source document and inspect the retrieved chunks.
- Record which configuration gives the most relevant and complete context.
Quick Quiz
A. Splits text using a predetermined size
B. Summarizes every document
C. Removes all metadata
A. Characters
B. Tokens
C. Both A and B
A. To preserve some neighboring context
B. To guarantee perfect retrieval
C. To eliminate embeddings
A. They can split an important idea
B. They cannot create vectors
C. They always require a GPU
30-Second Recap
Key Takeaways
- Fixed-size chunking creates predictable text windows using a chosen size.
- The size can be measured in characters, words, or tokens; these units are not interchangeable.
- Fixed boundaries can split meaningful content, so overlap can help preserve nearby context.
- Chunk size and overlap affect retrieval quality, context size, storage, and processing cost.
- Use fixed-size chunking as a practical baseline, then compare it with other strategies using real retrieval tests.