π Understanding RAG Chunking: Fixed-Size, Overlapping, Semantic, and Sentence-Based Chunking with Python
As Large Language Models (LLMs) become increasingly integrated into enterprise applications, Retrieval-Augmented Generation (RAG) has emerged as one of the most effective ways to build AI systems that answer questions using private and domain-specific data.
Whether you're building an AI chatbot, document search system, or internal knowledge assistant, the quality of your responses depends heavily on one often-overlooked step: chunking.
A poorly chunked document can lead to irrelevant retrieval, missing context, and inaccurate answers. A well-designed chunking strategy, on the other hand, dramatically improves retrieval accuracy and reduces hallucinations.
In this article, we'll explore four widely used chunking techniques, understand their strengths and trade-offs, and implement them using Python.
Why Does Chunking Matter?
Imagine you have a 300-page PDF containing company policies. Sending the entire document to an LLM for every question is neither practical nor efficient due to context window limitations.
Instead, a RAG pipeline works like this:
Documents
β
βΌ
Chunking
β
βΌ
Embeddings
β
βΌ
Vector Database
β
βΌ
Similarity Search
β
βΌ
Relevant Chunks
β
βΌ
Large Language Model
β
βΌ
Answer
The quality of the retrieved chunks directly influences the quality of the final response.
1. Fixed-Size Chunking
Fixed-size chunking is the simplest strategy. The document is divided into chunks containing a fixed number of characters, words, or tokens.
Python Example
text = """
Power Automate integrates with SharePoint.
It supports approvals.
It works with Microsoft Teams.
"""
chunk_size = 50
chunks = [
text[i:i + chunk_size]
for i in range(0, len(text), chunk_size)
]
for chunk in chunks:
print(chunk)
Advantages
- Easy to implement
- Fast indexing
- Consistent chunk sizes
Limitations
- May split sentences in the middle
- Important context can be lost
Best suited for: Learning RAG concepts and rapid prototyping.
2. Fixed-Size Chunking with Overlap
One limitation of fixed-size chunking is losing context at chunk boundaries. This is solved by introducing overlap, where adjacent chunks share part of their content.
Python Example
chunk_size = 50
overlap = 15
chunks = []
start = 0
while start < len(text):
chunks.append(text[start:start + chunk_size])
start += chunk_size - overlap
for chunk in chunks:
print(chunk)
Instead of completely separate chunks, consecutive chunks contain overlapping content, preserving context for downstream retrieval.
Advantages
- Better context preservation
- Improved retrieval quality
- Most common production strategy
Limitations
- Duplicate embeddings
- Higher storage requirements
Best suited for: Enterprise chatbots, document Q&A, and production RAG applications.
3. Semantic Chunking
Rather than splitting based on size, semantic chunking groups text based on meaning. Related content stays together even if chunk lengths vary.
Python Example
from langchain_experimental.text_splitter import SemanticChunker
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
splitter = SemanticChunker(embeddings)
documents = splitter.create_documents([text])
Semantic chunking uses embeddings to identify logical boundaries in the document, producing chunks that are more meaningful for retrieval.
Advantages
- Preserves topic coherence
- High retrieval accuracy
- Reduces irrelevant context
Limitations
- Computationally expensive
- Requires embedding models during preprocessing
Best suited for: Legal documents, technical manuals, policy documents, and enterprise knowledge bases.
4. Sentence-Based Chunking
Instead of splitting by characters or tokens, sentence-based chunking groups complete sentences together.
Python Example
import nltk
sentences = nltk.sent_tokenize(text)
chunk_size = 2
chunks = [
" ".join(sentences[i:i + chunk_size])
for i in range(0, len(sentences), chunk_size)
]
print(chunks)
Since sentence boundaries are preserved, the resulting chunks are easier for both retrieval systems and LLMs to understand.
Advantages
- Never breaks sentences
- Better readability
- Simple implementation
Limitations
- Chunk sizes vary
- Doesn't automatically preserve topic boundaries
Best suited for: Blogs, FAQs, tutorials, and product documentation.
Comparing the Techniques
| Technique | Context Preservation | Retrieval Quality | Complexity | Recommended Use |
|---|---|---|---|---|
| Fixed-Size | ββ | ββ | β | Learning and prototypes |
| Fixed-Size + Overlap | ββββ | ββββ | ββ | Production RAG systems |
| Semantic | βββββ | βββββ | ββββ | Enterprise AI applications |
| Sentence-Based | βββ | βββ | ββ | Documentation and articles |
Restricting the LLM to Your Documents
One of the greatest strengths of RAG is that you can instruct the model to answer only using the retrieved document context.
A typical prompt looks like this:
Answer only using the provided document context.
If the answer is not available in the retrieved documents, respond:
"I couldn't find this information in the provided documents."
Do not use external knowledge.
This significantly reduces hallucinations and ensures responses remain grounded in trusted information.
Which Chunking Strategy Should You Choose?
There isn't a universal solution.
- Choose Fixed-Size Chunking if you're learning or building a proof of concept.
- Choose Fixed-Size Chunking with Overlap for most production RAG applications because it offers an excellent balance between simplicity and retrieval quality.
- Choose Semantic Chunking when maintaining topic coherence is essential, especially for enterprise or domain-specific content.
- Choose Sentence-Based Chunking for documentation, blogs, and structured textual content where preserving sentence boundaries matters.
Final Thoughts
Chunking is much more than simply dividing text into smaller pieces. It is the foundation of an effective RAG system.
The chunking strategy you choose directly impacts retrieval accuracy, response quality, latency, and token consumption. While fixed-size chunking is a great place to start, production-grade AI systems often combine overlapping chunks, semantic boundaries, and intelligent retrieval techniques to deliver more reliable results.
If you're building RAG applications, invest time in experimenting with chunking strategiesβit's one of the highest-impact optimizations you can make.
In the next article, we'll explore Embeddings, Vector Databases, and Similarity Search to complete the RAG pipeline.
Happy Building! π
Top comments (0)