Agentic Chunking

Split documents with AgenticChunking, which uses a model to find natural breakpoints.

AgenticChunking asks a model to choose each split position within max_chunk_size characters. It uses the size limit when the model call fails or the response cannot be parsed as an integer. Positions above the limit are clamped.

Create a Python file

from agno.agent import Agent
from agno.knowledge.chunking.agentic import AgenticChunking
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reader.pdf_reader import PDFReader
from agno.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge = Knowledge(
    vector_db=PgVector(table_name="recipes_agentic_chunking", db_url=db_url),
)

knowledge.insert(
    url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
    reader=PDFReader(
        name="Agentic Chunking Reader",
        chunking_strategy=AgenticChunking(),
    ),
)

agent = Agent(
    knowledge=knowledge,
    search_knowledge=True,
)

agent.print_response("How do I make Thai curry?", markdown=True)

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai pypdf sqlalchemy psycopg pgvector

Export your OpenAI API key

Set OpenAI Key

Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.

export OPENAI_API_KEY=sk-***

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18

Run the script

python agentic_chunking.py

Custom Prompts

from agno.knowledge.chunking.agentic import AgenticChunking

AgenticChunking(
    custom_prompt="Split at major section boundaries. Keep complete clauses together.",
    max_chunk_size=3000,
)

custom_prompt is inserted into Agno's chunking instructions. For the chunker to make progress, the model must return a positive integer.

Set max_chunk_size explicitly when using a custom prompt so the model receives the intended limit.

Agentic Chunking Params

ParameterTypeDefaultDescription
modelOptional[Union[Model, str]]NoneThe model to use for chunking. Accepts a Model instance or a model ID string. Defaults to OpenAIChat with gpt-5.4-mini when not set.
max_chunk_sizeOptional[int]NoneThe maximum size of each chunk. Defaults to 5000 characters when not set.
custom_promptOptional[str]NonePersonalized instructions for determining chunk breakpoints.

Developer Resources