> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-agui.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Chunking

Custom chunking allows you to implement your own chunking strategy by creating a class that inherits from `ChunkingStrategy`. This is useful when you need to split documents based on specific separators, apply custom logic, or handle domain-specific content formats.

<Steps>
  <Step title="Create a Python file">
    ```python custom_chunking.py theme={null}
    from typing import List
    import asyncio
    from agno.agent import Agent
    from agno.knowledge.chunking.base import ChunkingStrategy
    from agno.knowledge.content import Document
    from agno.knowledge.knowledge import Knowledge
    from agno.knowledge.reader.pdf_reader import PDFReader
    from agno.vectordb.pgvector import PgVector

    class CustomChunking(ChunkingStrategy):
        def __init__(self, separator: str = "---", **kwargs):
            self.separator = separator

        def chunk(self, document: Document) -> List[Document]:
            # Split by custom separator
            chunks = document.content.split(self.separator)

            result = []
            for i, chunk_content in enumerate(chunks):
                chunk_content = self.clean_text(chunk_content)  # Use inherited method
                if chunk_content:
                    meta_data = document.meta_data.copy()
                    meta_data["chunk"] = i + 1
                    result.append(Document(
                        id=f"{document.id}_{i+1}" if document.id else None,
                        name=document.name,
                        meta_data=meta_data,
                        content=chunk_content
                    ))
            return result

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

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

    asyncio.run(knowledge.ainsert(
        url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
        reader=PDFReader(
            name="Custom Chunking Reader",
            chunking_strategy=CustomChunking(separator="---"),
        ),
    ))

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

    agent.print_response("How to make Thai curry?", markdown=True)
    ```
  </Step>

  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno sqlalchemy psycopg pgvector
    ```
  </Step>

  <Snippet file="run-pgvector-step.mdx" />

  <Step title="Run the script">
    ```bash theme={null}
    python custom_chunking.py
    ```
  </Step>
</Steps>

## Custom Chunking Params

<Snippet file="chunking-custom.mdx" />
