Skip to content

Chunking

Chunking splits each document's extracted text into the pieces that are embedded and searched. Every chunk keeps the location of the text it came from (page, slide, sheet, headings, rows, times), which becomes its citation.

Where the options come from

Options are merged in this order, each level overriding the one before:

  1. The server defaults in src/.env (CHUNKING_STRATEGY, CHUNK_SIZE, ...).
  2. The project's chunking, set when it is created or with PATCH /projects/{id}.
  3. The request's chunking options.

Each document stores the options it was processed with (chunking), so a reindex can repeat them.

# Project defaults
curl -X POST localhost:8000/api/v1/projects -H 'Content-Type: application/json' \
     -d '{"project_name": "handbook", "chunking": {"strategy": "recursive", "chunk_size": 800}}'

# Overridden for one upload (form fields)
curl -X POST localhost:8000/api/v1/projects/1/documents -F files=@report.pdf -F chunk_overlap=100

Strategies

auto (the default) picks a strategy from the content type:

Content Strategy picked by auto
Spreadsheets, CSV, Parquet, SQLite table
JSON json
Source code code
Markdown, HTML, Word (and Docling's Markdown) markdown
Slides page
Everything else recursive

You can choose one yourself:

Strategy What it does
fixed Fixed-size windows with overlap
token Fixed-size windows of tokens (tiktoken; sets length_unit to tokens)
recursive Paragraphs, then sentences, lines and words, merged up to the size, so wrapped PDF text is cut at sentence ends. Custom separators are supported
sentence Whole sentences merged up to the size
paragraph Whole paragraphs merged up to the size
page One chunk per page, slide, sheet, chapter or message (split if too long)
markdown Heading sections, keeping the heading path (headings)
code Splits at the class and function boundaries of the language
semantic Starts a new chunk where the meaning shifts (embedding distance)
table Groups rows and repeats the header in every chunk
json Splits into smaller valid JSON objects, keeping each value's path

Every strategy respects chunk_size: a section that is still too large is split recursively.

All options

Option Default
extractor native (DOCUMENT_EXTRACTOR) native or docling (Docling)
strategy auto (CHUNKING_STRATEGY) See above
chunk_size 1000 (CHUNK_SIZE) 20 to 100000, in length_unit
chunk_overlap 200 (CHUNK_OVERLAP) Smaller than chunk_size. A server default that is not is lowered to chunk_size / 5
length_unit chars (CHUNK_LENGTH_UNIT) chars or tokens (needs --extra tiktoken)
token_encoding cl100k_base (CHUNK_TOKEN_ENCODING) The tiktoken encoding for tokens
separators built in recursive: separators tried in order ("" is a hard split)
separators_regex false The separators are regular expressions
language from the file extension code: the programming language
rows_per_chunk fill chunk_size table: a fixed number of rows per chunk (1 to 100000)
table_format csv table: csv (header line, then rows), markdown (a Markdown table) or key_value (column: value per row)
semantic_threshold_type percentile semantic: percentile, standard_deviation, interquartile or gradient
semantic_threshold_amount 95 / 3 / 1.5 / 95 semantic: the amount for the threshold type
semantic_buffer_size 1 semantic: neighbouring sentences embedded with each sentence (0 to 10)
contextual_header false (CHUNK_CONTEXTUAL_HEADER) Embed each chunk with a file / page / section block in front
llm_context false (CHUNK_LLM_CONTEXT) Contextual retrieval: an LLM-written context per chunk
parent_chunk_size none (PARENT_CHUNK_SIZE) Group chunks into parents of up to this size, for small-to-big. Larger than chunk_size; 0 turns it off

Table-heavy documents

Tables are cut into row groups that fit chunk_size. When answers need many rows together (financial statements, price lists), a larger size, around 2000 characters, keeps a table's rows in one chunk and improves retrieval. Measure it with evaluation.

Semantic chunking

Each sentence (lines count as sentences too, for lists and table rows) is embedded with semantic_buffer_size neighbours on each side. The text is cut where the cosine distance between neighbouring sentences is above the threshold:

semantic_threshold_type Default amount Cut where the distance is above
percentile 95 This percentile of all distances
standard_deviation 3 The mean plus this many standard deviations
interquartile 1.5 The mean plus this many interquartile ranges
gradient 95 This percentile of the distance's gradient (where it changes fastest)

Groups longer than chunk_size are split recursively. The strategy uses the project's embedding model, so it costs one embedding per sentence (long documents are grouped first so the count stays bounded).

Contextual headers

With contextual_header, each chunk is embedded with a block that says where it comes from:

<document_metadata>
sourceDocument: report.pdf
title: ACME annual report 2024
page: 12
section: Results > Q3
</document_metadata>

Revenue grew 4% compared with ...

It adds the file name, title and author (when the file has them), the page range, the heading path and the table rows. The stored chunk text, the search results and the keyword index stay as they are. It costs nothing extra at ingestion.

Contextual retrieval (llm_context)

With llm_context, the configured LLM writes one to three sentences for each chunk that place it in its document, for example "From ACME's 2024 annual report, the Q3 revenue section". The context is embedded and keyword-indexed with the chunk. A chunk that never names its subject ("Revenue grew 4%") can then be found by that subject.

curl -X POST localhost:8000/api/v1/projects/1/documents \
     -F files=@report.pdf -F llm_context=true
  • Stored apart: the chunk text stays clean, and GET .../documents/{doc}/chunks returns the context as chunk_context.
  • Cost: one LLM call per chunk. Several run at once (LLM_CONCURRENCY, default 4), and re-processing reuses the context of every chunk whose text has not changed.
  • Long documents are sent as their start plus the part around each chunk, capped at LLM_CONTEXT_MAX_DOCUMENT_CHARS (60000).
  • Prompt caching: the document excerpt sits in a system prompt, so neighbouring chunks are cheaper on providers with prompt caching (Anthropic is cached explicitly; OpenAI and Gemini cache automatically).
  • Failures: nothing is indexed with contexts missing. A document that was already indexed keeps its previous index, with the error noted, and a new one is marked failed.

llm_context needs LLM_PROVIDER; without it the request returns 422. It works together with contextual_header. Use evaluation to check that it helps on your data: evaluate, re-process the documents with llm_context, and evaluate again with the same questions.

Parent chunks

With parent_chunk_size, consecutive chunks are also grouped into parent chunks of up to that size (in the same length_unit). The chunks don't change: they are still embedded and searched, and searches return the parent. See Small-to-big retrieval.

curl -X POST localhost:8000/api/v1/projects/1/documents \
     -F files=@handbook.pdf -F chunk_size=300 -F parent_chunk_size=1500

A PARENT_CHUNK_SIZE server default that is not larger than a request's chunk_size is ignored for that request. A parent_chunk_size set on the project or the request is an error (422) instead.

Trying options

curl -X POST localhost:8000/api/v1/data/preview -F file=@report.pdf -F strategy=semantic
curl -X POST localhost:8000/api/v1/projects/1/documents/7/preview \
     -H 'Content-Type: application/json' -d '{"chunking": {"chunk_size": 2000}}'

Nothing is stored. The response shows the chunks with their metadata, the chunk count and sizes, and the parents.