All posts

RAG chunking strategy: how to choose chunk size, overlap and boundaries

How to pick a RAG chunking strategy: fixed-size vs structure-aware splitting, chunk size and overlap trade-offs, and how to test the choice on your own documents.

CodeKrypt 5 min read

Most RAG quality problems get blamed on the model, then on the vector database, and only last on the step that quietly decides what the model can ever see: how the documents were cut into pieces. A weak RAG chunking strategy produces chunks that either bury the answer in noise or slice it in half, and no amount of prompt tuning recovers information that was never retrieved.

This post covers the decisions that matter (boundaries, size, overlap, added context), the trade-offs of each, and how to pick by measurement instead of copying a default from a tutorial.

Why does chunking change answer quality?

Retrieval works on chunks, not documents. Each chunk is embedded as one vector, so it competes for a slot in your top-k results as a single unit. That creates two opposite failure modes:

Anthropic's write-up on contextual retrieval makes the same point: documents are typically split into pieces of no more than a few hundred tokens, and chunk size, boundaries and overlap all affect retrieval performance.

Which chunking strategies are there?

StrategyHow it splitsStrengthWeakness
Fixed-sizeEvery N tokens or charactersTrivial, predictableCuts mid-sentence and mid-table
Recursive / separator-basedTries paragraphs, then sentences, then words until chunks fitRespects natural breaks, still simpleIgnores document structure beyond whitespace
Structure-awareSplits on headings, list items, table rows, code blocksChunks match how authors organised ideasNeeds a parser per format
SemanticStarts a new chunk where embedding similarity between sentences dropsCan follow topic shiftsExtra ingestion cost, results vary by corpus
Parent-childRetrieve small chunks, return the larger section they belong toPrecise matching, fuller context for the modelTwo levels to store and keep in sync

A sensible order of effort: start with recursive splitting, move to structure-aware splitting for the document types that matter most (contracts, manuals, policies, help-centre articles), and only try semantic or parent-child approaches if your evaluation shows a gap the simpler methods leave.

How do you pick chunk size?

Size is a trade-off between precision and context, and the right point depends on the questions people ask:

Also check the limits you cannot change: the maximum input length of your embedding model, and how many retrieved chunks fit in the prompt alongside the question and instructions. Pricing is per token on both embedding and generation, so larger chunks also raise cost on every query. Our post on reducing LLM API costs covers how that adds up.

Resist picking one number for everything. A FAQ, a legal contract and a code repository do not share a sensible chunk size, and a RAG system over mixed content often needs a per-document-type configuration.

Do you need chunk overlap?

Overlap repeats the end of one chunk at the start of the next, so a fact that straddles a boundary appears whole in at least one of them. It is a workaround for cutting in the wrong place.

The costs are real: duplicated text enlarges the index, and a query can return two near-identical chunks that waste slots in your top-k. If you split on headings and paragraphs, boundaries already fall where ideas change, and you can often use little or no overlap. If you must split mechanically, a modest overlap is cheap insurance. Test with and without it rather than assuming.

What do you do about chunks that lose their context?

Splitting removes the surrounding document. Two common repairs:

  1. Prepend metadata. Attach the document title, section heading and date to the chunk text before embedding. This is cheap and needs no model call.
  2. Add generated context. Anthropic's contextual retrieval has a model write a short, chunk-specific explanation (typically 50 to 100 tokens) from the full document, then prepends it before embedding and before building the BM25 index. In their reported experiments, measured as the share of relevant chunks missing from the top 20, this cut failed retrievals by 35% with contextual embeddings alone and by 49% when combined with contextual BM25. Adding reranking brought the reduction to 67%. Those figures are from Anthropic's own datasets, so treat them as evidence the technique can help, not as a number your corpus will reproduce.

Generated context costs a model call per chunk at ingestion, which is a one-off cost per document but a real one on a large corpus. Pair it with hybrid search and reranking, since the techniques address different failure points.

Special cases that break naive chunkers

How do you test a chunking strategy?

Treat it as an experiment:

  1. Collect 30 to 100 real questions, ideally from support tickets or search logs, each with the document passage that answers it.
  2. Ingest the corpus with two or three configurations (for example recursive at two sizes, and structure-aware).
  3. For each question, check whether the correct passage appears in the top-k chunks. Record that recall, plus how many prompt tokens the retrieved context used.
  4. Read the failures. Misses where the answer was split across chunks point to boundaries; misses where a chunk was retrieved but ambiguous point to missing context.

Store the chunking configuration with each index version so you can re-run the comparison when documents or embedding models change. This evaluation set doubles as the regression test described in LLM evals in CI.

What to do next

Write down your top 50 real questions before touching a chunk-size parameter. Run a recursive-splitting baseline, measure recall, then change one variable at a time: boundaries first, size second, overlap third, added context last. Stop when the gain per change stops justifying its ingestion cost.

If you are building a retrieval system for an AI product and want a second opinion on the design, see how we approach AI SaaS development and get in touch.

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

What is the best chunk size for RAG?
There is no universal best size. Anthropic describes chunks of no more than a few hundred tokens as typical, but the right size depends on your documents and questions. Build a small evaluation set and compare two or three sizes on recall before committing.
Do I need chunk overlap?
Overlap protects against a fact being cut in half at a boundary, but it duplicates content, grows the index and can return near-identical chunks. Structure-aware splitting on headings and paragraphs often reduces the need for it.
Is semantic chunking worth it?
Only if measurement says so. It adds an embedding step at ingestion and its gain over structure-aware splitting varies by corpus. Try it against a simple baseline on your own queries.
Can I fix bad chunks without re-chunking everything?
Partly. Adding chunk-specific context before embedding, as in Anthropic's contextual retrieval, helps chunks that lose meaning out of context, and reranking helps order the results. Neither fixes boundaries that split an answer in two.

Related reading

Chat on WhatsApp