Chunking Strategies
Document chunking strategies for optimal RAG performance using the chonkie library
PraisonAI integrates chonkie for high-performance document chunking.
from praisonaiagents import Agent
agent = Agent(
name="researcher",
instructions="Use chunked knowledge for long documents",
knowledge=True,
)
agent.start("Find the section about deployment limits")The user queries long sources; chunking controls what reaches retrieval.
PraisonAI integrates chonkie for high-performance document chunking.
Quick Start
- Default chunking on an agent
Agent with Chunking (Simplest) from praisonaiagents import Agent # Default chunking (token-based) agent = Agent( instructions="Answer questions from documents.", knowledge=["research.pdf", "docs/"] ) response = agent.start("What are the key findings?")Agent with Custom Chunking from praisonaiagents import Agent agent = Agent( instructions="Answer questions from documents.", knowledge={ "sources": ["research.pdf"], "chunker": { "type": "semantic", # token, sentence, recursive, semantic, sdpm, late "chunk_size": 512, "chunk_overlap": 128 } } ) response = agent.start("What methodology was used?")Direct Chunking API from praisonaiagents import Chunking # Create chunker chunker = Chunking( chunker_type="recursive", chunk_size=512, chunk_overlap=128 ) # Chunk text chunks = chunker.chunk("Your document content here...") for chunk in chunks: print(f"Tokens: {chunk.token_count}") print(chunk.text[:100])
Available Strategies
| Strategy | Best For | Speed |
|---|---|---|
token | Fixed-size chunks | ⚡ Fastest |
sentence | Natural boundaries | ⚡ Fast |
recursive | Structured docs (markdown) | ⚡ Fast |
semantic | Topic segmentation | 🔄 Medium |
sdpm | Research papers | 🔄 Medium |
late | Best embeddings | 🔄 Medium |
Fixed-size token chunks. Fast and predictable.
Sentence ChunkingSplit at sentence boundaries. Natural flow.
Recursive ChunkingHierarchical splitting. Great for markdown.
Semantic ChunkingSimilarity-based splits. Topic coherence.
Chunker Configuration
All Parameters
| Parameter | Type | Default | Applies To |
|---|---|---|---|
type | str | "token" | All |
chunk_size | int | 512 | All |
chunk_overlap | int | 128 | token, sentence |
tokenizer_or_token_counter | str | "gpt2" | token, sentence, recursive |
embedding_model | str | auto | semantic, sdpm, late |
Strategy Examples
Token
agent = Agent(
instructions="Process documents.",
knowledge={
"sources": ["docs/"],
"chunker": {
"type": "token",
"chunk_size": 256,
"chunk_overlap": 50
}
}
)Sentence
agent = Agent(
instructions="Process articles.",
knowledge={
"sources": ["articles/"],
"chunker": {
"type": "sentence",
"chunk_size": 512
}
}
)Recursive
agent = Agent(
instructions="Process markdown docs.",
knowledge={
"sources": ["README.md", "docs/"],
"chunker": {
"type": "recursive",
"chunk_size": 512
}
}
)Semantic
agent = Agent(
instructions="Process research papers.",
knowledge={
"sources": ["papers/"],
"chunker": {
"type": "semantic",
"chunk_size": 512,
"embedding_model": "all-MiniLM-L6-v2"
}
}
)Which Strategy Should I Use?
How It Works
The user adds documents; the chunking engine splits them by the chosen strategy, embeds each chunk, and stores it for retrieval.
Installation
pip install "praisonaiagents[knowledge]"This installs the chonkie library automatically.
Best Practices
Match chunk size to retrieval use case
Smaller chunks improve precision for Q&A; larger chunks preserve narrative for summarisation tasks.
Install the knowledge extra
Run pip install "praisonaiagents[knowledge]" so chonkie and indexing backends load only when needed.
Test overlap on sample docs
Tune overlap on a representative document before indexing an entire corpus.
Pair with smart retrieval
Combine chunking with hybrid search and reranking for better recall on long knowledge bases.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
ImportError: chonkie package not found. Please install it using: pip install 'praisonaiagents[knowledge]' on the first Knowledge.add(...) / agent.start(...) call | semantic (the default), sdpm, or late chunker is selected but the [knowledge] extra is not installed. Since PraisonAI PR #5105 both the chunker registry and the embedding backend surface the same actionable hint. | pip install 'praisonaiagents[knowledge]'. Or fall back to chunking_strategy="sentence" / "paragraph" / "fixed" / "token", which do not need chonkie’s embedding model. |
ImportError that names chonkie.embeddings but omits the pip install hint | chonkie is installed but its embeddings submodule cannot import (broken sentence-transformers, torch, or transformers). PR #5105 intentionally preserves the original error so real transitive failures are not masked. | Read the underlying error, then reinstall or upgrade that package (pip install --upgrade sentence-transformers is the most common fix). |