Chunking Strategies (Fixed, Sentence, Semantic)
Trade-offs between fixed-size, sentence-aware, and semantic chunking for retrieval quality.
Chunking Strategies (Fixed, Sentence, Semantic) is a free AI Agents lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Chunk?
You cannot embed an entire 200-page PDF as one vector — the vector would be too abstract to match specific queries. You chunk the document into smaller pieces (each ~200-800 tokens), embed each, and search at chunk level.
Fixed-Size Chunking
Simplest approach — split every N characters or tokens:
def fixed_chunks(text, chunk_size=1000, overlap=200):
chunks = []
i = 0
while i < len(text):
chunks.append(text[i:i + chunk_size])
i += chunk_size - overlap
return chunks
sample_text = "A" * 2500
chunks = fixed_chunks(sample_text, chunk_size=1000, overlap=200)
print(f"Split {len(sample_text)} characters into {len(chunks)} chunks")
for i, c in enumerate(chunks):
print(f"Chunk {i+1}: {len(c)} chars")
Why Overlap?
Overlap (typically 10-20% of chunk size) ensures a sentence that straddles the boundary appears intact in at least one chunk. Without it, a key fact split across chunks may not be retrievable.
Sentence-Based Chunking
Split on sentence boundaries — never break mid-sentence:
import re
def sentence_chunks(text, max_chars=1000):
sentences = re.split(r'(?<=[.!?])\s+', text)
chunks = []
current = ''
for s in sentences:
if len(current) + len(s) > max_chars and current:
chunks.append(current.strip())
current = s
else:
current += ' ' + s
if current:
chunks.append(current.strip())
return chunks
sample_text = "This is sentence one. This is sentence two! Is this sentence three? Yes it is."
chunks = sentence_chunks(sample_text, max_chars=40)
for i, c in enumerate(chunks):
print(f"Chunk {i+1}: {c!r}")
Recursive Splitting (LangChain)
LangChain's RecursiveCharacterTextSplitter tries to split on paragraph boundaries first, then sentences, then words — preserving structure as much as possible.
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(
chunk_size=800,
chunk_overlap=100,
separators=['\n\n', '\n', '. ', ' ']
)
chunks = splitter.split_text(document)Semantic Chunking
Split where the topic changes. Implementations embed each sentence and split where consecutive sentence embeddings are far apart.
Slower to compute but produces topically-coherent chunks.
Markdown-Aware Chunking
For Markdown docs, split on header boundaries to keep sections together. Each chunk inherits its parent headings as context.
Code-Aware Chunking
For source code, split on function/class boundaries, not character count. Tools like tree-sitter give you AST-aware chunking.
Choosing Chunk Size
Trade-offs:
- Small chunks (~200 tokens) — precise matches, more chunks to manage
- Large chunks (~1000 tokens) — more context per match, less precise
Default: 500-800 tokens with 10-20% overlap.
Metadata Preservation
Attach metadata to every chunk:
- Source URL / file path
- Page number
- Document title
- Section / heading
- Created / updated timestamps
Useful for filtering, citations, and re-ranking.
Contextual Chunks
Recent technique: prefix each chunk with a 1-line context generated by an LLM (e.g. "This chunk is from the company financial report Q2 2024, discussing revenue."). Improves retrieval by 30-50%.
Parent-Child Chunks
Index small chunks for precise matching, but retrieve their LARGER parent paragraph for context:
- Embed sentence-level chunks
- On match, return the parent 800-token chunk
Why Overlap?
Why do chunks typically overlap by 10-20%?
Recap
Start with recursive character splitting at ~800 tokens with 10% overlap. Add semantic / structural awareness as your needs grow.
Frequently asked questions
Is the “Chunking Strategies (Fixed, Sentence, Semantic)” lesson free?
Yes — the full text of “Chunking Strategies (Fixed, Sentence, Semantic)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Chunking Strategies (Fixed, Sentence, Semantic)”?
Trade-offs between fixed-size, sentence-aware, and semantic chunking for retrieval quality. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Chunking Strategies (Fixed, Sentence, Semantic)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What RAG Solves (Knowledge Cut-off, Hallucinations)
- Chunking Strategies (Fixed, Sentence, Semantic)
- Indexing a Document Set
- Building a Naive RAG with FAISS or Chroma