AI Hints
Home›LangChain Tutorial›Chunks and Embeddings
LangChain · Lesson 62 of 120

Chunk Size and Overlap

Getting it right.

⏱ 2 min read▶ Video at 1:09:49🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

How big should a chunk be? Too small, and a chunk loses its meaning. Too big, and it mixes several topics together. Overlap helps when a key sentence sits on a boundary.

Chunk Size and Overlap
From the video: Chunk Size and Overlap
  • Too small: no meaning
  • Too big: mixed topics
  • Overlap at boundaries

Seeing overlap

Here's one short paragraph, split into chunks of sixty characters, first without overlap, then with fifteen characters of overlap. Run it. With overlap, the end of each chunk is repeated at the start of the next, so a sentence cut in half still appears whole in one of them.

overlap.pypython
from langchain_text_splitters import RecursiveCharacterTextSplitter

text = ("Riverside Books opened in 1998. We sell new and used books. "
        "The shop is open seven days a week. Delivery is free over £25.")

for overlap in [0, 15]:
    splitter = RecursiveCharacterTextSplitter(chunk_size=60, chunk_overlap=overlap)
    print(f"overlap={overlap}:")
    for chunk in splitter.split_text(text):
        print("   ", repr(chunk))
Output
overlap=0:
    'Riverside Books opened in 1998. We sell new and used books.'
    'The shop is open seven days a week. Delivery is free over'
    '£25.'
overlap=15:
    'Riverside Books opened in 1998. We sell new and used books.'
    'used books. The shop is open seven days a week. Delivery is'
    'Delivery is free over £25.'

Starting points

There's no perfect size, but here are starting points. For short policy documents, a few hundred characters works well. For longer articles, around a thousand characters, with a hundred or two of overlap. For code, split by functions. Then test with real questions, and adjust.

ContentChunk sizeOverlap
Short policies, FAQs200–500 chars~10%
Articles, manuals~1000 chars100–200
Codeby function/classnone
Alwaystest and adjust

Summary

Chunk size balances meaning against focus, and overlap protects sentences on boundaries. Start with sensible defaults, and test. Next, turning text into numbers that capture meaning.

Key takeaways
  • Not too small
  • Not too big
  • ~10% overlap
Next up: Embeddings — meaning as numbers