Back to Tutorials
LLM Application Development
Intermediate

Chunking Strategies for Better RAG

How you cut documents into pieces quietly decides what your AI can answer. Cut one yourself and watch an answer get sliced in half.

10 minInteractive: drag, click and try things as you read


Ask a company chatbot about its refund policy and it doesn’t read the whole policy. It reads a few small pieces of it, picked by a search, and answers from those. If the piece it gets holds half the answer, you get half an answer, delivered with full confidence. How you cut documents into pieces quietly decides what your AI can answer.

Why documents get cut up at all#

That chatbot is using RAG, retrieval-augmented generation: before the model answers, a search finds the passages most related to the question and pastes them into the prompt. The model then answers from what it was handed.

The search can’t hand over whole documents. A policy manual can run to hundreds of pages, and stuffing all of it into every prompt is slow and expensive, and it buries the one relevant line. So, ahead of time, every document is cut into chunks, and the search returns the best few chunks for each question.

Which means the search can only ever return a chunk you made. Bad cuts put a ceiling on your answers that no model upgrade will lift. Here’s a short booking policy to cut up yourself.

Try it
The document, questions and cutting are all real. To keep the demo light, search here ranks chunks by shared keywords (a classic method called BM25); most RAG systems search by meaning with embeddings, and the cutting problem is exactly the same.

Out of the box, the figure cuts every 200 characters, wherever that lands. Look at the first question’s answer, underlined in rose: “Cancel 7 to 13 days before departure and you get 50% back.” The cut lands in the middle of it. The chunk search picks ends partway through the sentence, before the one number that matters, so the model never sees the 50%.

Now try the insurance question. The chunk search picks ends at “must be added”, just before “within 48 hours”, the only part anyone asked about. The bags question gets a milder cut: it chops “Checked” in half, but both prices survive. Not every cut is fatal, and that’s what makes bad chunking hard to spot. Everything looks fine until a question lands on a bad boundary.

Three ways to cut#

The options in the figure are the three classic strategies, simplest first:

  • Fixed size: cut every N characters (or tokens), no matter what. Fast and simple, and blind to meaning, so it slices through sentences.
  • Sentences: pack whole sentences into each chunk until it’s full. A sentence is never split, though a thought that spans two sentences still can be.
  • Sections: follow the document’s own structure. Split at headings first, and only break a section into sentences if it’s too long for one chunk. This is the idea behind the “recursive” splitters in popular libraries.

Predict first

Go back to Fixed size, 200 characters. Without changing the strategy or the size, how could you stop the refund answer being cut?

Then why not just use big chunks?#

Switch to Sections and every question comes back whole. So why doesn’t everyone use one chunk per section, or per page?

Predict first

What’s the cost of making chunks much bigger?

So chunk size is a trade-off. Too small cuts answers in half; too big buries them. The sweet spot is a chunk that holds one complete idea and not much else.

A sensible starting point#

For most document collections, this gets you most of the way:

  1. Split on structure when the documents have it. Headings, then paragraphs, then sentences.
  2. Aim for a few hundred tokens per chunk, about a long paragraph or two, with roughly 10 to 20% overlap. (A token is about four characters of English.)
  3. Label every chunk with where it came from: the document title and section heading. A chunk that just says “Cancel 7 to 13 days before departure…” is much easier to find, and to trust, when it carries “Booking terms › Cancellations and refunds” with it.
  4. Test with real questions. Write down 20 questions your users actually ask, note where each answer lives, and check how often search returns the chunk that holds it. Change one setting at a time and measure again.

In code#

Fixed-size chunks with overlap are three lines of Python:

def chunk(text, size=800, overlap=120):
    step = size - overlap
    return [text[i:i + size] for i in range(0, len(text), step)]

For real projects, LangChain’s recursive splitter does the “structure first” approach for you. It tries to split on blank lines (paragraphs) first, then single line breaks, then spaces, only cutting smaller when a piece is still too big:

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=120)
chunks = splitter.split_text(document_text)

Both measure size in characters by default. Whatever you use, print a handful of chunks and read them. If a chunk wouldn’t make sense to a person reading it on its own, it won’t make sense to your model either.

What to remember

  • RAG search can only return chunks you made, so bad cuts cap the quality of every answer.
  • Cut on structure when you can, keep each chunk to one complete idea, and add some overlap so nothing near a boundary gets lost.
  • Too small cuts answers in half; too big buries them and costs more. Test with real questions to find the size that works.

Keep going

Sign in to save your place and keep a learning streak. Free, with Google.