Chunking strategies
MediumRAGChunking
How do you chunk documents for RAG, and how do you choose chunk size?
TL;DR
Chunking splits documents into pieces small enough to embed precisely and fit in the prompt, but large enough to carry a complete idea. Too small and a chunk loses context ("it increased by 20%"; what did?). Too large and the embedding blurs several topics and wastes tokens. Start with structure-aware recursive splitting (by headings, then paragraphs, then sentences) at roughly 300–800 tokens with 10–20% overlap, then tune on an evaluation set of real questions instead of guessing.
Key points
- Fixed-size: split every N tokens. Simple, but cuts mid-sentence or mid-table.
- Recursive / structure-aware: respects headings, paragraphs and code blocks. The best default.
- Semantic chunking: splits where the embedding similarity between sentences drops (a topic shift). Sometimes better, more expensive.
- Document-specific: keep tables whole, split code by function, split FAQs per question-and-answer pair.
- Add context to each chunk: prepend the document title and section path ("Billing > Refunds > EU customers"), or generate a one-line summary of where the chunk fits (contextual retrieval).
- Parent-child (small-to-big): embed small chunks for precise matching, but send the larger parent section to the LLM.
- Store metadata (source, section, date, permissions) for filtering and citations.
- Parsing quality matters as much as chunking: bad PDF extraction (merged columns, lost tables) breaks everything downstream.
Example
text
Document: "Refund policy" > "EU customers"
Chunk text stored for embedding:
"Refund policy > EU customers: Customers in the EU can request a full refund
within 14 days of purchase, without giving a reason..."Common follow-ups
- Why overlap? So a sentence that straddles a boundary still appears complete in at least one chunk.
- How do you know a chunking change helped? Re-run retrieval metrics (recall@k) on the same labeled questions before and after.
What interviewers look for
Tip
The size trade-off, structure-aware splitting as the default, adding context to chunks, and tuning with evaluation.
- Red flag: one fixed chunk size picked once and never measured.