HOW-TO · RAG
How to Optimize Chunk Size and Overlap Strategy
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
RAG pipeline with configurable chunk parameters
What this does
Chunk size and overlap are the two parameters with the largest impact on retrieval quality. Too small—context is lost. Too large—irrelevant text dilutes signal. Overlap compensates for boundary effects where a concept spans two chunks. This guide provides an empirical method to find the right values for your corpus.
Steps
Define a sweep grid for chunk size and overlap.
chunk_sizes = [256, 512, 768, 1024] overlaps = [32, 64, 128, 256] configs = [{"chunk_size": cs, "chunk_overlap": co} for cs in chunk_sizes for co in overlaps] print(f"Testing {len(configs)} configurations")Run a retrieval evaluation for each configuration.
from langchain.text_splitter import RecursiveCharacterTextSplitter def evaluate_config(config, docs): splitter = RecursiveCharacterTextSplitter(**config) chunks = splitter.split_documents(docs) recall = 0.0 # compute actual recall against ground truth return {"config": config, "recall": recall, "chunk_count": len(chunks)} results = [evaluate_config(c, docs) for c in configs] best = max(results, key=lambda r: r["recall"]) print(f"Best config: {best['config']} with recall {best['recall']:.3f}")Apply the winning configuration.
production_splitter = RecursiveCharacterTextSplitter( chunk_size=best["config"]["chunk_size"], chunk_overlap=best["config"]["chunk_overlap"], ) print(f"Production chunk count: {len(production_splitter.split_documents(docs))}")
- Record the local run evidence. Save the exact command, runtime or package version, model name if applicable, and observed output so the result can be reproduced later.
Verification
python -c "
from langchain.text_splitter import RecursiveCharacterTextSplitter
s = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
c = s.split_text('A' * 2000)
print(f'Chunks: {len(c)}, first: {len(c[0])}, last: {len(c[-1])}')
"
# Expected: Chunks: 4, first: 500, last: <=550
Common failures
- Recall drops sharply at 1024 tokens. Embedding model truncates long inputs. Cap chunk size at 512 or 768.
- Identical chunks across configs. Corpus too small or repetitive. Use a more diverse evaluation corpus.
- Overlap too large wastes storage. Keep overlap ~15% of chunk size.
- Chunk boundaries cut mid-sentence. Move sentence-ending punctuation earlier in the separator list.
- Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
- Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.
Related guides
RELATED GUIDES