LESSON 37 · CHUNKING

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.

30 min read•Beginner•Generative AI

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.
1

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.

DocumentLarge source content
→
ChunkingSplit into useful pieces
→
EmbeddingsOne vector per chunk
→
RetrievalFind relevant chunks
Key idea:A chunk is not just an arbitrary slice of text. Good chunking tries to create retrieval units that contain enough meaning to answer questions without carrying too much unrelated information.
2

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.

WITHOUT CHUNKING

Whole document

One very large retrieval unit contains installation, troubleshooting, warranty, safety, specifications, and many other topics.

→
WITH CHUNKING

Focused sections

The warranty section can become a smaller retrieval unit that is easier to match to the question.

EXAMPLE 1Employee handbook

Split policies into useful sections such as leave, expenses, benefits, and remote-work rules.

EXAMPLE 2Technical documentation

Split documentation into concepts, configuration steps, API sections, and troubleshooting guidance.

3

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.

1. LoadRead documents
→
2. ChunkSplit content
→
3. EmbedCreate vectors
→
4. StoreSave vectors + metadata
User question“What is the warranty period?”
→
RetrieveSearch chunk embeddings
→
GenerateLLM answers using retrieved context

The important connection is that retrieval happens over the chunks, not over the original document as one giant unit.

4

Document vs Chunk

A document is the original source. A chunk is a smaller retrievable piece derived from that source.

DOCUMENTEmployee Handbook.pdfOriginal source containing many topics.
CHUNK 1Leave PolicyRelevant section about annual and sick leave.
CHUNK 2Travel ExpensesRules for reimbursement and receipts.
CHUNK 3Remote WorkGuidelines for working from home.
Metadata matters: chunks often keep metadata such as document ID, title, section, page number, URL, or product ID so the application can trace a retrieved chunk back to its source.
5

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.

TOO SMALL

High precision, low context

A tiny chunk may contain only part of an explanation and lose important surrounding information.

BALANCED

Useful context

A well-sized chunk can contain a complete idea while remaining focused enough for retrieval.

TOO LARGE

More noise

A huge chunk may contain several unrelated topics, making retrieval less precise and adding unnecessary context.

Small“Warranty: 2 years.”
MediumWarranty section + conditions + exclusions.
LargeEntire product chapter with warranty, installation, troubleshooting, and specifications.
6

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.

WEAK BOUNDARY

“The refund is available within 30 days if the product is returned…”

Next chunk starts: “unused and in its original packaging.”

BETTER BOUNDARY

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.

7

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.

</> Python
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}")
What this teaches:the original text becomes multiple smaller strings. Real RAG systems usually use smarter boundaries and often work with tokens rather than raw characters.
8

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.

</> Python
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)
Why this can be better:sentences are meaningful language units, so the chunk is less likely to cut a sentence in half. This is still a simplified example, not a complete production chunker.
9

Chunk Size vs Retrieval Quality

Chunking creates a trade-off. Smaller chunks can make matching more focused, while larger chunks can preserve more context.

Smaller chunks
  • More focused retrieval
  • Less unrelated context
  • May lose surrounding explanation
Larger chunks
  • More surrounding context
  • Fewer chunks to manage
  • May include unrelated information
Practical rule:Do not choose a chunk size just because a tutorial says it is “optimal.” Test chunking on your own documents and questions.
10

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.

CHUNK A“Reset your password…”
→
EMBEDDING[0.12, -0.44, 0.81, …]
CHUNK B“Change your login…”
→
EMBEDDING[0.10, -0.39, 0.79, …]

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.

11

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.

GOOD CHUNK

Focused answer context

The retrieved text directly addresses the question and gives the model enough information to answer.

BAD CHUNK

Noisy context

The retrieved text contains the answer plus many unrelated sections, increasing the amount of irrelevant context.

Think of it this way:Embedding quality matters, but the embedding model cannot fix a badly chosen retrieval unit. If the right information is split incorrectly, retrieval may return incomplete or noisy context.
12

Chunking Is Not the Same as Summarization

Chunking and summarization solve different problems.

CHUNKING

Split the original content

The chunks normally preserve the source text so the retriever can return exact supporting content.

VS
SUMMARIZATION

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.

13

Common Chunking Mistakes

1. One giant chunk

The whole document is stored as one retrieval unit, reducing retrieval precision.

2. Extremely tiny chunks

Important context is fragmented across many pieces.

3. Ignoring document structure

Headings, tables, lists, and sections may provide useful boundaries.

4. Never evaluating

A chunk size that works for one dataset may perform poorly on another.

5. Losing metadata

Without source information, it becomes harder to cite or trace retrieved content.

6. Confusing chunks with tokens

A chunk is a retrieval unit; tokenization is how text is represented for model processing.

14

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.

RELEVANCEDid we retrieve the right topic?

Relevant chunks should address the user's question.

COMPLETENESSIs enough context present?

The answer should not be split across missing pieces.

NOISEHow much unrelated text?

Too much unrelated content can hurt the downstream answer.

Question“How do I reset my password?”
Retrieved chunksReset password steps + account recovery section
EvaluationRelevant + complete + low noise
PRACTICE

Design a Chunking Strategy

Imagine you are building a RAG assistant for a 200-page technical manual.

  1. Choose a logical unit you would try to keep together.
  2. Decide whether you would prefer very small, medium, or large chunks and explain why.
  3. List two pieces of metadata you would store with every chunk.
  4. Write one question you would use to test whether your chunks retrieve the right information.
Think first: there is no single chunk size that is automatically correct for every document. Your document structure and question patterns should influence the decision.

Quick Quiz

1. What is chunking?

A. Converting a vector into text
B. Splitting larger content into smaller retrievable pieces
C. Training an LLM from scratch

2. Why can a huge chunk hurt retrieval?

A. It can contain too much unrelated information
B. It always deletes metadata
C. It prevents embeddings from existing

3. What should good chunking try to preserve?

A. Meaningful context and useful boundaries
B. Random character positions only
C. The entire document in every chunk

30-Second Recap

1. ChunkSplit large content into smaller retrieval units.
2. SizeBalance focused retrieval with enough context.
3. BoundariesPrefer meaningful document or language boundaries.
4. EmbedCreate vectors from the chunks.
5. RetrieveUse similarity search to find relevant chunks.

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.