Chunking strategies and KnowledgeConfig

Why chunk size and overlap make or break RAG quality

Chunks that are too tiny lose context (splitting a rule from its exception); chunks that are too huge dilute embeddings and waste prompt tokens. Tune chunk_size, chunk_overlap, and ChunkingStrategy via KnowledgeConfig.

Example

from praisonai import KnowledgeConfig, ChunkingStrategy

def overlap_chunks(text: str, size: int = 80, overlap: int = 20) -> list[str]:
    chunks, start = [], 0
    while start < len(text):
        chunks.append(text[start:start + size].strip())
        start += size - overlap
    return chunks

cfg = KnowledgeConfig(
    chunking_strategy=ChunkingStrategy.SENTENCE,
    chunk_size=500,
    chunk_overlap=80,
    retrieval_k=4,
)
sample = 'Refunds apply within 30 days for unused items. Digital licenses are final sale unless defective. Enterprise contracts follow custom SLAs.'
parts = overlap_chunks(sample)
print('Strategy:', cfg.chunking_strategy, '| k:', cfg.retrieval_k)
for i, p in enumerate(parts, 1):
    print(f'Chunk {i}:', p)

Try it Yourself

Example explained

  • ChunkingStrategy.SENTENCE — Avoids slicing right through the middle of a sentence or number.
  • chunk_overlap=80 — Carries boundary sentences into adjacent chunks so facts near edges are never lost.
  • retrieval_k=4 — Returns the top 4 most relevant chunks to balance recall and context cost.

Inspect all built-in ChunkingStrategy options

Start with sentence or recursive chunking (400–600 tokens, 10–15% overlap) for docs and switch to semantic chunking for messy transcripts.

Example

from praisonai import ChunkingStrategy
print('Available strategies:', [s.value for s in ChunkingStrategy])

Try it Yourself

Example explained

  • ChunkingStrategy — Lists every chunking mode supported directly inside KnowledgeConfig.

Common pitfall: zero overlap splitting a policy from its exception

If 'Refunds allowed in 30 days' ends Chunk 1 and 'except digital goods' starts Chunk 2, zero overlap makes your agent promise illegal refunds. Keep 10–20% overlap.

Reference: documentation