Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Optimizing Document Chunking for Retrieval

Hello! Welcome to the next lesson in our module on giving agents a knowledge base.

In the last lesson, we established a framework for curating the knowledge base, focusing on what to include or exclude based on relevance, quality, structure, and risk. Now that we have our set of trusted documents, we must prepare them for the embedding process. This is where chunking comes in.

The learning outcome for this lesson is to select an appropriate chunking method and configure chunk size and overlap for a given document type and retrieval goal.

Chunking—the process of splitting large documents into smaller, coherent pieces—is a critical design decision in any RAG system. It directly influences the signal-to-noise ratio of retrieved context, the completeness of information provided to the LLM, and the overall computational cost of the system. For your goal of delivering agentic AI services, mastering chunking is essential for building reliable and efficient solutions for your clients.

1. The Spectrum of Chunking Strategies

Chunking is not a one-size-fits-all process. Strategies range from simple, computationally inexpensive heuristics to complex, semantically-driven methods. Understanding this spectrum is key to making informed architectural decisions.

A superb resource for this is a video by Greg Kamradt which breaks down text splitting into five conceptual levels. We'll use it to build our understanding from the ground up.

The 5 Levels Of Text Splitting For Retrieval

Let's start with an introduction to the theory behind chunking and the first two, most common levels of text splitting. This video explains why we chunk, introduces the core concepts of chunk_size and chunk_overlap, and covers the foundational heuristic-based methods.

Please watch the following sections: Theory & Introduction (00:00 - 06:31): Focus on why we chunk (context limits, signal-to-noise ratio) and where it fits in the retrieval process. Level 1: Character Splitting (06:31 - 10:43): Understand the basic idea and its limitations. Chunk Overlap Explained (10:43 - 13:07): Grasp the concept of overlap and how it helps maintain context across chunk boundaries. The visualization tool chunkviz.com is particularly helpful here. Level 2: Recursive Character Splitting (16:09 - 20:51): This is the most common and important heuristic method. Pay close attention to how it uses a list of separators (e.g., \n\n, \n, ) to respect the natural structure of the text.

To summarize the key points from these sections:

  • Fixed-size splitting (by character or token) is simple but naive, as it often breaks sentences or even words, destroying semantic meaning.
  • Chunk Overlap is a technique where the end of one chunk is repeated at the beginning of the next. This helps preserve context that might otherwise be lost across a hard boundary, at the cost of some data redundancy.
  • Recursive Character Splitting is the workhorse of RAG. It attempts to split text along natural semantic boundaries (paragraphs, sentences) first, only resorting to finer splits (words, characters) if a chunk is still too large. This is often the best place to start due to its balance of performance and computational efficiency.

Content-Aware & Document-Specific Chunking

The recursive approach works well for general prose, but we can be more intelligent when dealing with documents that have a known structure, like code, Markdown, or complex PDFs.

The 5 Levels Of Text Splitting For Retrieval

The next level of sophistication is to tailor the chunking strategy to the document's format. This part of the video covers how to handle specialized content.

Watch the section on Level 3: Document Specific Splitting (20:51 - 32:06). Note the different strategies for: Markdown: Using headers as primary split points. Code: Using classes and functions as boundaries. Multimodal PDFs: Using tools like unstructured.io to extract not just text, but also tables (often converted to HTML for the LLM) and images.

The core architectural principle here is to leverage the document's inherent structure to create more semantically coherent chunks. For example, chunking a Python file by classes or functions is far more effective than splitting it by arbitrary line numbers. For images, the strategy of creating a text summary and embedding that summary is a common and effective pattern.

Semantically-Driven and Agentic Chunking

The most advanced—and computationally expensive—methods move beyond structural heuristics and attempt to split text based on its semantic meaning.

The 5 Levels Of Text Splitting For Retrieval

The final levels in this overview explore experimental but powerful concepts using embeddings and LLMs to drive the chunking process itself.

Watch the final two levels: Level 4: Semantic Splitting (32:06 - 47:48): Understand the concept of calculating the embedding distance between consecutive sentences to find 'breakpoints' where the topic shifts. Level 5: Agentic Splitting (47:48 - 01:01:44): Grasp the experimental idea of using an LLM as a decision-maker to iteratively group sentences into coherent chunks.

These advanced methods are powerful because they directly target semantic coherence. The agentic approach, in particular, mirrors how a human might logically group ideas.

Agentic Chunking with LangGraph
This diagram illustrates an agentic chunking workflow. An LLM iteratively decides whether each new sentence belongs to the current chunk or should start a new one, creating dynamically-sized, semantically-grouped chunks.

While these semantic methods are promising, their high computational cost and slower processing times mean they are typically reserved for situations where simpler heuristics have proven insufficient.

2. Configuring Chunk Size and Overlap: An Evidence-Based Approach

Once you've chosen a chunking method, you must configure its two primary parameters: chunk_size and chunk_overlap. This choice involves a fundamental trade-off between precision and recall.

  • Chunk Size:

    • Smaller chunks: Lead to more specific, targeted retrievals. This increases precision (the retrieved information is highly relevant) but can hurt recall (the chunk may not contain all the necessary context to answer the query).
    • Larger chunks: Capture more context, increasing recall. However, they also introduce more noise, which can decrease precision and increase the token load on the LLM during the generation step.
  • Chunk Overlap:

    • Adding overlap generally improves both precision and recall by reducing the chance of splitting a key concept across two chunks. The trade-off is an increase in the total number of chunks and data storage.

