Code Safari

Chapter 58·Intermediate·9 min read

Chunking for RAG: How to Split Documents Well

How chunking shapes RAG quality — why you split documents, the chunk-size trade-off, overlap, and structure-aware splitting. A plain-English guide to the most under-rated step in retrieval.

June 30, 2026

We can now search text by meaning. But a question we glossed over: search what, exactly? You don't embed a whole 80-page handbook as one vector — that would be useless. You split it into chunks first. Chunking sounds like a boring preprocessing detail; it's actually one of the biggest levers on whether RAG works at all.

Why you chunk in the first place

Two reasons force you to split documents into pieces:

  1. Precision. RAG's job is to hand the model the specific passage that answers a question. If you retrieve a whole manual, the answer is buried and the context window overflows with irrelevant text.
  2. Embedding quality. As we saw with embeddings, a vector summarizes the meaning of its text. Embed a long document covering ten topics and you get one muddy vector that's a blurry average of all of them — close to nothing in particular.

So you split documents into chunks small enough to be about one thing, embed each chunk, and retrieve at that granularity.

The chunk-size trade-off

How big should a chunk be? This is the central tension, and both extremes fail:

Chunk sizeUpsideDownside
Too largePlenty of context per chunkBlurry embedding; retrieves off-topic text; wastes context
Too smallSharp, precise embeddingMay not contain enough to answer; splinters ideas
Tiny (1 sentence)
low
Small (~paragraph)
high
Medium (~page)
med
Huge (chapter)
poor
Retrieval quality is a sweet spot, not 'smaller is better'

(Illustrative — the exact sweet spot depends on your content.) The takeaway is that there's a middle band where chunks are big enough to stand on their own but small enough to stay focused. A paragraph-ish size is a common starting point.

Overlap: don't cut ideas in half

Split a document at fixed boundaries and you'll eventually slice an explanation right down the middle — the setup in one chunk, the payoff in the next, and neither chunk fully answers the question. The fix is overlap: let consecutive chunks share a little text at their edges.

...end of chunk A
shared overlap
start of chunk B...
Overlapping chunks so no idea falls into a crack between them

A modest overlap (say, a sentence or two) means an idea sitting on a boundary survives in at least one complete chunk. Too much overlap, though, bloats your index with near-duplicates — another balance to strike.

Split on structure, not character counts

The crudest method — cut every N characters — is also the worst, because it ignores meaning and happily splits mid-sentence. Structure-aware chunking does much better by splitting on the document's own seams:

  • on headings and sections, so each chunk is one topic;
  • on paragraphs, which are already coherent units;
  • for code, on functions or classes rather than line counts;
  • for tables or lists, keeping each row or item intact.

The principle is simple: a good chunk is about one coherent thing. Respecting the document's structure gets you far closer to that than any fixed length, because the author already grouped related ideas for you.

Chunking sets the ceiling

Here's why this unglamorous step deserves real attention: if the answer to a question never lands cleanly inside a single retrievable chunk, no downstream step can recover it. A perfect embedding model and a flawless ranker can only work with the chunks you gave them. Bad chunking quietly caps the quality of the entire system, and the failures are hard to spot — retrieval just returns almost the right thing.

So treat chunking as a first-class design decision, not a default. And because the best strategy depends entirely on your content — dense reference docs, conversational guides, and source code all behave differently — test it on real queries rather than trusting a magic number from a blog post.

Recap

  • You chunk documents so retrieval returns the specific passage that answers a question, not a whole file.
  • Chunk size is a trade-off: too large gives blurry embeddings, too small loses the context needed to answer.
  • Overlap keeps ideas that sit on a boundary from being split and lost.
  • Structure-aware splitting (on headings, paragraphs, functions) beats blind fixed-length cuts.
  • Chunking sets the ceiling on RAG quality — and the best strategy depends on your content, so test it.

We have meaningful chunks, each as a vector. Now we need somewhere to store millions of them and search them fast. That's the job of a vector database. Continue to Vector Databases.

Chunking for RAG: How to Split Documents Well | Code Safari