Library

Volume 2 · RAG Architecture

Notebook
Sign in

Section 19

Chunking Strategies

556 words · about 3 min

19.1 The Core Chunking Methods

1. Fixed-Size Chunking — Split by character/token count (e.g., 512 tokens) with overlap (50-100 tokens) to preserve boundary context. Simple and predictable; may split mid-sentence/concept.

2. Recursive Character Splitting — Try splitting by paragraph, then sentence, then word, in order, respecting natural boundaries. Most commonly used default (e.g., LangChain’s RecursiveCharacterTextSplitter).

3. Semantic Chunking — Compute sentence-level embeddings, group consecutive sentences with high similarity, split when similarity drops below a threshold. Preserves semantic coherence but is computationally expensive and produces variable chunk sizes.

4. Document-Structure-Aware Chunking — Split on document headings/sections, preserving parent-child hierarchy in metadata. Ideal for well-structured documents (technical docs, legal contracts, policies).

5. Agentic Chunking — An LLM reads the document and decides optimal chunk boundaries directly. Highest quality, most expensive (LLM call per document) — reserve for high-value documents where quality justifies the cost.

6. Sliding Window Chunking — Fixed-size window that slides by a step smaller than the window size, creating overlapping chunks. Ensures no information is lost at boundaries at the cost of storage/retrieval redundancy.

7. Proposition-Based Chunking — LLM extracts atomic, self-contained factual propositions, each becoming its own chunk (e.g., “The company was founded in 2005” as one chunk). Excellent for FAQ-style/knowledge-base content where each fact should be independently retrievable.

19.2 Chunking Strategies by Data Type

Data Type Recommended Strategy Chunk Size Key Consideration
Technical docs Structure-aware + overlap 512-1024 tokens Preserve code blocks and headings
Legal contracts / payer rules Clause/section-level segmentation 256-512 tokens Never split mid-clause
Conversational data Turn-based + sliding window 3-5 turns Preserve dialog context
Code repositories Function/class-level + AST Per-function Preserve complete code units
Tables/CSV Row-group or full table Entire table if small Keep column headers with each chunk
Knowledge base articles Section-level 512-768 tokens Include article title in each chunk
Clinical/medical notes Section-based (HPI, ROS, Assessment, Plan) Paragraph/semantic Preserve clinical section boundaries

19.3 Chunk Size Decision Framework

                    What type of queries?
                    /              \
            Factual/Specific    Analytical/Complex
                  │                    │
           256-512 tokens        512-1024 tokens
                  │                    │
          Precision-focused     Context-rich
          (legal clauses,       (research, analysis,
           specific facts)      multi-paragraph reasoning)

19.4 Metadata Enrichment Per Chunk

{
  "chunk_id": "doc_123_chunk_5",
  "text": "The actual chunk content...",
  "source_document": "architecture_guide.pdf",
  "page_number": 12,
  "section_hierarchy": ["Chapter 3", "3.2 Deployment", "3.2.1 Kubernetes"],
  "document_type": "technical_documentation",
  "created_date": "2024-06-15",
  "chunk_index": 5,
  "total_chunks": 42,
  "parent_chunk_id": "doc_123_chunk_4",
  "entities": ["Kubernetes", "Docker", "Helm"],
  "access_groups": ["engineering"],
  "classification": "internal"
}

Section 19 — Interview Q&A

Q: How do you decide which chunking strategy to use for a given document type? A: I start from the query pattern, not the document format: precise factual lookups (legal clauses, specific facts) want small chunks (256-512 tokens) so the precision signal isn’t diluted; analytical/reasoning queries want larger, context-rich chunks (512-1024 tokens). Then I layer in document structure — well-structured content (manuals, contracts, policies) uses structure-aware chunking that splits on headings and never breaks a clause mid-sentence, since a fragmented legal clause is useless. Code always uses AST-based chunking at function/class boundaries — never mid-function. For genuinely unstructured or topically diverse content, semantic chunking (grouping by embedding similarity) earns its extra compute cost. I reserve agentic chunking for a small set of very high-value documents where the retrieval quality gain justifies an LLM call per document.