Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 10

Chunking for RAG: Optimizing Retrieval

Recommended resources

In our last lesson, you successfully loaded documents and extracted their text into standardized Document objects. This was the essential first step in building our knowledge base. However, these documents, which often represent entire pages or even whole files, are generally too large to be used directly in a RAG pipeline. This brings us to a crucial preprocessing step: chunking.

This lesson addresses the fundamental question of why we must split documents into smaller pieces. Your goal is to explain the technical and semantic reasons that make chunking a non-negotiable step for building effective and efficient RAG systems. Understanding this "why" is critical before we dive into the "how" in our next session.

Situating Chunking in the RAG Pipeline

Before we explore the reasons for chunking, let's visualize where it fits within the overall RAG architecture. As you learned previously, the process starts with a user's question and ends with an LLM-generated response. Chunking is a preparatory step that happens offline, before any questions are asked.

This diagram illustrates the RAG workflow. The lower part shows the offline process where a large text is broken down by a chunking model into multiple smaller chunks. These chunks form the "Context" that the Retriever searches through to answer a user's question.

As the diagram shows, we break down large source documents into smaller, manageable chunks. These chunks are then used to build the knowledge base that the retriever component will search. But why go to this trouble? Why not just use the whole documents? The reasons are rooted in both practical engineering constraints and the theoretical principles of semantic search.


Reason 1: The Constraints of Models and Systems

As a software engineer, you're familiar with system limitations like memory, processing power, and API rate limits. Similar constraints exist in the world of LLMs and embedding models, forming the most immediate and practical reason for chunking.

  1. Embedding Model Context Windows: The models that generate text embeddings (which we covered in Module 2) have a fixed input size, often referred to as the "context window." For example, a popular all-MiniLM-L6-v2 model has a limit of 256 word pieces. You simply cannot feed an entire 10-page document into such a model at once. Chunking ensures that the text segments we want to embed fit within this operational limit.

  2. LLM Context Windows: The final generative LLM also has a context window. When we retrieve relevant information to "augment" the prompt, we need to pass that context to the LLM. If our retrieved "chunks" are actually entire documents, we might only be able to fit one or two into the prompt before hitting the token limit. This severely restricts the amount of evidence the LLM can use to synthesize an answer. Smaller chunks allow us to provide a more diverse set of targeted information from multiple sources.

  3. Cost and Latency: Processing text with neural networks is computationally expensive. Embedding and generating responses from large volumes of text takes more time and costs more money. Chunking the data into smaller pieces means we can retrieve and process only the most relevant information, making the entire system faster and more cost-effective.


Reason 2: The Impact on Retrieval Quality

Beyond the practical constraints, the most important reason for chunking lies in its impact on the quality of semantic search. The goal of retrieval is to find text segments whose meaning is closest to the user's query. The size and coherence of your chunks directly affect your ability to do this accurately.

This is fundamentally a signal-to-noise problem. A perfect chunk contains a specific, self-contained idea. Its resulting embedding is a precise vector representation of that idea. Conversely, a very large chunk that discusses multiple disparate topics will produce a "noisy" or "averaged" embedding that doesn't strongly represent any single idea.

The following video provides an excellent explanation of this concept.

Chunking Strategies Explained

This video, "Chunking Strategies Explained" by Redis, clearly articulates the core purpose of chunking and the trade-offs involved.

First, watch the introduction from the beginning to understand the definition of chunking and its primary purpose: minimizing noise and maintaining relevance. Then, pay close attention to the section on embedding short vs. long content. This highlights the crucial trade-off: short content allows for precise matching but may miss broader context, while long content captures overall themes but can dilute specific details.

As the video explains, large chunks can "dilute the significancy of specific details." When a user asks a specific question, its embedding will struggle to find a strong match with a diluted, noisy vector from a large chunk. This leads to the retrieval of less relevant information, which in turn degrades the quality of the final generated answer.


Reason 3: Improving LLM Focus and Reducing Hallucinations

