Stop Wasting Tokens! semchunk Cuts RAG Errors by 15%
Your RAG pipeline is bleeding accuracy—and you don't even know it.
Every day, thousands of developers feed their language models chunks of text that sever ideas mid-sentence, split legal clauses in half, and destroy the very context their retrieval systems desperately need. The result? Hallucinations, wrong answers, and embarrassed demos in front of stakeholders. You've felt that sting. You've seen your "intelligent" system pull the wrong chunk and confidently deliver nonsense.
But what if your text splitter actually understood what it was cutting?
Enter semchunk, the lightning-fast Python↗ Bright Coding Blog library that's quietly becoming the secret weapon of teams at Microsoft, Docling, and production systems handling millions of requests monthly. While others blindly chop text at fixed token counts, semchunk preserves semantic meaning with surgical precision—and its new AI-powered mode delivers 15% better RAG performance than anything else on the market.
Ready to stop sabotaging your own AI? Let's dive in.
What is semchunk?
semchunk is a fast, lightweight, and brutally effective Python library for splitting text into semantically meaningful chunks. Created by Umar Butler and maintained by Isaacus, it solves one of NLP's most deceptively simple problems: how do you break long documents into pieces without breaking their meaning?
The library has exploded in popularity, racking up millions of monthly downloads and earning production trust from heavyweight systems like:
- Docling – IBM's document processing toolkit
- Microsoft Intelligence Toolkit – Enterprise-grade AI infrastructure
- Isaacus API – Legal AI platform powering real-world applications
But here's what makes semchunk genuinely different from the sea of chunking utilities on PyPI.
Most chunkers treat text like a sausage to be sliced—uniform pieces regardless of what's inside. semchunk treats text like a document with structure, using a novel hierarchical recursive algorithm that respects paragraphs, sentences, clauses, and even word boundaries in order of semantic importance. And with version 4.0's AI-powered chunking via Isaacus enrichment models, it can now leverage language understanding to identify optimal split points that no rule-based system could catch.
The library's philosophy is radical in its simplicity: preserve local semantic context at all costs. When your retriever fetches a chunk, that chunk should contain complete thoughts—not fragments that leave your generator guessing.
Key Features That Make semchunk Insane
🔬 Novel Hierarchical Chunking Algorithm
semchunk doesn't just split—it reasons about structure. Its recursive splitter prioritizes boundaries by semantic weight:
- Paragraph breaks (newlines/carriage returns) – strongest semantic boundary
- Tab sequences – structural separators like indented blocks
- Whitespace runs – general content separation
- Sentence terminators (
.,?,!) – complete thoughts preserved - Clause separators (
;,,, brackets, quotes) – sub-sentence boundaries - Sentence interrupters (
:,—,…) – mid-thought pauses - Word joiners (
/,\,–,&,-) – weakest meaningful boundary - All other characters – absolute last resort
This hierarchy ensures your chunks never split mid-sentence unless absolutely unavoidable.
🤖 AI-Powered Chunking (New in 4.0)
The game-changer. By passing an Isaacus enrichment model like kanon-2-enricher, semchunk sends text through a neural understanding layer that identifies semantic spans—natural content boundaries invisible to rule-based systems. It constructs a containment tree of spans and navigates it intelligently, falling back to hierarchical splitting only where needed.
🔗 Universal Tokenizer Compatibility
Works with any tokenizer or token counter:
- OpenAI models (
gpt-4,cl100k_base) - Hugging Face Transformers (
AutoTokenizer) - Tiktoken encodings
- Custom functions (
lambda text: len(text.split()))
⚡ Production-Grade Performance
- Multiprocessing support for batch chunking
- Memoization with configurable cache bounds
- Progress bars for long-running operations
- Chunk overlap (ratio or absolute tokens)
- Character offsets returned for precise source attribution
- Whitespace-only chunk exclusion (v3.0+)
Use Cases Where semchunk Destroys the Competition
1. Legal Document RAG
Legal texts have brutal complexity: nested clauses, cross-references, and definitions that span pages. Split a contract mid-clause and your system might miss a liability limitation entirely. semchunk's AI mode understands legal structure via Isaacus's legal-specialized models, making it the choice for legal AI platforms.
2. Technical Documentation Search
API docs mix prose, code blocks, tables, and parameter lists. Fixed-size chunking slices code examples in half, destroys table rows, and separates parameters from their descriptions. semchunk's hierarchical splitter respects structural boundaries, keeping code blocks intact and parameter definitions with their names.
3. Long-Form Content Summarization
When summarizing books, articles, or research papers, you need chunks that contain complete arguments. A chunk ending mid-evidence or mid-conclusion poisons your summarization. semchunk's sentence-aware splitting ensures each chunk carries complete rhetorical units.
4. Multi-Turn Conversation Context Windows
Feeding conversation history to LLMs? You need chunks that preserve dialogue turns. semchunk's paragraph and sentence boundaries naturally align with speaker transitions, keeping "who said what" coherent across chunks.
5. Scientific Literature Processing
Research papers have rigid structure: abstract, introduction, methods, results, discussion. semchunk's tab and newline awareness preserves section boundaries, ensuring your retriever never returns a "methods" chunk contaminated with "results" interpretation.
Step-by-Step Installation & Setup Guide
Basic Installation
# Standard pip installation
pip install semchunk
# Lightning-fast with uv
uv pip install semchunk
# Conda users
conda install conda-forge::semchunk
# or
conda install -c conda-forge semchunk
AI-Powered Chunking Setup
For the full neural experience, add the Isaacus SDK:
pip install isaacus
Then configure your API key:
export ISAACUS_API_KEY="your-key-here"
Or in Python:
from os import environ
environ["ISAACUS_API_KEY"] = "your-key-here"
Grab your key at platform.isaacus.com/accounts/signup/.
Environment Verification
import semchunk
print(semchunk.__version__) # Verify installation
Rust Alternative
For Rustaceans, @dominictarro maintains semchunk-rs:
cargo add semchunk-rs
REAL Code Examples from the Repository
Example 1: Basic Chunking with Multiple Tokenizers
This snippet from semchunk's quickstart demonstrates its universal tokenizer interface—the same chunker constructor works across completely different tokenization ecosystems:
import semchunk
import tiktoken # Transformers and Tiktoken are not dependencies,
from transformers import AutoTokenizer # they're just here for demonstration purposes.
chunk_size = 4 # A low chunk size is used here for demonstration purposes. Keep in mind, semchunk
# does not know how many special tokens, if any, your tokenizer adds to every input,
# so you may want to deduct the number of special tokens added from your chunk size.
text = 'The quick brown fox jumps over the lazy dog.'
# You can construct a chunker with `semchunk.chunkerify()` by passing the name of an OpenAI model,
# OpenAI Tiktoken encoding or Hugging Face model, or a custom tokenizer that has an `encode()` method
# (like a Tiktoken or Transformers tokenizer) or a custom token counting function that takes a text and
# returns the number of tokens in it.
chunker = semchunk.chunkerify('isaacus/kanon-2-tokenizer', chunk_size) or \
semchunk.chunkerify('gpt-4', chunk_size) or \
semchunk.chunkerify('cl100k_base', chunk_size) or \
semchunk.chunkerify(AutoTokenizer.from_pretrained('isaacus/kanon-2-tokenizer'), chunk_size) or \
semchunk.chunkerify(tiktoken.encoding_for_model('gpt-4'), chunk_size) or \
semchunk.chunkerify(lambda text: len(text.split()), chunk_size)
# If you give the resulting chunker a single text, it'll return a list of chunks. If you give it a
# list of texts, it'll return a list of lists of chunks.
assert chunker(text) == ['The quick brown fox', 'jumps over the', 'lazy dog.']
assert chunker([text], progress=True) == [['The quick brown fox', 'jumps over the', 'lazy dog.']]
# If you have a lot of texts and you want to speed things up, you can enable multiprocessing by
# setting `processes` to a number greater than 1.
assert chunker([text], processes=2) == [['The quick brown fox', 'jumps over the', 'lazy dog.']]
# You can also pass an `offsets` argument to return the offsets of chunks, as well as an `overlap`
# argument to overlap chunks by a ratio (if < 1) or an absolute number of tokens (if >= 1).
chunks, offsets = chunker(text, offsets=True, overlap=0.5)
What's happening here? The chunkerify() function is doing something remarkable: it's creating a tokenizer-agnostic chunker that normalizes across Tiktoken, Transformers, and even raw Python functions. The or chain demonstrates fallback options—you'd typically use just one. Notice the critical warning about special tokens: many tokenizers add <|endoftext|> or similar tokens to every input, so your effective chunk_size might need reduction. The progress=True flag triggers a tqdm bar for batch operations, while processes=2 enables true multiprocessing for throughput. The offsets=True return is gold for source attribution in RAG—you can highlight exactly where each chunk came from in the original document.
Example 2: AI-Powered Chunking for Long Documents
This is where semchunk 4.0 flexes its neural muscles:
import requests # For demonstration purposes, we'll use `requests` to download a long document.
import semchunk
from os import environ
# Set your `ISAACUS_API_KEY` environment variable to your Isaacus API key.
environ["ISAACUS_API_KEY"] = "INSERT_YOUR_API_KEY_HERE"
# Download a very long document to chunk.
text = requests.get("https://examples.isaacus.com/dred-scott-v-sandford.txt").text
# Construct a chunker that uses `kanon-2-enricher` for AI-powered chunking.
# NOTE Because we're using a Hugging Face Transformers tokenizer, the `transformers` library is required here,
# however, you can use any tokenizer or token counter you like.
chunker = semchunk.chunkerify("isaacus/kanon-2-tokenizer", 512, chunking_model="kanon-2-enricher")
# Chunk the document with AI-powered chunking.
chunks = chunker(text)
The magic under the hood: This isn't just "call an API and hope." semchunk first applies its hierarchical algorithm to create 1,000,000-character guardrail chunks, then sends these to the kanon-2-enricher model. The enrichment model extracts semantic spans—boundaries like "Issue," "Holding," "Reasoning" in legal texts. semchunk then builds a containment tree from these spans, ensuring parent concepts aren't separated from children. If a span exceeds chunk size, it recurses into children; if no children exist, it falls back to the hierarchical algorithm. The result? Chunks that align with meaningful document structure rather than arbitrary token boundaries.
Example 3: Direct chunk() API for Custom Workflows
For cases where you need lower-level control:
from semchunk import chunk
# Direct chunking with a custom token counter
def my_token_counter(text: str) -> int:
"""Simple whitespace tokenization for demonstration."""
return len(text.split())
result = chunk(
text="Your long document here...",
chunk_size=256,
token_counter=my_token_counter,
offsets=True, # Return (start, end) positions
overlap=0.2, # 20% overlap between chunks
memoize=True, # Cache token counts for speed
cache_maxsize=1024 # Bound the cache
)
# result is either list[str] or tuple(list[str], list[tuple[int, int]])
When to use chunk() vs chunkerify()? Use chunk() for one-off operations or when you're building your own chunking pipeline where the tokenizer setup is handled elsewhere. Use chunkerify() when you want a reusable, configured chunker function you can pass around your application. The chunk() function exposes the same overlap and offset capabilities but gives you direct control over the token counter implementation.
Advanced Usage & Best Practices
🎯 Optimize for Your Tokenizer's Special Tokens
# GPT-4 adds special tokens; reduce chunk_size accordingly
encoder = tiktoken.encoding_for_model('gpt-4')
# Test: how many tokens for empty string?
special_tokens = len(encoder.encode(''))
chunk_size = 8192 - special_tokens # Instead of raw 8192
🚀 Batch Processing at Scale
# Process thousands of documents with multiprocessing + progress
chunker = semchunk.chunkerify('cl100k_base', 512)
documents = load_your_corpus() # Your data source
results = chunker(
documents,
processes=8, # Match your CPU cores
progress=True # Visual feedback for long jobs
)
🔄 Smart Overlap Strategies
# For retrieval: small overlap catches boundary-crossing queries
chunks = chunker(text, overlap=0.1) # 10% overlap
# For summarization: larger overlap preserves context continuity
chunks = chunker(text, overlap=50) # 50 tokens absolute
💾 Memoization Tuning
# For repeated chunking of similar texts (e.g., templated documents)
chunker = semchunk.chunkerify(
'gpt-4',
1024,
memoize=True,
cache_maxsize=10000 # Prevent unbounded growth
)
⚖️ Legal/Structured Document Mode
Pass Isaacus ILGS Documents directly—the AI chunking activates automatically without re-enriching, saving API calls and latency.
Comparison with Alternatives
| Feature | semchunk | LangChain Recursive | Chonkie | Fixed-Size |
|---|---|---|---|---|
| RAG Correctness | 37.7% (AI) / 35.5% (standard) | 34.8% | 32.6% | 33.3% |
| Semantic Awareness | ✅ Hierarchical + AI | ⚠️ Recursive rules only | ⚠️ Semantic + recursive | ❌ None |
| Universal Tokenizers | ✅ Any callable/model | ⚠️ Limited set | ⚠️ Limited set | ❌ Usually custom |
| Chunk Overlap | ✅ Ratio or absolute | ✅ Configurable | ✅ Configurable | ❌ Rare |
| Character Offsets | ✅ Built-in | ❌ Usually not | ❌ Usually not | ❌ No |
| Multiprocessing | ✅ Native | ❌ External | ❌ External | ❌ External |
| Production Usage | ✅ Microsoft, Docling | ⚠️ Common but basic | ⚠️ Newer | ❌ Avoid |
| Speed | ⚡ Optimized | ⚠️ Moderate | ⚠️ Moderate | ⚡ Fast but wrong |
The verdict? On the Legal RAG QA benchmark, semchunk's AI mode achieves 37.7% correctness—a 15% relative improvement over its closest competitor and 8% over its own standard mode. That's not marginal; that's the difference between a demo that impresses and one that embarrasses.
FAQ
Is semchunk free for commercial use?
Yes. semchunk is MIT-licensed. The core library is completely free. AI-powered chunking requires an Isaacus API key with usage-based pricing, but standard semantic chunking costs nothing.
Does semchunk work with my custom tokenizer?
Absolutely. Any callable that takes a string and returns an integer token count works. Pass it to chunkerify() or chunk(). This includes Tiktoken, Transformers, SentencePiece, or your own regex-based counter.
How does AI chunking handle API failures?
semchunk gracefully falls back to its hierarchical algorithm if the Isaacus API is unavailable. Your pipeline stays resilient.
What's the performance overhead of AI chunking?
AI chunking adds API latency but dramatically improves quality. For throughput-critical paths, use standard mode; for accuracy-critical paths (legal, medical, financial), the AI mode's 15% accuracy gain typically justifies the latency.
Can I use semchunk without internet access?
Yes for standard mode—it's fully local. No for AI mode—it requires Isaacus API connectivity. The Rust port (semchunk-rs) offers additional deployment flexibility.
How do I cite semchunk in research?
Use the provided BibTeX entry in the repository, or reference: Butler, U. (2023). semchunk: a Python library for semantic chunking. Isaacus.
Does chunk overlap hurt retrieval performance?
Strategic overlap (10-20%) generally helps by ensuring boundary-crossing queries find complete context. Excessive overlap (>50%) can dilute retrieval signal. semchunk lets you tune this precisely.
Conclusion
Here's the uncomfortable truth: your chunking strategy is the invisible ceiling on your RAG system's performance. You can spend thousands optimizing embeddings and reranking, but if your chunks split thoughts in half, you're building on sand.
semchunk changes the equation. Its hierarchical algorithm respects document structure. Its AI mode understands semantic boundaries. And its production pedigree—millions of downloads, Microsoft adoption, proven benchmarks—means you can deploy with confidence.
The 15% accuracy improvement isn't a lab curiosity. It's the difference between a system that answers correctly and one that hallucinates confidently. In legal AI, medical QA, financial analysis—domains where wrong answers have real consequences—that margin is everything.
Stop slicing your text blindly. Start chunking with semantic intelligence.
👉 Get semchunk on GitHub — star it, install it, and watch your RAG accuracy climb. Your future self—and your users—will thank you.
pip install semchunk
The chunks you save may be your own.