May we use cookies so Meta, Google, LinkedIn and ChatGPT see your visit and we learn which ads work? PrivacyMay we use cookies to see which pages help, and let Meta, Google, LinkedIn and ChatGPT see your visit, so we know which of our ads work? That way we spend less on ads that do not work. We do not sell your data, and the site works the same if you decline. You can change your answer at any time under Cookie settings at the foot of the page. Privacy policy
Loading...
System Design and AI Engineering, by Roni Das. 989 interactive lessons in two courses.: 770 in System Design and 219 in AI Engineering.
Chunking Strategies for RAG: How to Split Documents, With Size and Overlap Measured
What are the main chunking strategies for RAG?
There are four: fixed-size chunking cuts every N tokens, structural chunking cuts on headings and paragraphs, recursive chunking falls back from big boundaries to small ones until each piece fits, and semantic chunking cuts where the meaning between sentences changes. Start with fixed-size, around 512 tokens with 10 to 15% overlap, and move to structural, recursive or semantic only when a test on your own questions shows it retrieves better. In our lab on 466 questions, embedding search scored 0.908 at 256 words and fell to 0.725 at 4,096, so oversized chunks cost the most.
Answer last checked
Save your progress and get the AI Engineering cheat sheet PDF
AI Engineering Cheat Sheet: what our labs measured, one rule per result. 12 pages. Free with an account.
This guide answers 15 questions about chunking for RAG: what chunking is, the four main strategies including semantic and recursive chunking, how parent-child retrieval works, what chunk size and overlap to use, and how to tell when chunking is not your problem. The measurements come from our own lessons, run on this course's 111 AI lessons with 466 questions.
The four chunking strategies, and what each one costsFixed-size chunking always fits but often cuts mid-idea. Structural chunking keeps one thought per chunk but needs clean markup. Recursive chunking keeps structure and bounds the size. Semantic chunking finds topic shifts without structure but embeds the text just to decide the cuts. Start simple and move right only when a measurement on your own data pays for it.
The four chunking strategies, and what each one costsFixed-size chunking always fits but often cuts mid-idea. Structural chunking keeps one thought per chunk but needs clean markup. Recursive chunking keeps structure and bounds the size. Semantic chunking finds topic shifts without structure but embeds the text just to decide the cuts. Start simple and move right only when a measurement on your own data pays for it.
The short version
Chunk boundaries decide what retrieval can ever return, so they cap answer quality. Begin with fixed-size chunks and modest overlap, keep code, tables and transcripts in their natural units, store the document id and position on every chunk so you can fetch the parent section, and check your embedding model's real token window. Then measure size, overlap and strategy against a small set of real questions and keep what wins, and test a keyword search baseline before you tune anything else.
Readers in India pay in rupees; readers in the US, UK, Singapore, Australia and the Gulf see US dollar prices and can pay by card or PayPal. The pages for your region: India, Singapore, Australia, the Gulf and UK and Europe.
This page is the free part.
The course goes deeper on Chunking strategies for RAG
₹1,333 in India/$35 everywhere elseonce, for the whole course
The Generative AI and Agentic AI course covers Chunking strategies for RAG across a run of lessons, not one page. These 4 alone are about 218 minutes of step-by-step reading, every one with code you run in the browser, all with a quiz.
Chunk boundaries set the ceiling on RAG answer quality. How too-large and too-small chunks fail, why overlap helps, and how to size chunks against your own eval.
The chunking lesson drew retrieval quality as a hill over chunk size. This lab measured it: 111 lessons, 466 quiz questions, 8 chunk sizes, embedding search against plain keyword search. There was no hill. Embedding search was best with small chunks and fell from 0.908 to 0.725 as chunks grew, steadily before and after the model's hidden 2,048-token limit. Keyword search, with no model, stayed near 0.95 at every size from 128 words up.
Retrieve 50, rerank with a cross-encoder, keep 5. Why a second-stage reranker is the cheapest, highest-impact fix for a RAG system that keeps missing the right chunk.
Retrieval quality cannot exceed the quality of the text you extracted. Ingestion is the true ceiling of a RAG system, upstream of chunking, reranking, and hybrid search.
1. What chunking is and why it decides retrieval quality
A RAG system cannot hand a whole manual to the model, so it cuts each document into pieces, turns each piece into a vector and searches those. Where you cut decides what can ever come back together.
What is chunking in RAG?
Chunking is cutting your documents into smaller pieces, called chunks, before you embed and store them. Each chunk becomes one vector. At question time, retrieval compares the question with those vectors and returns the closest few chunks, and the model answers from them.
That makes the chunk the smallest thing retrieval can return. A fact that was split across two chunks never arrives in one piece, so neither a bigger model nor a reranker can recover it. In the lesson's example, a sentence giving an upload limit of 512 megabytes was cut at token 512: one chunk held the subject, the next held the number, and the assistant answered that it did not know.
What goes wrong when chunks are too big or too small?
Too big, and one vector has to stand for several topics at once, so it sits between them and matches none sharply. A retrieved chunk also carries text unrelated to the question into the prompt. There is a hard wall too: OpenAI's text-embedding-3 models accept at most 8,191 tokens, and some models cut long input silently.
Too small, and one idea is spread across two or three chunks. Each piece alone does not read like an answer, so none of them is retrieved, and you store many more vectors. The goal is roughly one complete idea per chunk.
Four, from simple to costly. Fixed-size chunking counts N tokens, cuts, and repeats, usually with some overlap. Structural chunking cuts on boundaries the author already made, such as headings, paragraphs, list items and code blocks. Recursive chunking tries the biggest boundaries first and falls back to smaller ones until each piece fits a size limit. Semantic chunking embeds each sentence and cuts where the meaning changes.
The lesson's order of choice is plain: start with fixed-size, use structural or recursive when your documents have real headings, and adopt semantic chunking only when a measurement on your own questions shows it wins. LangChain, LlamaIndex and Pinecone all ship several splitters for exactly this reason: no single one is right for every corpus.
The names sound more exotic than the mechanics. Each strategy is a rule for where the next boundary goes, and each pays for its precision somewhere.
What is fixed-size chunking, and when is it enough?
Fixed-size chunking ignores the text and only counts length: every N tokens it cuts. It is fast, predictable, and every chunk is guaranteed to fit the embedding model. Its weakness is that the cut lands wherever the counter stops, often in the middle of a sentence, which is why it is almost always paired with overlap.
It is the sensible default for a first version and for a corpus with no useful structure. The common pairing of 512 tokens with 50 tokens of overlap is a reasonable place to begin, but it is a starting point, not a proven best setting.
Recursive chunking tries the largest natural boundary first, say sections, and only when a piece is still too long does it split that piece on the next smaller boundary, then paragraphs, then sentences, until everything fits the size limit. So it keeps the author's structure where it can and still bounds the size.
It costs a little more work than fixed-size, and it helps most on text that has some structure you want to keep, such as documentation with headings and paragraphs of very different lengths.
Semantic chunking runs a small pipeline before the real embedding step. It splits the document into sentences, embeds each sentence, and measures how far each sentence's meaning is from the one before it. Where the distance stays low the topic has not changed; where it jumps past a threshold, it places a boundary.
The price is that you embed the text just to decide where to cut, which is slower, and on messy input the distances can come out noisy and put boundaries in odd places. That is why the lesson treats it as the last tool to reach for, used when the text has no structure and an evaluation on your own data shows it beats a simpler splitter.
Should code, tables and transcripts be chunked the same way as prose?
No. Prose splits well on paragraphs and headings. Code should keep a whole function or class together, because half a function means little to either half. A table row cut away from its header is a line of numbers with no column names, so the header has to travel with every group of rows. Transcripts and chat logs split on speaker turns, with the speaker label kept and a question kept with its answer.
A real ingestion pipeline often routes each document type to its own chunker. One prose-shaped setting across a mixed corpus quietly damages the code and the tables, and you only find out when those questions start failing.
It stops asking one chunk size to do two jobs. You embed and search small child chunks, so the vectors are sharp and matching is accurate. When a child matches, you use its stored parent id to fetch the larger section it belongs to, and you hand the model that section instead of the small fragment.
In the lesson's drawing the children are about 128 to 256 tokens and the parent is a 1,024 to 2,048 token section that is never embedded for search. LangChain ships this as the parent document retriever and LlamaIndex as auto-merging retrieval. It only works if each chunk stores its document id and position when you write it, so add that metadata at ingestion, not later.
Most advice on chunk size is a number copied from somewhere else. The course measured both size and overlap on its own lessons, and the results did not match the usual story.
Chunk size measured on 111 lessons and 466 questions: flat, then downOn this course's own lessons, embedding search did about as well with 32-word chunks as with 256-word chunks, then fell steadily as chunks grew. Keyword search stayed near 0.95 at every size from 128 words up.
There is no universal best size; there is a range you confirm on your own questions. In the course's lab, 111 lessons (353,513 words) were cut at eight sizes and searched with 466 quiz questions using nomic-embed-text in Ollama. The right lesson came back in the top 5 for 0.925 of questions at 32 words, 0.908 at 128 and 256 words, 0.886 at 512, 0.826 at 1,024, 0.781 at 2,048 and 0.725 at 4,096.
So there was no hill on the small side in the top 5: small chunks did about as well as medium ones, and large chunks steadily did worse. In the stricter top 1, embedding search did best at 64 to 128 words. These questions were written in the lessons' own words, so real users who phrase things differently may move the numbers; the method of measuring is what carries over.
What we measured: 111 lessons, 466 questions, nomic-embed-text via Ollama, no overlap, right lesson in the top 5: 0.925 at 32 words, 0.908 at 256, 0.826 at 1,024, 0.725 at 4,096.
Why do very large chunks hurt retrieval even below the model's limit?
Two reasons, and the lab saw both. Between 256 and 1,024 words no chunk was cut, yet the score still fell from 0.908 to 0.826, so size alone blurred the vectors. Past that, the version of nomic-embed-text that Ollama serves reads only 2,048 tokens and does not warn you: at 2,048 words, 135 of 227 chunks were cut short, and everything after the cut was invisible to search.
Check your embedding model's real token window before you choose a size. The page may describe the window as large while the table under it says 2K.
What we measured: nomic-embed-text in Ollama reads 2,048 tokens: 0 of 299 checked chunks cut at 1,024 words, 135 of 227 cut at 2,048 words, 98 of 130 at 4,096.
A common start is about 10 to 15% of the chunk size, which is where 512 tokens with 50 overlap comes from. Overlap means each chunk starts a little before the last one ended, so a sentence sitting on a boundary appears whole in at least one chunk. Counted over 59,660 sentences of the course, 500-character chunks with no overlap cut 22.6% of sentences; a 20% overlap cut that to 7.8%, at the cost of 1.25 times as many chunks to store and embed.
Whether that saves retrieval is a separate question. In the word-based lab, 15% overlap at 64 words won on 16 questions and lost on 10, a split luck gives about 33 times in 100, while turning 5,577 chunks into 6,582. Only past the embedding model's token wall did overlap show a clear hint of helping. Measure it on your data before you pay for it.
What we measured: 59,660 sentences at 500 characters: 22.6% cut with no overlap, 7.8% with 20% overlap at 1.25x the chunks. Retrieval at 64 words: overlap won 16, lost 10, within luck.
It pulls two bills in opposite directions. For a corpus of one million tokens with 10% overlap and the top 5 chunks sent to the model, 128-token chunks give about 8,700 vectors but only about 640 prompt tokens per question; 512-token chunks give about 2,175 vectors and about 2,560 prompt tokens; 1,024-token chunks give about 1,090 vectors and about 5,120 prompt tokens.
Small chunks grow the index, the build time and the vector database bill. Large chunks raise what you pay the model on every question and fill the prompt with text unrelated to it. No size wins on quality, storage and per-question cost at once, which is one more reason to measure rather than copy a number.
Chunking is the first lever, not the only one. Before you rebuild the index with a new splitter, check whether the failure is somewhere else.
How do I know whether chunking is causing bad RAG answers?
Pull the raw retrieved chunks for a handful of failing questions and read them. If the answer is split across two chunks, or buried in a huge chunk about five things, it is a chunking problem. If the right chunk came back but ranked too low to make the prompt, it is a ranking problem, and a reranker is the fix. If the right chunk is missing entirely, look further up: the parser, the embedding model or the query.
Then build a small set of real questions with known source passages and measure whether each chunking choice retrieves them. Change size, overlap and strategy one at a time and keep what wins on that measurement.
Upstream, the parser: retrieval cannot be better than the text you extracted. A two-column PDF read in storage order splices lines from both columns into nonsense, and a scanned page gives no text at all without OCR. No chunker can repair that. Downstream, exact identifiers such as error codes and part numbers are often blurred by embeddings, and hybrid retrieval with BM25 keyword search brings them back.
In the chunk size lab, plain keyword search scored 0.955 in the top 5 at 256 words against 0.908 for embeddings, so test a keyword baseline before you tune anything. At large scale, a metadata filter that shrinks the candidate set before ranking does more than any chunk setting.
What we measured: Same 466 questions at 256 words: BM25 keyword search 0.955 in the top 5, nomic-embed-text 0.908.
What is contextual retrieval, and does it replace chunking?
It does not replace chunking; it repairs what chunking loses. A chunk cut out of its document loses the context that made it clear, so a chunk saying the limit is 512 megabytes no longer says it is about uploads. Anthropic's Contextual Retrieval adds a short, chunk-specific sentence of context to each chunk before embedding it.
In Anthropic's own testing, the contextual embedding step alone cut retrieval failures by roughly a third, with larger reductions when combined with other techniques such as reranking. It adds a model call per chunk at indexing time, so weigh it against cheaper fixes first.
How is Chunking strategies for RAG done in a real production system?
The answers on this page are the short version. The lessons below are the long one, with the trade-offs and failure cases, starting with "Chunking: The First Lever on Retrieval Quality".
The lessons that teach the next part
Chunk boundaries set the ceiling on RAG answer quality. How too-large and too-small chunks fail, why overlap helps, and how to size chunks against your own eval.
The chunking lesson drew retrieval quality as a hill over chunk size. This lab measured it: 111 lessons, 466 quiz questions, 8 chunk sizes, embedding search against plain keyword search. There was no hill. Embedding search was best with small chunks and fell from 0.908 to 0.725 as chunks grew, steadily before and after the model's hidden 2,048-token limit. Keyword search, with no model, stayed near 0.95 at every size from 128 words up.
Retrieve 50, rerank with a cross-encoder, keep 5. Why a second-stage reranker is the cheapest, highest-impact fix for a RAG system that keeps missing the right chunk.
Retrieval quality cannot exceed the quality of the text you extracted. Ingestion is the true ceiling of a RAG system, upstream of chunking, reranking, and hybrid search.
Every answer on this page comes from our AI Engineering course: 219 lessons on RAG, evals, agents, serving, security and MLOps. Many of them are built around a real experiment. You learn why the answer is right, which is what an interviewer checks with the second question. 10 lessons are free to read, with no card needed.
These questions are a start. The interview asks more.
Get all 150 questions in the AI Engineering (LLM applications) pack
Each question has why it is asked, a full answer, what the interviewer listens for, the follow-ups, the common mistakes and a diagram (180 diagrams in all). A PDF you keep, junior to staff level.