News / Learn / Retrieval-augmented generation (RAG)
How should you split documents into chunks?
You'll be able to choose a chunking strategy and chunk size that keeps each piece of text useful for retrieval.
In plain English
Chunking means cutting a long document into smaller pieces before you store them for search. A retrieval system finds the pieces that match a question and passes them to a language model. The size and shape of each piece decide what the model can see and whether the right answer gets found at all.
Why it matters
Chunking sets the ceiling on your RAG system's quality. If a chunk is too large, it dilutes the signal and wastes space in the context window. If it is too small, the answer gets split across pieces and the model never sees the full thought. Most retrieval failures that look like model failures start here.
How it works
A chunk is embedded as one vector, so the vector has to represent everything in that chunk. Long chunks blur several topics into one vector, which makes matching weaker. Short chunks are precise but can lose the context that gives a sentence its meaning, such as which product, date, or section it belongs to.
The simplest method splits text into fixed-size windows measured in tokens or characters, with some overlap so that sentences at the edges are not lost. Recursive splitting is a common refinement. It tries to split on paragraph breaks first, then sentence breaks, then words, so that chunks end at natural boundaries when they can.
Structure-aware chunking uses the document itself. Headings, sections, list items, code blocks, and table rows give you boundaries the author already drew. Keeping the heading path with each chunk, such as 'Billing Refunds Exceptions', gives the embedding and the model the context a bare paragraph lacks.
You can also store metadata with each chunk, such as the document title, section, date, or source URL. Retrieval can then filter on that metadata, and the model can cite where an answer came from.
A concrete example
A simple splitter with overlap and a heading prefix. Real pipelines often use a library, but the idea is the same.
def chunk_text(text, source, max_chars=1200, overlap=150):
chunks = []
start = 0
while start < len(text):
end = min(start + max_chars, len(text))
# try to end on a sentence boundary
if end < len(text):
cut = text.rfind(". ", start, end)
if cut > start + max_chars // 2:
end = cut + 1
piece = text[start:end].strip()
if piece:
chunks.append({
"text": piece,
"source": source,
"start": start,
})
if end == len(text):
break
start = max(end - overlap, start + 1)
return chunks
docs = chunk_text(open("refund_policy.md").read(), source="refund_policy.md")
print(len(docs), "chunks")A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Using one chunk size for every document Test chunk sizes on your own documents and questions. A policy manual and a code reference rarely need the same size.
- Cutting through the middle of a sentence or table Split on paragraph, heading, or row boundaries first. Only fall back to hard character cuts when nothing else works.
- Dropping the context a chunk depends on Prepend the document title and heading path to each chunk, or store them as metadata that is added to the text sent to the model.
- Skipping overlap entirely Add a modest overlap so that a fact spanning a boundary still appears whole in at least one chunk.
Going deeper
Learn how embedding models handle input length, since that sets a hard limit on chunk size. Then study parent-child retrieval, where you search small chunks but return their larger parent section to the model.
FAQ
What chunk size should I start with?
Start with a few hundred tokens, with a small overlap, and measure. Then adjust based on whether retrieved chunks contain the answer and enough surrounding context. There is no universal number.
Should I split by tokens or characters?
Tokens match how models count text, so they give more predictable sizes. Characters are simpler to implement. Either works if you measure consistently.
Does a bigger context window mean I can use bigger chunks?
It lets you send more text, but larger chunks still blur their embeddings and can bury the relevant part. Retrieval precision often gets worse, not better.
Do I need to chunk every document type the same way?
No. Prose, code, tables, and markdown with headings each have natural boundaries. Use a separate splitter for each type where you can.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.