Hermes Wiki
Developer/AI/RAG/Fundamentals/chunking-strategy-and-retrieval-quality-in-rag

Chunking Strategy and Retrieval Quality in RAG

Concept

Retrieval-Augmented Generation grounds an LLM's answer in retrieved document text instead of relying on the model's training-time memory: documents are split into chunks, each chunk is embedded into a vector, the vectors are stored in a vector database, and at query time the top-k chunks most similar to the query embedding are pulled back and injected into the prompt as context. The entire system's ceiling is set by retrieval quality, not by the LLM — an LLM given the wrong chunks will confidently generate a wrong answer, and no amount of prompt engineering downstream fixes retrieval that pulled back the wrong text in the first place. That makes chunking, the step that decides what a "unit of retrieval" even is, the highest-leverage design decision in the whole pipeline.

The core tension is a precision/context trade-off. Small chunks (a sentence or paragraph) let similarity search be precise — the embedding represents one idea, so a query matching that idea scores it highly — but a small chunk handed to the LLM in isolation often lacks the surrounding context needed to answer correctly (a paragraph that says "it increased by 12%" is useless without the sentence naming what "it" is). Large chunks (a full section or document) preserve that context but dilute the embedding: a chunk covering five different subtopics produces a vector that's a blurry average of all five, so it scores only moderately against a query on any single one of them, and irrelevant chunks start out-competing it.

Production systems in 2025-2026 largely converge on hierarchical chunking to resolve this directly rather than picking a single chunk size and compromising: index small chunks for precise matching, but at retrieval time expand each match out to its parent section (or a fixed window of surrounding chunks) before handing it to the LLM. This gets the precision of small-chunk search and the context of large-chunk generation without forcing one chunk size to do both jobs.

A second technique, Anthropic's contextual retrieval (published September 2024), attacks a different failure mode: a chunk can be precise and still be ambiguous once separated from its source document (a chunk that says "the company's revenue grew 3% in the third quarter" says nothing about which company or which year once it's floating alone in a vector index). The fix is to prepend a short (50-100 token) LLM-generated summary to each chunk before embedding it — e.g. "This chunk is from an SEC filing about ACME Corp's Q2 2023 performance" — so the embedding captures context the chunk text alone doesn't carry. Anthropic reported this cut retrieval failures by 49%, and by 67% when combined with a reranking step, across their test corpora; the cost is modest with prompt caching, since the surrounding document context is shared and cached across all of that document's chunks.

Tradeoffs

Chunking approach Benefit Cost
Fixed-size (e.g. 512 tokens, no overlap) Trivial to implement, predictable chunk count and cost Cuts mid-sentence/mid-idea arbitrarily; a chunk boundary can split the exact fact a query needs across two chunks
Fixed-size with overlap Reduces boundary-splitting failures cheaply More stored chunks (redundant text), doesn't fix the precision/context dilution problem itself
Semantic/recursive splitting (split on natural boundaries — headings, paragraphs) Chunks align with actual ideas, less arbitrary truncation More implementation complexity; still forces one chunk size to serve both matching and generation
Hierarchical (small chunks indexed, parent context retrieved) Gets precision at match time and context at generation time simultaneously Extra indexing/retrieval-time complexity (parent-lookup step); slightly larger context injected per retrieved match
Contextual retrieval (context-augmented chunk before embedding) Directly fixes ambiguous-out-of-context chunks; large measured recall gains Requires an LLM call per chunk during ingestion (mitigated by prompt caching); adds ingestion pipeline complexity

When to use / when not to

  • Use RAG at all only when answers must be grounded in documents that are private, change over time, or were never in the model's training data — if the source material comfortably fits in the context window every time, skip retrieval entirely and just paste the document in; retrieval adds a whole failure surface (bad chunking, bad matches) for no benefit over that simpler approach.
  • Start with semantic/recursive splitting, not fixed-size, as the default — the added complexity over naive fixed-size chunking is small and it removes an entire class of mid-idea truncation bugs.
  • Reach for hierarchical chunking once retrieval quality on real queries shows chunks that match but lack context to actually answer — this is a symptom to observe, not something to build preemptively before evidence it's needed.
  • Reach for contextual retrieval when chunks are frequently ambiguous once separated from their source document (financial reports, legal contracts, anything with pronouns/references that only resolve within the source) — it's not worth the ingestion-time LLM cost for content where each chunk is already self-contained (e.g. independent FAQ entries).
  • Don't tune chunk size by eyeballing a handful of example queries — retrieval quality has to be measured (recall@k, MRR against a fixed eval set of query/expected-chunk pairs) the same way any other engineering change is measured, or a chunking change that looks better on the three examples someone checked can be silently worse everywhere else.

Common pitfall

Treating chunk size as a single global constant tuned once and left alone, when the right chunk size is a property of the source document's structure, not a universal default. A knowledge base mixing short FAQ entries and long technical specifications forced through one fixed chunk size will either over-fragment the FAQ entries (destroying their already-small, already-coherent unit) or under-fragment the specifications (diluting their embeddings across unrelated subsections) — there is no single number that serves both well. The fix is chunking strategy per document type, not a single pipeline-wide setting, and re-evaluating it whenever a genuinely new document type is added to the corpus.

Engineering Lens

RAG's failure modes are almost always retrieval failures wearing a generation-quality disguise: a hallucinated or wrong answer gets blamed on "the model," when the actual cause was that the wrong chunks were retrieved and the LLM did the reasonable thing with bad input. The engineering discipline that separates a production RAG system from a prototype is measuring retrieval in isolation — recall@k and MRR against a fixed eval set, checked before every chunking or embedding-model change ships — rather than only ever eyeballing final answers. A team that can point to a retrieval eval set and a number that moved is doing RAG engineering; a team that only checks whether the final chat response "looks right" is debugging the wrong half of the system.

Sources

Hermes Wiki