Skip to content

Latest commit

 

History

History
115 lines (72 loc) · 11.6 KB

guide-draft.md

File metadata and controls

115 lines (72 loc) · 11.6 KB

How to Choose a Chunking Strategy

Two questions decide most of a chunking setup. The first is how much text you can afford to send to the model for each answer, which this guide calls the context budget. The second is how much a passage depends on the rest of its document to make sense, which this guide calls context dependency. Every recommendation here follows from those two answers.

The numbers come from three public datasets with human-labeled answer passages: SQuAD (Wikipedia articles), RepliQA (company policies, incident reports, and local news about made-up entities), and Qasper (NLP research papers). Every setup is compared at the same token budget, so a setup that sends more text does not get credit for it. The main metric is how often the passage that answers the question reaches the model. All code and results are in the companion repository.

Do You Need to Chunk?

Chunking is one of three ways to get the right text to a model. You can put the whole document in the prompt, let an agent search the raw files with a tool like grep, or split documents into chunks and retrieve the relevant ones.

Sending the whole document works when you already know which document the question is about and it fits your budget. An average RepliQA document is about 1,250 tokens, so sending it whole is cheap. An average SQuAD article is about 8,000 tokens and a Qasper paper about 5,000, so every answer would cost that much. Most applications also do not know which document holds the answer, and searching a whole collection is the problem retrieval solves.

Grep needs no index and finds exact strings such as IDs and error codes well. It depends on the question using the same words as the document. In the original datasets, where annotators wrote questions while reading the paragraph, plain grep found the answer within a 1,000-token budget for 75% of SQuAD questions and 86% of RepliQA questions. To test the realistic case, an LLM rewrote 300 questions per dataset in other words, keeping names and numbers. The share of question words that also appear in the answer passage fell from about 60% to about 20%.

The table shows the share of answers found within 1,000 tokens, for the same 300 questions in both wordings.

Retrieval SQuAD, original SQuAD, reworded RepliQA, original RepliQA, reworded
Grep 74% 30% 86% 29%
Keyword search (BM25) 92% 54% 91% 50%
Dense vectors, 256-token chunks 88% 75% 73% 59%
Dense vectors, 128-token chunks 90% 81% 84% 68%
Hybrid (dense and BM25) 95% 76% 92% 63%

Grep and keyword search lose most of their hits when users phrase questions in their own words. Dense retrieval loses the least.

Grep finds the answer when the user repeats the document's words, and misses it most of the time when they do not. An agent changes that, because it can rephrase its searches. An LLM with two tools, grep and a tool that reads a range of lines, searched until it answered, for at most 10 steps. It ran on 100 questions per dataset.

Questions Answer found Median tokens per answer Mean tokens per answer Steps
SQuAD, original 99% 4,000 14,100 2.8
SQuAD, reworded 96% 7,600 26,800 3.8
RepliQA, original 98% 5,100 8,000 3.0
RepliQA, reworded 94% 7,600 31,900 4.4
Qasper, within the paper 95% 8,500 15,200 4.1

The agent finds nearly every answer, but each answer takes three to four LLM calls, and a few hard questions cost far more tokens than the median.

Chunked retrieval sends one fixed budget per answer, in a single call. At a 4,000-token budget with hybrid search it found 99.7% of original SQuAD answers and 90% of reworded ones, and 96% and 76% on RepliQA. The agent is more accurate on reworded questions, at about twice the tokens in a typical case and up to eight times on average, plus the latency of several calls in a row. Chunking pays its cost once, when you index.

A common argument says agents no longer need retrieval: store company knowledge as markdown files, one concept per file with sources and links, and let the agent grep and read. The results support part of that. On a few hundred well-organized files, an agent with a filesystem finds most answers, and there is no index to keep in sync. Two parts of the argument are easy to miss. Writing one concept per file, with a title and links, is chunking by hand: it is structure-aware boundaries with a title prefix, and the agent works well because someone already did that work. Most collections arrive as PDFs, contracts, and tickets instead. The other part is cost: every answer is several model calls that re-read raw text, so the cost grows with the number of questions and the size of the collection, while retrieval pays once at indexing.

