Back to Generative AI Notes
Topic #109

Document Chunking

Chunking splits a document's text into smaller pieces before embedding — because embedding an entire long document as a single vector loses too much specificity for precise retrieval, and because LLM context windows can't hold arbitrarily large amounts of retrieved text anyway.

Why Not Embed the Whole Document?

Embedding one 50-page document as a single vector:
  → the resulting vector represents an "average" of everything
    in the document — a very specific question about page 30
    won't be strongly reflected in a vector dominated by the
    other 49 pages' content

Embedding smaller, focused chunks:
  → each chunk's vector accurately represents its specific
    content, so a query about page 30's topic can match that
    chunk precisely, not get diluted by unrelated content

A Basic Chunking Example

def chunk_text(text, chunk_size=500, overlap=50):
    chunks = []
    start = 0
    while start < len(text):
        end = start + chunk_size
        chunks.append(text[start:end])
        start = end - overlap  # overlap prevents losing context
                                 # right at chunk boundaries
    return chunks

This simple fixed-size approach is a starting point — see Chunking Strategies for smarter approaches, and Chunk Size / Chunk Overlap for tuning these two specific parameters.

The Core Tradeoff

Chunk SizeEffect
Too smallLoses surrounding context; a chunk may not make sense on its own, or may miss the answer entirely if it's split across chunk boundaries
Too largeDilutes specificity (back to the "whole document" problem, just less severe); retrieves more irrelevant text alongside the relevant part

Practical Use Case

Every RAG system's retrieval quality is fundamentally shaped by its chunking strategy — two systems using the identical LLM and vector database can have meaningfully different answer quality purely based on how documents were chunked.

Common Mistakes

  • Using one fixed chunk size for structurally very different content (dense technical text vs conversational FAQ entries) without considering whether it suits each
  • Chunking purely by character/token count with no regard for natural document structure (splitting mid-sentence or mid-table)
  • No overlap between chunks, risking that relevant information spanning a chunk boundary gets split and neither chunk contains the complete answer

Interview Relevance

"Why does chunk size matter for RAG retrieval quality?" — the too-small-loses-context vs too-large-dilutes-specificity tradeoff above is exactly the expected answer.

Practice Question

A RAG system's answers are frequently missing information that spans across two adjacent chunks. Propose one specific fix.

Want to go beyond the notes?

Join Coding Now Tech Institute's Generative AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Document Chunking – FAQs

Quick answers about learning Document Chunking in Generative AI.

This free note from Coding Now Tech Institute explains Document Chunking in Generative AI — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Generative AI topic on Coding Now Tech Institute, including Document Chunking, is 100% free with no signup required.
With focused practice, most students grasp Document Chunking in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now