RAG

Document Chunking for RAG👨‍💻

Chunking is the process of breaking long documents into smaller pieces before embedding and indexing them for retrieval. It is arguably the most impactful decision in a RAG pipeline, yet it receives far less attention than model selection or prompt engineering.

Why does chunking matter? Embedding models compress a piece of text into a single fixed-length vector. If you embed an entire 50-page document as one vector, that vector must represent everything in those 50 pages -- it becomes a vague average of all topics covered. When a user asks a specific question, the vague document-level vector may not be close enough to the query vector to surface in search results. But if you chunk that document into 100 focused paragraphs, the paragraph that actually answers the question will have a precise embedding that matches the query well.

The challenge is finding the right granularity. Chunks that are too small lose context. Chunks that are too large dilute relevance. Overlap strategies, separator hierarchies, and semantic boundaries all play a role in getting this right.

Key Takeaways

  • 1Chunking determines retrieval precision: smaller, focused chunks produce embeddings that match specific queries, while larger chunks capture more context but dilute the signal for any single topic.
  • 2Fixed-size chunking (split every N characters or tokens) is the simplest strategy and works surprisingly well as a baseline. It is fast, deterministic, and requires no document structure analysis.
  • 3Recursive character splitting uses a hierarchy of separators (paragraphs, sentences, words) and recursively splits text until each chunk fits within the size limit, preserving natural boundaries.
  • 4Overlap between consecutive chunks ensures that information near a boundary is not lost. Typical overlap is 10-20% of chunk size -- enough to capture context without excessive duplication.
  • 5Semantic chunking groups sentences by embedding similarity, creating chunks that represent coherent topics. It produces the most meaningful chunks but is slower and harder to implement.
  • 6Chunk size depends on your embedding model's sweet spot, the context window of your LLM, and the nature of your documents. There is no universal best size -- you must benchmark on your own data.

Examples

Fixed-size chunking with overlap (no dependencies)

python

Fixed-size chunking is the most predictable strategy. The overlap parameter ensures that information at chunk boundaries appears in both the preceding and following chunk, so retrieval does not miss content that spans a split point. This is your baseline -- try this first before more complex strategies.

Recursive character splitting with LangChain

python

RecursiveCharacterTextSplitter is the most widely used chunking strategy in production RAG systems. It tries to split on paragraph boundaries first (double newline), then sentences (period-space), then words, and only falls back to character-level splitting if needed. This preserves the natural structure of the text while respecting the chunk size limit.

Token-aware chunking with tiktoken

python

Character-based chunking can exceed embedding model token limits because tokens and characters do not have a fixed ratio. tiktoken (OpenAI's tokenizer) lets you split on actual token boundaries. This is critical when using models with strict token limits -- input beyond the limit is silently truncated, silently degrading embedding quality.

Semantic chunking -- group sentences by embedding similarity

python

Semantic chunking creates boundaries where the topic shifts, producing chunks that are coherent units of meaning. It embeds each sentence and measures similarity between consecutive sentences -- when similarity drops below the threshold, a new chunk begins. This produces better retrieval results for documents that cover multiple topics but is slower due to the per-sentence embedding step.

Benchmarking chunk sizes on retrieval quality

python

There is no universal best chunk size. This evaluation framework measures retrieval recall at different chunk sizes using your actual queries and expected answers. Run it during development to find the optimal size for your specific content. The overlap ratio of 10% prevents boundary information loss without excessive chunk count inflation.

Common Mistakes

Mistake:

Using a single chunk size for all document types. A 500-character chunk works for technical documentation but is far too small for legal contracts with long clauses and too large for FAQ entries.

Fix:

Tune chunk size per document type. Use smaller chunks (200-400 chars) for dense, fact-heavy content like FAQs and API docs. Use larger chunks (800-1500 chars) for narrative documents like reports and contracts where context spans longer passages.

Mistake:

Setting overlap to zero, which means information at chunk boundaries is split across two chunks and may not be retrievable by either embedding.

Fix:

Use 10-20% overlap (e.g., 50-100 chars for a 500-char chunk). The overlap ensures boundary content is captured in at least one chunk with enough surrounding context for the embedding to represent it accurately.

Mistake:

Chunking by character count when the embedding model has a token limit. Characters and tokens do not have a fixed ratio -- 500 characters can be anywhere from 100 to 200 tokens depending on the text.

Fix:

Use token-aware chunking with tiktoken or the tokenizer for your embedding model. Set the chunk size in tokens, not characters, to ensure no chunk exceeds the model's maximum input length.

Mistake:

Splitting in the middle of sentences, code blocks, or tables, producing chunks that are incomplete and whose embeddings do not represent any coherent meaning.

Fix:

Use recursive splitting with separators that respect document structure: paragraph breaks, then sentence boundaries, then word boundaries. For code and markdown, use format-aware splitters that recognize code fences and table syntax.

Mistake:

Never evaluating chunking quality and treating chunk size as a set-and-forget configuration. As your document corpus grows and diversifies, the original settings may degrade retrieval performance.

Fix:

Build a test set of queries with known correct source chunks. Measure retrieval recall at different chunk sizes and overlap values. Re-run this evaluation whenever you add a new document type to your knowledge base.

Best Practices

  • Start with RecursiveCharacterTextSplitter at 500 characters with 50-character overlap. This is the most widely validated default and works well across document types. Optimize from there based on evaluation.
  • Preserve document metadata through the chunking pipeline. Every chunk should carry its source filename, page number, section heading, and position so the LLM can cite sources and users can verify answers.
  • Use token-aware splitting when your embedding model has a strict token limit. Silently truncated inputs produce degraded embeddings that retrieve poorly, and the failure mode is invisible.
  • Consider document structure when choosing separators. Markdown documents should split on headings before paragraphs. Code files should split on function boundaries. PDFs should split on page breaks before paragraphs.
  • Test your chunking strategy with edge cases: very short documents (should they be one chunk or split?), documents with tables and code blocks, and documents in multiple languages.
  • Keep chunk size as a runtime parameter, not a hardcoded constant. This makes it easy to A/B test different sizes and adapt as your document corpus changes.

Summary

Document chunking is the most underrated optimization in RAG pipelines. The right chunk size and splitting strategy determine whether retrieval returns the precise passage that answers the query or a vague fragment that confuses the LLM. Start with recursive character splitting at 500 characters with 10% overlap. Graduate to token-aware or semantic chunking when your evaluation shows the baseline is not enough. Always benchmark on your own queries and documents -- the optimal chunk size depends on your content type, embedding model, and retrieval requirements.

Practice RAG with hands-on challenges

Learn document chunking for rag hands-on in your IDE

Interactive lessons and challenges on Stanza, practice in VS Code, Cursor, or the web.

Related Concepts

Related Cheatsheets

Master RAG with Stanza

Interactive lessons and challenges, right in your code editor.

Check the free courses. No credit card.