A grep agent is a good fit when the collection is small, questions are rare, or users search for exact identifiers. Chunked retrieval is the better fit when you answer many questions over a large collection and need a predictable cost per answer. The two combine well: an agent can use vector search as one of its tools. If your users search for exact identifiers, keep keyword matching in the mix through hybrid search.

Start With Your Context Budget

The model's context window is the hard limit. Your budget is what you can afford per answer in cost, latency, and answer quality, and it is usually much smaller. The budget also has to hold your instructions, the question, and any tool output, so the space left for retrieved text is smaller again.

Divide that space by the size of your chunks and you get the number of chunks you can send. A 4,000-token budget fits about 15 chunks of 256 tokens or about seven chunks of 510 tokens. The choice is between more small chunks and fewer large ones.

Small chunks won on the two datasets whose passages stand on their own. At a 1,000-token budget, the share of answers found was:

Chunk size SQuAD RepliQA Qasper
128 tokens 88% 84% 46%
256 tokens 86% 77% 51%
510 tokens 65% 59% 40%

Large chunks spend the budget on text around the answer. On Qasper, where passages depend on their paper, 256 tokens beat 128.

Overlap between neighboring chunks is the most common default advice, often stated as 10% to 20%. At an equal budget it lowered the share of answers found on every dataset. With 64 tokens of overlap on 256-token chunks, the share at 1,000 tokens fell from 86% to 84% on SQuAD, from 77% to 68% on RepliQA, and from 51% to 45% on Qasper. Overlap repeats text the model has already seen, and that repetition takes budget away from new chunks.

Check one more limit before you choose a size: the embedding model's input length. Many open embedding models stop reading at 512 tokens or fewer and drop the rest without any warning. For example, all-MiniLM-L6-v2 reads 256 tokens and bge-small-en-v1.5 reads 512, including two special tokens. Measure chunk sizes with the embedding model's own tokenizer, because most text splitters count characters by default, and compare your largest chunk with the model's limit.

Check How Much Context Your Documents Carry

A chunk depends on its document when it cannot be understood on its own. It might refer back to something defined earlier ("the employee", "our model"), need the document's structure (which section, which table), or be ambiguous without the title ("revenue grew 3%" for which company?). These categories come from the ConTEB benchmark.

To measure dependency, two LLMs from different families (DeepSeek v4.1 Flash and Claude Haiku 5.5) each read the chunk that holds the answer for 100 random questions per dataset, without its document, and judged whether the chunk made sense on its own. The two agreed on about 80% of chunks. A person also labeled 30 of the chunks and agreed with DeepSeek on 27 and with Claude Haiku on 23, so the table leads with DeepSeek's rates and gives Claude Haiku's more lenient rates as the upper end. The person was the strictest of the three.

Dataset Self-contained answer chunks With title and section prefix
SQuAD (Wikipedia) 71% to 82% 75% to 91%
RepliQA (policies, reports, news) 60% to 74% 71% to 82%
Qasper (research papers) 10% to 23% 22% to 30%

Both models rank the datasets the same way, and both find that a prefix helps. Most failures are references back to earlier text: 25% of SQuAD chunks, 35% of RepliQA chunks, and 90% of Qasper chunks, as judged by DeepSeek.

You can run the same test on your own documents. Pick 20 random chunks, read each one without its document, and count how many you could answer a question from. Have an LLM do it if you want a larger sample, and check a handful of its answers by hand. If most chunks pass, your documents have low dependency. If most fail, as with research papers or contracts, treat them as high dependency.

Adding the document title and section heading to each chunk made more chunks understandable, mostly by removing ambiguity about which entity a chunk is about. It did not help retrieval in these runs: on RepliQA the share of answers found at 1,000 tokens fell from 77% to 71%, because the prefix costs tokens in every chunk. A prefix helps the model read a chunk more than it helps search find it.

The Methods, Grouped by What They Do

Recommendations by Budget and Dependency

Check Whether Your Choice Works

When to Adjust

Related Reading