Token-Based Sizing
Cognee uses token-based sizing for chunks, rather than character counts. This means thatchunk_size refers to the maximum number of tokens allowed in a chunk, which is directly tied to the tokenizer used by your embedding model. This ensures that chunks are always within the model’s context window.
Available Chunkers
Cognee provides several built-in chunkers to handle different types of content:- TextChunker: The default chunker. It splits text by paragraphs while respecting the token limit. It tries to keep paragraphs together but will split them if they exceed the
chunk_size. - CsvChunker: Designed specifically for CSV data. It splits by rows, ensuring that each chunk contains complete rows and does not break data in the middle of a record.
- LangchainChunker: Wraps LangChain’s
RecursiveCharacterTextSplitter. It splits text recursively by characters (e.g.,\n\n,\n,) and supportschunk_overlap(in words; default:10). Requirespip install cognee[langchain]. - TextChunkerWithOverlap: A paragraph-based chunker that supports overlap via a
chunk_overlap_ratio(a fraction ofchunk_size, e.g.0.2= 20% overlap). Useful for maintaining context across chunk boundaries.
Overlap only works with
LangchainChunker and TextChunkerWithOverlap. The default TextChunker splits strictly at paragraph boundaries and does not use overlap. Calling cognee.config.set_chunk_overlap() has no effect when using TextChunker.Additional Information
Using a Specific Chunker
Using a Specific Chunker
Using Chunk Overlap
Using Chunk Overlap
Chunk overlap causes consecutive chunks to share a portion of text, which helps preserve context at chunk boundaries and can improve entity extraction quality — at the cost of more LLM calls and a slightly larger graph.
TextChunkerWithOverlap takes a chunk_overlap_ratio between 0.0 and 1.0 (fraction of chunk_size). Because this value currently cannot be passed through cognee.config.set_chunk_overlap() at runtime, configure it by subclassing the chunker:LangchainChunker uses the same subclassing pattern and accepts a chunk_overlap parameter measured in words (default: 10). It requires pip install cognee[langchain].Custom Chunkers
Custom Chunkers
You can create a custom chunker by inheriting from the
Chunker base class and implementing the read method. Your chunker must yield DocumentChunk objects.Configuring chunk size via Docker or REST API
Configuring chunk size via Docker or REST API
The With Docker Compose, add these to your Then start the server:With
/cognify REST API endpoint does not accept a chunk_size parameter directly. When you use Cognee through Docker or the HTTP API, set the chunk size via environment variables instead:.env before starting:docker run, pass them inline:The
chunk_size environment variable is read once at startup. Restart the container after changing it.Chunk Size and Graph Quality
Chunk Size and Graph Quality
The
If
chunk_size passed to cognify() directly affects how the knowledge graph is built:| Smaller chunks | Larger chunks | |
|---|---|---|
| Entity granularity | Fine-grained — each entity fits in fewer chunks | Coarser — entities may span fewer nodes |
| Context per chunk | Less surrounding context for the LLM | More surrounding context for the LLM |
| LLM calls | More calls (higher cost, slower) | Fewer calls (lower cost, faster) |
| Best for | Dense, technical text (code, legal, science) | Narrative or long-form prose |
chunk_size is not set, Cognee auto-calculates it as the minimum of your embedding model’s context window and half of your LLM’s context window.