
Chunk Overlap and Context Windows
Optimize chunk overlap to maintain context while avoiding redundancy in RAG systems.
Chunk Overlap and Context Windows
Balance context preservation with storage efficiency through strategic overlap.
Why Overlap Matters
# Without overlap - context lost
chunk1 = "The CEO announced new"
chunk2 = "product initiatives for 2026."
# With overlap - context preserved
chunk1 = "The CEO announced new product"
chunk2 = "new product initiatives for 2026."
Implementing Overlap
def chunk_with_overlap(text, size=500, overlap=100):
"""
Create overlapping chunks for better context.
"""
words = text.split()
chunks = []
start = 0
while start < len(words):
end = min(start + size, len(words))
chunk = ' '.join(words[start:end])
chunks.append({
'text': chunk,
'word_range': (start, end),
'has_overlap': start > 0
})
# Move window but keep overlap
start += (size - overlap)
return chunks
Module 10 complete! Continuing...