TextChunker splits your documents in the wrong places — for example across Markdown sections — and you still want update() to re-extract only the chunks an edit touched.
Before You Start
- Complete Quickstart to understand basic operations
- Ensure you have LLM Providers configured (
LLM_API_KEYin.env) - Read Chunkers for the rules a custom chunker follows
- No data is required up front — the script ingests its own text, but it starts with
cognee.forget(everything=True), which wipes all existing Cognee data; run it against a setup you can afford to reset
Code in Action
What Just Happened
Step 1: Select the Chunker by Name
chunker uses the one DEFAULT_CHUNKER names. See Default chunker for a deployment for how the setting resolves.
Step 2: Define the Chunker
read() splits the text before each heading line, then hands each section to an inner TextChunker, so a section over the token budget is still cut to size. The split uses a lookahead, so the sections join back to the original text with nothing lost or repeated. That is what lets update() replace only the chunks an edit touched.
The inner TextChunker sees one section at a time, so the ids it gives its chunks only count repeated text within that section. Two sections with identical text would then get the same chunk id. The loop therefore rebuilds each id with content_chunk_id(), counting repeats across the whole document. It also stamps the outer chunker’s chunker_id and renumbers chunk_index so the indexes run through the whole document.
Step 3: Register the Chunker
"SectionChunker" a valid value for DEFAULT_CHUNKER. A server needs the same call at startup.
Step 4: Update One Section
update() reports incremental with one region: it re-cuts that section, extracts its new chunk, and keeps the chunks of the other two sections as they are. recall() then answers with the new closing time. To see a full rebuild instead, change DEFAULT_CHUNKER between remember() and update(); the update() reference lists the fallback reasons.
Chunkers
How Cognee splits documents into chunks.
update()
Parameters and return values of update().