Making this decision shouldn't be based on intuition alone. Several studies have benchmarked different configurations.

JetBrains Chunking Evaluation Framework

This GitHub repository from a JetBrains project provides a clear evaluation of how chunk size and overlap affect retrieval metrics. It offers practical, data-driven insights.

Read the 'Findings' sections on the repository's main page: Effect of Chunk Size: Note the inverse relationship between chunk size and precision, and the direct relationship with recall. Effect of Chunk Overlap: See how increasing overlap generally improves all metrics. Best Chunking Strategy by Metric: This is crucial. It gives concrete recommendations for whether you need to optimize for Precision, Recall, or Ranking.

For an even more rigorous analysis, we can turn to academic research.

The Impact of Chunking Strategies on Domain-Specific Information Retrieval

This research paper provides a large-scale benchmark of 90 different chunker-model configurations, offering empirically grounded guidelines.

This is a dense paper, so focus on these key takeaways: Abstract and Contributions (Section I): Note the main finding: sentence-based splitting with a 512-token window and 200-token overlap achieved the highest performance (IoU score) while being computationally efficient. Chunking Strategies (Section III-A): Briefly review the three families of splitters they compared: Semantic, Sentence, and Token. Results (Section IV-B): Look at the 'Top-performing chunking configurations'. This confirms the 512-200 sentence splitter as a top contender. Conclusion (Section V): Read the first paragraph, which reinforces that simple sentence-based splitting consistently outperforms more complex methods and that moderate sizes (256-512 tokens) are optimal.

The consensus from these empirical studies provides a powerful starting point: a sentence-based splitter with a chunk size in the 256-512 token range and an overlap of 100-200 tokens is a robust, high-performing baseline for many use cases involving prose.

3. A Practical Framework for Selection

Synthesizing this, we can create a decision-making framework for selecting and configuring a chunking strategy for a client project.

  1. Analyze the Document Type:

    • General Prose (reports, articles): Start with a SentenceSplitter (like LlamaIndex's) or RecursiveCharacterTextSplitter (like LangChain's). These are the most reliable and well-benchmarked options.
    • Structured Text (Markdown, code): Use a document-specific splitter that leverages the inherent structure (e.g., MarkdownTextSplitter, PythonCodeTextSplitter).
    • Complex/Multimodal (PDFs): Use a pre-processing library like unstructured to parse elements first. Then, apply a sentence/recursive splitter to the extracted text, convert tables to Markdown, and generate summaries for images.
  2. Define the Primary Retrieval Goal:

    • For high precision (e.g., Q&A, fact extraction): The JetBrains study suggests a RecursiveCharacterTextSplitter with a smaller chunk size (e.g., 200-400 tokens) and a moderate overlap (~100 tokens).
    • For high recall (e.g., summarization, document drafting): The same study recommends a SentenceChunker with a larger chunk size (e.g., 600 tokens) and significant overlap (~200 tokens).
    • For a balanced, high-performance start: The academic paper provides a strong, evidence-backed default: SentenceSplitter with 512 tokens and 200 tokens of overlap. This is an excellent configuration for a proof-of-concept.
  3. Consider the Cost & Complexity Budget:

    • Always begin with heuristic-based methods (Sentence, Recursive). They are fast, cheap, and, as the data shows, highly effective.
    • Only escalate to semantic or agentic chunking if you have a clear business case where the improved semantic coherence justifies the significant increase in processing time and API costs. This should be treated as a later-stage optimization, not a starting point.

Conclusion

In this lesson, we dissected the critical process of chunking, moving from high-level strategy to evidence-based parameter tuning. Choosing the right chunking method is a balancing act between semantic coherence, computational cost, and the specific precision/recall needs of your application.

Key Takeaways:

  • A Spectrum of Methods: Chunking strategies range from simple heuristics (Recursive splitting) to content-aware (Code/Markdown splitting) and semantically-driven (Embedding/Agentic splitting) methods.
  • Start with Heuristics: For most applications, a SentenceSplitter or RecursiveCharacterTextSplitter is the most effective and efficient starting point.
  • Size and Overlap are Key Levers: Chunk size and overlap allow you to tune the trade-off between precision and recall. Smaller chunks favor precision; larger chunks favor recall. Overlap mitigates context loss at boundaries.
  • Let Data Guide Your Choice: Empirical studies provide strong baselines. A sentence splitter with a chunk size of ~512 tokens and an overlap of ~200 tokens is a well-supported, high-performance configuration for general-purpose retrieval.
  • Match Strategy to Goal: The optimal configuration depends on your document types and retrieval goals. Tailor your approach based on whether you need high precision, high recall, or a balanced performance.

In our next lesson, we will explore what happens after the chunks are created and embedded. We will focus on how to identify and address common RAG failure modes, such as irrelevant retrieval, missing context, and conflicting sources. This will help us build robust systems that can handle the complexities of real-world data.

Can't find a good explanation? Sign up and we'll make it for you

Sign up