Skip to main content

Chunking

Chunking splits large text documents (.txt, .md, .html, .pdf, .docx) into smaller pieces that fit in the context limits of the LLM. The records of JSON, CSV, TSV and Excel files are not chunked, unless field names one long field of the record to split (see Split one field of a record). Agent Actions supports multiple chunking strategies. Each strategy is optimized for different use cases.

Configuration​

Configure chunking at the project level in agent_actions.yml:

yaml
YAML
default_agent_config:
chunk_config:
chunk_size: 4000
chunk_overlap: 500
split_method: tiktoken

Configuration Fields​

FieldDefaultDescription
chunk_size300The maximum size of each chunk (tokens or characters)
chunk_overlap10The overlap between consecutive chunks
split_methodtiktokenThe chunking strategy: tiktoken, chars, spacy, markdown or the name of a custom split function (see below)
tokenizer_modelcl100k_baseThe tokenizer that measures the chunk size
fieldnoneThe field of each JSON, CSV, TSV or Excel record to split into parts, as source.<name>. See Split one field of a record

chunk_config takes only these five keys. Any other key is refused at load, and the error names the field that it resembles. You can also write each of the first four keys as a separate key on an action, in defaults: or in default_agent_config:. field is read only in the chunk_config of an action or of defaults:.

Settings merge by name, not by block. Thus a block that omits a name keeps the value that a wider level set for that name. The nearest level has priority. In one level, the block has priority over a key beside it:

text
TXT
default_agent_config key < default_agent_config block < defaults: key
< defaults: block < action key < action block

For tiktoken, chars and markdown splits, an overlap at or above the chunk size is refused at load, not at the split. The reason is that merging by name can pair a nearer size with a wider overlap.

The defaults are tuned for the short-document benchmarks of the framework. The examples below show larger values (4000 / 8000), tuned for OpenAI-class context windows. For production workloads on long documents, start with these larger values.

Per-Action Override​

Override the chunking for specific actions:

yaml
YAML
actions:
- name: process_large_docs
chunk_config:
chunk_size: 8000
chunk_overlap: 1000
split_method: chars

Split one field of a record​

A record of a JSON, CSV, TSV or Excel file is staged whole. If one field of a record is so long that the prompt does not fit in the context window of the model, field splits that field into parts. Each part becomes a record of its own:

yaml
YAML
actions:
- name: assess_scope # the first action, which reads the staged records
chunk_config:
field: source.page_content
chunk_size: 200000
chunk_overlap: 0
split_method: chars
  • Each part keeps every other field of the record, such as its url or its id. Its field holds one piece of the text, split by chunk_size, chunk_overlap and split_method as a text file is split.
  • Each part gets two more fields: chunk_index (0 for the first part) and chunk_count (the number of parts of the record). A prompt can read them as source.chunk_index and source.chunk_count.
  • A record whose field fits in one chunk, is empty or is missing becomes one part, with chunk_index: 0 and chunk_count: 1. Thus every record of the file has the same fields.
  • Each part has its own identity, so the actions below process and store each part as a record.
  • The load refuses a field that does not name one top-level field, as source.<name> or <name>. A record whose field is not text, or that already holds a field named chunk_index or chunk_count, stops the file before any record is stored or sent.
  • The split happens where the records are staged, so set it on the first action or in defaults:. The default_agent_config: of the project refuses it, because the field belongs to the records of one workflow.
  • A text file in the same staging folder is chunked by the same chunk_size.

With split_method: chars, a field of 638,545 characters and a chunk_size of 200,000 gives 4 parts.

To let a model read a larger prompt with no split, set context_window.

Split Methods​

tiktoken (Default)​

Token-based chunking with the tokenizer of OpenAI:

yaml
YAML
chunk_config:
chunk_size: 4000
chunk_overlap: 500
split_method: tiktoken

When to use: OpenAI models, precise token control, optimization for context limits.

chars​

Character-based splitting:

yaml
YAML
chunk_config:
chunk_size: 8000
chunk_overlap: 1000
split_method: chars

When to use: Non-OpenAI models, simple text processing, predictable chunk sizes.

spacy​

Semantic chunking with spaCy NLP. It splits at sentence boundaries:

yaml
YAML
chunk_config:
chunk_size: 4000
chunk_overlap: 500
split_method: spacy

When to use: Sentences that must stay complete, natural language content, quality that is more important than speed.

markdown​

Structure-based splitting, measured in tokens. It splits at headings first, then at paragraphs, then at sentences. Each chunk starts with the path of its headings, for example Billing > Duplicate charges. The heading path counts in the chunk size:

yaml
YAML
chunk_config:
chunk_size: 200
chunk_overlap: 0
split_method: markdown

When to use: Markdown, HTML and other documents with headings, when each chunk must say which section it comes from. It is the default of a knowledge base, which does not inherit the chunk_config of the workflow.

Custom split function​

Any other split_method value names a Python module and a function of that name in the tools/ folder (or in the folder that TOOLS_PATH names). The function takes (text, chunk_size, overlap, tokenizer_model) and returns a list of strings.

Overlap​

Overlap makes sure that context is not lost at chunk boundaries. Overlapping sections occur in both adjacent chunks.

Content TypeRecommended Overlap
Technical docs10-20% of chunk size
Narrative text15-25% of chunk size
Code5-10% of chunk size
Structured dataMinimal or none

Examples​

Large Document Processing​

yaml
YAML
# agent_config/{workflow}.yml — chunking belongs in the workflow's defaults block.
# `default_agent_config:` is a key of agent_actions.yml, and a workflow file that
# carries it is refused.
name: chapter_summaries
description: "Summarise a long document in chunks"

defaults:
chunk_config:
chunk_size: 4000
chunk_overlap: 500
split_method: tiktoken

actions:
- name: summarize_chapters
intent: "Summarise each chunk of a long document"
prompt: |
Summarize this section:
{{ source.content }}
schema: chapter_summary
context_scope:
observe: [source.content]

Downstream Aggregation​

When you need to combine the results from all chunks:

yaml
YAML
actions:
- name: process_chunks
granularity: record # Process each chunk

- name: aggregate_results
kind: tool
impl: aggregate_results
granularity: file # Combine all chunks (tool actions only)
dependencies: [process_chunks]

Best Practices​

  1. Match split method to model: Use tiktoken for OpenAI models. Use chars for other models.
  2. Account for prompt size: Leave space in the chunk size for your prompt template.
  3. Test boundaries: Run agac run -w <workflow> --file-limit 1. Then read the records of the first action with agac records -w <workflow> -A <action> to see each chunk.

Disabling Chunking​

To process a document as one piece, set a chunk_size larger than the document. If you omit chunk_config, chunking is not disabled. The defaults above apply.