What is Chunking?
Chunking is the process of splitting large content into smaller, meaningful pieces before embedding and retrieval. It is one of the most important design decisions in a RAG pipeline.
By the end of this lesson, you will be able to:
- Explain what chunking means and why large documents are split.
- Understand how chunks fit into the embedding and RAG pipeline.
- See the difference between a whole document and smaller retrievable chunks.
- Understand chunk size, boundaries, and the trade-off between context and precision.
- Create simple chunks with Python.
- Recognize why chunking quality directly affects retrieval quality.
What Is Chunking?
Chunking means dividing a larger piece of content into smaller sections called chunks. A chunk can be a paragraph, a group of sentences, a section, or another logical unit of information.
In a typical RAG system, documents are too large to treat as one retrieval unit. Instead, the system splits them into chunks, creates an embedding for each chunk, stores those embeddings, and later retrieves the chunks that are most relevant to a user's question.
Why Do We Need Chunking?
Suppose you have a 100-page product manual. A user asks, “How long is the warranty?” Sending the entire manual into every retrieval step is inefficient and can introduce unrelated information.
Whole document
One very large retrieval unit contains installation, troubleshooting, warranty, safety, specifications, and many other topics.
Focused sections
The warranty section can become a smaller retrieval unit that is easier to match to the question.
Split policies into useful sections such as leave, expenses, benefits, and remote-work rules.
Split documentation into concepts, configuration steps, API sections, and troubleshooting guidance.
Where Chunking Fits in RAG
Chunking normally happens during the indexing or ingestion stage, before a user asks a question. The chunks are then embedded and stored for later retrieval.
The important connection is that retrieval happens over the chunks, not over the original document as one giant unit.
Document vs Chunk
A document is the original source. A chunk is a smaller retrievable piece derived from that source.
Chunk Size
Chunk size describes how much content each chunk contains. It can be measured using characters, words, tokens, sentences, or another unit depending on the chunking method.
High precision, low context
A tiny chunk may contain only part of an explanation and lose important surrounding information.
Useful context
A well-sized chunk can contain a complete idea while remaining focused enough for retrieval.
More noise
A huge chunk may contain several unrelated topics, making retrieval less precise and adding unnecessary context.
Chunk Boundaries
Where a chunk starts and ends can be just as important as its size. Ideally, a chunk should avoid cutting an important idea in the middle.
“The refund is available within 30 days if the product is returned…”
Next chunk starts: “unused and in its original packaging.”
Keep the complete refund condition together:
“The refund is available within 30 days if the product is returned unused and in its original packaging.”
Logical boundaries can come from headings, paragraphs, sections, lists, sentences, or document structure. Later lessons will cover specific chunking strategies.
Simple Python Chunking
Here is a basic example that splits text into fixed-size character chunks. It is intentionally simple so you can understand the core idea before learning more advanced strategies.
text = "Generative AI can answer questions from documents. " \
"Chunking divides long documents into smaller pieces. " \
"Those pieces can then be embedded and retrieved."
chunk_size = 60
chunks = [
text[i:i + chunk_size]
for i in range(0, len(text), chunk_size)
]
for number, chunk in enumerate(chunks, start=1):
print(f"Chunk {number}: {chunk}")Chunking With Simple Sentences
A sentence-aware approach can preserve meaning better than cutting text at an arbitrary character position. This example groups sentences until a target size is reached.
sentences = [
"Chunking splits documents into smaller pieces.",
"Each piece can be embedded separately.",
"Retrieval can then return only relevant pieces.",
"Good boundaries help preserve meaning."
]
max_sentences = 2
chunks = []
for i in range(0, len(sentences), max_sentences):
chunks.append(" ".join(sentences[i:i + max_sentences]))
for chunk in chunks:
print(chunk)Chunk Size vs Retrieval Quality
Chunking creates a trade-off. Smaller chunks can make matching more focused, while larger chunks can preserve more context.
- More focused retrieval
- Less unrelated context
- May lose surrounding explanation
- More surrounding context
- Fewer chunks to manage
- May include unrelated information
Chunking and Embeddings
After chunking, each chunk can be passed to an embedding model. The resulting vectors represent the chunks in a form that supports similarity search.
If a user's question is semantically related to Chunk A, similarity search can rank that chunk highly. Poor chunking can make the embedding less useful because the vector represents a mixture of too many unrelated ideas.
Chunking and RAG
In RAG, the retrieved chunks become context for the language model. This makes chunk quality important at two different points: retrieval and generation.
Focused answer context
The retrieved text directly addresses the question and gives the model enough information to answer.
Noisy context
The retrieved text contains the answer plus many unrelated sections, increasing the amount of irrelevant context.
Chunking Is Not the Same as Summarization
Chunking and summarization solve different problems.
Split the original content
The chunks normally preserve the source text so the retriever can return exact supporting content.
Rewrite content more briefly
A summary creates a new representation that may omit details from the original source.
A RAG pipeline can use both, but they should not be treated as the same operation.
Common Chunking Mistakes
The whole document is stored as one retrieval unit, reducing retrieval precision.
Important context is fragmented across many pieces.
Headings, tables, lists, and sections may provide useful boundaries.
A chunk size that works for one dataset may perform poorly on another.
Without source information, it becomes harder to cite or trace retrieved content.
A chunk is a retrieval unit; tokenization is how text is represented for model processing.
How Do You Know If Chunking Is Good?
Good chunking should be evaluated with real questions. Ask whether the retrieved chunks contain the information needed to answer each question.
Relevant chunks should address the user's question.
The answer should not be split across missing pieces.
Too much unrelated content can hurt the downstream answer.
Design a Chunking Strategy
Imagine you are building a RAG assistant for a 200-page technical manual.
- Choose a logical unit you would try to keep together.
- Decide whether you would prefer very small, medium, or large chunks and explain why.
- List two pieces of metadata you would store with every chunk.
- Write one question you would use to test whether your chunks retrieve the right information.
Quick Quiz
A. Converting a vector into text
B. Splitting larger content into smaller retrievable pieces
C. Training an LLM from scratch
A. It can contain too much unrelated information
B. It always deletes metadata
C. It prevents embeddings from existing
A. Meaningful context and useful boundaries
B. Random character positions only
C. The entire document in every chunk
30-Second Recap
Key Takeaways
- Chunking turns large documents into smaller units that can be embedded and retrieved.
- Chunk size and boundaries strongly influence retrieval quality.
- Good chunks contain focused meaning without losing necessary context.
- Chunking happens before embedding in a typical RAG ingestion pipeline.
- Evaluate chunking with real questions instead of assuming one fixed size is always best.