The quality of the retrieved context directly influences the LLM's ability to generate a factual, grounded response.

  • Precise Context, Precise Answers: When the retriever provides the LLM with small, highly relevant chunks of text, it gives the model exactly the information it needs to answer the question. This reduces the cognitive load on the model and helps it generate a concise and accurate response.
  • Noisy Context, Noisy Answers: If you provide the LLM with a large chunk that contains the answer buried within pages of irrelevant text, you risk "context confusion." The model might get distracted by the noise, miss the key detail, or pull in related but incorrect information from the surrounding text.

The following article from Deepchecks reinforces these points, framing chunking as the foundation of a reliable RAG system.

Why Chunking Is Important for AI and RAG Applications? | Deepchecks

This article provides a concise and direct explanation of chunking's role in RAG performance.

Read the introduction to understand how chunking helps reduce noise and hallucination. Then, review the section on Chunking in RAG Architecture which clarifies why embedding models and retrieval engines struggle with large inputs. Finally, the FAQ section provides a succinct summary in response to the question Why is chunking critical?.

By ensuring the retrieved context is dense with relevant information, good chunking practices minimize the chance of the LLM hallucinating or inventing facts, as it has a strong, clear source to draw from. Poor chunking, on the other hand, can break semantic relationships and lead to confusing context for the model.

Chunking Strategies in RAG: Optimising Data for Advanced AI Responses

This short video clip provides a stark visual of what happens when chunking goes wrong.

Watch the segment from the beginning, where the presenter shows how a naive fixed-size splitting strategy can break words and sentences, destroying the semantic meaning and leading to irrelevant results.


Evidence: Why a Good Chunking Strategy Can Outperform a Better Model

The importance of chunking is not just theoretical. Recent studies have shown that a well-optimized retrieval pipeline, with effective chunking, can have a greater impact on RAG performance than using a more powerful (and expensive) LLM.

This challenges a common misconception that the advent of very large context window LLMs makes RAG obsolete. The following article from Snowflake provides compelling evidence to the contrary.

How Retrieval & Chunking Impact Finance RAG

This engineering blog post presents a case study on financial documents, showing how chunking and retrieval strategies are massive determinants of RAG quality.

Please read the following sections to see the data-backed arguments: Read the introduction, which immediately disproves the theory that long context windows eliminate the need for retrieval and chunking. Review the key findings, especially the point that optimal chunking matters and that overly large chunks dilute relevance. Read the section explaining that the choice of retrieval and chunking can matter more than the generative model itself. This is a crucial takeaway. Finally, understand why larger chunks perform worse by reading the section that explains how they can degrade performance and lead to "context confusion" even in powerful models.

The article's findings are a powerful testament to the principles we've discussed. Even the most advanced LLMs struggle when presented with noisy, unfocused context. A carefully tuned retrieval pipeline, built upon a foundation of well-structured chunks, can enable even a smaller model to achieve high performance.


Conclusion

In this lesson, you have learned the critical reasons why documents must be split into chunks before being used in a RAG system. This is not just a minor preprocessing step, but a foundational element that dictates the performance, efficiency, and reliability of the entire application.

Here are the key takeaways:

  • Technical Constraints: Chunking is necessary to work within the fixed context windows of embedding models and LLMs, and to manage computational cost and latency.
  • Semantic Accuracy: Splitting documents into small, coherent chunks creates "clean" embeddings that represent specific ideas. This significantly improves the signal-to-noise ratio for semantic search, leading to more relevant retrieval results.
  • LLM Performance: Providing the LLM with concise, relevant context reduces "context confusion" and minimizes the risk of hallucinations, leading to more faithful and accurate answers.
  • Strategic Importance: An effective chunking strategy can have a greater impact on RAG quality than the choice of the generative model itself.

Now that you have a firm grasp of why chunking is essential, we are ready to move on to the practical implementation. In the next lesson, you will learn how to apply two common chunking strategies: fixed-size chunking and recursive character-based chunking.