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.
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 RecursiveCharacterTextSplittertext = ("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.'
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.
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.