Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 11

Fixed-Size Document Chunking with Overlap

Recommended resources

Welcome to the next step in preparing your knowledge base. In our previous lesson, we established the fundamental reasons why chunking is a critical, non-negotiable part of any RAG pipeline. You learned that it's essential for fitting within model context windows, improving the signal-to-noise ratio in semantic search, and helping the LLM generate more focused and faithful responses.

Now that the "why" is clear, we will shift our focus to the "how." This lesson covers the most direct approach to document splitting: fixed-size chunking with overlap. Your goal is to implement this strategy in Python. We will explore its underlying algorithm, the crucial role of its parameters, and the practical trade-offs involved in using it. This will provide you with a solid baseline technique for document processing.

The Core Idea: Slicing Text with Overlap

Fixed-size chunking is exactly what it sounds like: we slice a document into segments of a predetermined length, usually measured in characters or tokens. It's the simplest method because it doesn't try to understand the document's structure; it just cuts.

However, this simplicity comes with a risk. A rigid cut can happen mid-sentence or even mid-word, destroying the semantic context you're trying to preserve for the embedding model. To mitigate this, we introduce chunk overlap. This means that a small portion of text from the end of one chunk is repeated at the beginning of the next. This simple technique significantly increases the chance that a complete thought or sentence is captured fully within at least one chunk.

The image below illustrates this concept perfectly. A piece of text is split into three chunks, with a small overlap ensuring the context around the split points is preserved.

A visual representation of fixed-size chunking. The text is divided into three chunks of a specified size (35 characters), with a 4-character overlap between adjacent chunks to maintain context.

A visual representation of fixed-size chunking. The text is divided into three chunks of a specified size (35 characters), with a 4-character overlap between adjacent chunks to maintain context.

The following article provides a concise definition and a useful table comparing fixed-size chunking to other methods.

How to Create Fixed-Size Chunking

This resource from OneUptime clearly defines fixed-size chunking and its properties.

Read the section What is Fixed-Size Chunking? to solidify your understanding of this method's characteristics, such as its predictability and low complexity.


Implementing Fixed-Size Chunking

As a software engineer, you'll find the algorithm for fixed-size chunking quite straightforward. It operates on two main parameters: chunk_size and overlap. The key is to determine the "step size"—how far to advance the starting point for each new chunk. This is simply chunk_size - overlap.

The process is as follows:

  1. Start at the beginning of the text (index 0).
  2. Take a slice of length chunk_size. This is your first chunk.
  3. Advance the starting point by step_size.
  4. Take another slice of length chunk_size. This is your second chunk.
  5. Repeat until you have processed the entire text.

The article from OneUptime not only explains this algorithm but also provides a complete, from-scratch Python implementation that is perfect for understanding the mechanics.

How to Create Fixed-Size Chunking

This resource provides a full Python implementation of a FixedSizeChunker class.

First, review the section How It Works for a flowchart of the algorithm. Then, in the "Implementation in Python" section, examine the FixedSizeChunker class. Pay close attention to the init method, where the parameters are set, and then study the chunk_by_characters method, which contains the core logic for character-based splitting.

Now, let's see this FixedSizeChunker class in action. The code below demonstrates how to initialize the chunker and use it to split a sample document.

# Assuming the FixedSizeChunker class from the article is defined in a file 
# named fixed_size_chunker.py

from fixed_size_chunker import FixedSizeChunker

sample_text = """
Retrieval-Augmented Generation (RAG) is a technique that enhances
large language models by providing them with relevant context from
external knowledge bases. The process involves three main steps:
first, documents are split into chunks and embedded into vectors;
second, user queries are embedded and used to find similar chunks;
third, retrieved chunks are included in the LLM prompt to provide
context for generating accurate responses.
"""

# Initialize the chunker with a chunk size of 150 characters and an overlap of 30.
chunker = FixedSizeChunker(chunk_size=150, overlap=30)

# Generate chunks from the text.
char_chunks = chunker.chunk_by_characters(sample_text)

# Print the results
for chunk in char_chunks:
    print(f"\n--- Chunk {chunk.chunk_index} ---")
    print(f"Position: {chunk.start_index}-{chunk.end_index}")
    print(f"Length: {len(chunk.text)} chars")
    print(f"Text: '{chunk.text}'")

