Fixed Size Chunking

Split text into character-limited chunks with optional overlap.

FixedSizeChunking creates chunks up to chunk_size characters and avoids splitting a word when a boundary is available.

Create a Python file

from agno.agent import Agent
from agno.knowledge.chunking.fixed import FixedSizeChunking
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_fixed_size_chunking", db_url=db_url),
)

knowledge.insert(
    url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
    reader=PDFReader(
        name="Fixed Size Chunking Reader",
        split_on_pages=False,
        chunking_strategy=FixedSizeChunking(),
    ),
)

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 sqlalchemy psycopg pgvector pypdf openai

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 fixed_size_chunking.py

The example sets split_on_pages=False so PDFReader combines the pages before applying FixedSizeChunking. Keep the default value of True to chunk each page independently.

Fixed Size Chunking Params

ParameterTypeDefaultDescription
chunk_sizeint5000Maximum characters per chunk. Use a positive value.
overlapint0Characters repeated between adjacent chunks. Use 0 <= overlap < chunk_size.

Developer Resources