Chunking is the process of splitting documents into smaller, semantically meaningful pieces. Proper chunking is critical for RAG performance - chunks that are too large lose precision, while chunks that are too small lose context.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/mastra-ai/mastra/llms.txt
Use this file to discover all available pages before exploring further.
Chunking Strategies
Mastra provides multiple strategies optimized for different content types:Recursive
Hierarchically splits text using multiple separators. Best for general content.
Markdown
Preserves markdown structure and headers. Ideal for documentation.
HTML
Respects HTML structure and sections. Use for web content.
Semantic
Groups semantically related content. Best for narrative text.
Code
Language-aware splitting. Preserves code structure.
JSON
Recursive JSON splitting. Handles nested structures.
Recursive Chunking (Default)
Recursive chunking splits text using a hierarchy of separators:Maximum chunk size in characters
Number of characters to overlap between chunks
Custom separator hierarchy. Defaults to
['\n\n', '\n', ' ', '']Where to place the separator in chunks
Language-Aware Chunking
For code files, specify the language for syntax-aware chunking:Markdown Chunking
Preserve markdown structure and hierarchy:Header patterns to preserve:
[markdown_prefix, header_name]Return each line as a separate chunk
Remove header markers from chunk text
Header Metadata
Markdown chunking adds header hierarchy to metadata:HTML Chunking
Split HTML by semantic sections:Semantic Markdown Chunking
Group semantically related content:Semantic similarity threshold (0-1) for joining chunks
Model for token counting
Encoding for token counting
JSON Chunking
Handle nested JSON structures:Maximum chunk size (required for JSON)
Minimum chunk size before splitting
Escape non-ASCII characters
Convert lists to separate chunks
Token-Based Chunking
Chunk by token count instead of characters:Token-based chunking is useful when you need precise token counts for embedding models with token limits.
Sentence Chunking
Split by sentences while respecting size limits:Maximum chunk size
Minimum chunk size
Target chunk size to aim for
Characters that end sentences
Fall back to word splitting if sentences are too long
Fall back to character splitting if words are too long
Character Chunking
Simple splitting by separator:Chunk Overlap
Overlap maintains context between chunks:Metadata Extraction
Extract metadata during chunking:Metadata extraction runs after chunking, enriching each chunk with AI-generated context.
Custom Length Functions
Use custom length calculations:Complete Chunking Pipeline
Put it all together:Choosing Chunk Size
Optimal chunk size depends on your use case:- Small (200-500)
- Medium (500-1000)
- Large (1000-2000)
Best for:
- Precise information retrieval
- Question answering
- Fact extraction
- High precision
- Lower token usage
- May lose context
- More chunks to manage
Best Practices
Match Content Type
Use markdown chunking for docs, code chunking for code, semantic for narratives.
Test Different Sizes
Experiment with 500, 1000, and 1500 character chunks to find optimal size.
Use 10-20% Overlap
Overlap maintains context across chunk boundaries without excessive duplication.
Extract Metadata
Add titles, summaries, and keywords to improve retrieval accuracy.
Troubleshooting
Chunks too small
Chunks too small
- Increase
maxSize - Reduce number of separators
- Use character or token chunking for uniform sizes
Chunks too large
Chunks too large
- Decrease
maxSize - Add more separators to hierarchy
- Use sentence chunking with
maxSizelimit
Lost context between chunks
Lost context between chunks
- Increase
overlap(try 20% of chunk size) - Use larger chunks
- Extract summaries for each chunk
Poor retrieval quality
Poor retrieval quality
- Experiment with different chunk sizes
- Add metadata extraction
- Try semantic or markdown chunking
- Verify separator hierarchy matches content structure
Next Steps
Ingestion
Learn about document loading and preprocessing
Retrieval
Implement semantic search and reranking
RAG Overview
Return to RAG overview