# --- Expected Output ---
#
# --- Chunk 0 ---
# Position: 1-151
# Length: 150 chars
# Text: 'Retrieval-Augmented Generation (RAG) is a technique that enhances
# large language models by providing them with relevant context from
# external knowledg'
#
# --- Chunk 1 ---
# Position: 121-271
# Length: 150 chars
# Text: 'e models by providing them with relevant context from
# external knowledge bases. The process involves three main steps:
# first, documents are split into chu'
#
# --- Chunk 2 ---
# Position: 241-391
# Length: 150 chars
# Text: 's are split into chunks and embedded into vectors;
# second, user queries are embedded and used to find similar chunks;
# third, retrieved chunks are include'
# ... and so on

Notice how the overlapping text (e.g., "e models by providing...") appears at the end of Chunk 0 and the beginning of Chunk 1, successfully bridging the context.

While building from scratch is excellent for understanding, in practice, you'll often use libraries like LangChain or LlamaIndex. The following video clip shows how to use LangChain's CharacterTextSplitter to achieve the same result.

Chunking in RAG (with hands-on in LangChain and LlamaIndex) - RAG video series

This video from AI Bites demonstrates fixed-size chunking using the popular LangChain library.

Watch the segment from the LangChain implementation, where the presenter uses the CharacterTextSplitter with chunk_size and chunk_overlap parameters to split a document. This shows you the practical application within a common framework.


Choosing Your Chunk Size and Overlap

You now know how to implement fixed-size chunking, but how do you choose the right chunk_size and overlap? These are not arbitrary numbers; they are crucial hyperparameters that directly affect your RAG system's performance.

The Overlap Strategy

A common rule of thumb is to set the overlap to be 10-20% of the chunk size. This usually provides a good balance between context preservation and data redundancy. The OneUptime article has an excellent section with visuals that compare different overlap percentages.

How to Create Fixed-Size Chunking

This section dives into strategies for selecting a good overlap size.

Read the section on Overlap Strategies. Pay special attention to the table of "Overlap Recommendations" and the "Visual Comparison" flowchart, which clearly illustrates how overlap helps preserve a sentence that would otherwise be split.

The Chunk Size Dilemma: Characters vs. Tokens

Choosing the chunk_size is even more critical. Your first instinct might be to pick a number of characters, like 200 or 500. However, there's a major pitfall here. The embedding models we use don't see characters; they see tokens. Each embedding model has a strict maximum sequence length (a token limit).

If you create a character-based chunk that, when tokenized, exceeds this limit, the model will simply truncate the input. All the text beyond the limit will be ignored during embedding, meaning that information becomes invisible to your retrieval system. This is a subtle but common failure mode in RAG pipelines.

The following video explains this problem exceptionally well and shows why defining chunk size in terms of tokens is a more robust approach.

How to Set the Chunk Size in Document Splitter | RAG | LangChain

This video from Prompt Engineering clearly explains the relationship between chunk size and the embedding model's token limit.

First, watch the explanation from the beginning. The presenter demonstrates how a character-based chunk size can easily exceed an embedding model's token limit, leading to silent data loss. Then, watch the solution from how to fix this, which shows how to use a tokenizer-aware splitter in LangChain to define chunk size directly in tokens.

This insight—that chunking should ideally be coupled to the tokenizer of your chosen embedding model—is a key step toward building a more reliable and performant RAG system.


Conclusion

In this lesson, you've moved from theory to practice by implementing the first and most fundamental chunking strategy. You now have a tangible method for breaking down large documents into pieces suitable for a RAG pipeline.

Here are the key takeaways:

  • Fixed-size chunking is a simple, fast, and deterministic method for splitting text based on a fixed length (e.g., characters).
  • Chunk overlap is essential to mitigate the risk of splitting sentences or ideas, preserving context across chunk boundaries.
  • The core algorithm iterates through text with a step size calculated as chunk_size - overlap.
  • A major pitfall is that character-based chunk_size does not directly correspond to tokens. Exceeding an embedding model's token limit will lead to truncation and information loss.
  • For more robust systems, it is better to define chunk sizes in terms of tokens rather than characters.

Fixed-size chunking is a valuable tool and an excellent starting point. However, its disregard for document structure can be a limitation. In our next lesson, we will explore a more sophisticated technique: recursive character-based chunking. This method attempts to split text along natural semantic boundaries like paragraphs and sentences, leading to more coherent chunks.