Sub topic 13
Choosing a strategy and avoiding common pitfalls
This chapter assumes you have read the earlier chapters. You should know what a token is, how embeddings and vector search work, and how fixed-size, recursive, structure-aware, semantic, and parent-child chunking work. Here you will turn those techniques into a decision.
Start simple, then add complexity
Begin with the cheapest strategy that respects your content. Move to a more complex one only when your evaluation set shows a gap. Semantic chunking and parent-child retrieval add cost and moving parts, so each one has to earn its place with a measured gain.
Matching strategy to content
| Content | Good starting point | Reason |
|---|---|---|
| Markdown or HTML docs | Structure-aware, with the heading path as metadata | Headings mark real topic boundaries |
| Articles, reports, plain prose | Recursive splitting, 200 to 400 tokens, 10 to 15% overlap | Paragraph and sentence breaks are respected where possible |
| Chat logs, transcripts | Group by speaker turns or time windows | Single messages are too short to be useful alone |
| Source code | Split by function or class | Fixed windows cut definitions in half |
| Tables, FAQs | One row or one question-answer pair per chunk | Splitting separates a value from its label |
| Long legal or technical text | Small child chunks with larger parents | Search needs precision, and the model needs surrounding clauses |
Common pitfalls
Overlap as a fix for everything. Overlap only helps when a boundary cuts through a sentence. Large overlap creates near-duplicate chunks that fill the top k results with the same text. This leaves less room for different evidence.
Measuring size in characters or words, not tokens. An embedding model truncates input past its limit, and the cut text is never embedded. Always check your chunk size against the model's token limit.
Splitting inside code fences. The Markdown splitter shown earlier treats a # comment inside a fenced block as a heading. Track whether you are inside a fence before you match headings.
Chunks that make no sense alone. A chunk reading "It increased by 12% the following year" cannot be retrieved for the right question. Attach the document title and heading path, or prepend them to the text before embedding.
Tuning by eye. A few good-looking answers are not evidence. Change one setting at a time and compare recall@k on the same questions.
Forgetting to re-index. Changing your chunker changes every chunk ID and embedding. Re-embed the whole corpus. Mixing old and new chunks in one index makes results impossible to compare.
A quick sanity check before you index
Run a size report on your chunks before embedding them. Many chunking bugs show up as outliers.
def chunk_report(chunks):
sizes = sorted(len(c.split()) for c in chunks)
n = len(sizes)
return {
"count": n,
"min": sizes[0],
"median": sizes[n // 2],
"max": sizes[-1],
"tiny": sum(1 for s in sizes if s < 20),
}
chunks = ["Short.", "A longer chunk " * 30, "Another chunk of moderate length " * 5]
print(chunk_report(chunks))Many tiny chunks suggest a separator problem, such as splitting on every newline in a document with hard-wrapped lines. A maximum far above your target suggests a piece that never matched any separator.
A practical checklist
- Pick the strategy from the table that fits your content.
- Write 20 to 50 real questions, each with the chunk or document that answers it.
- Measure recall@5 for two or three sizes and overlaps.
- Read the failures. Check whether the answer was split, buried, or stripped of context.
- Add metadata, parent-child retrieval, or semantic splitting only if the failures point to it.
- Keep the evaluation set. Re-run it whenever the corpus, chunker, or embedding model changes.
Exercise
Your recall@5 is low, and reading the failures shows the right chunk is retrieved but says things like "the limit above" without naming what the limit applies to. Which change should you try first, and why?
Show answer
Add context to the chunks, such as the document title and heading path, before embedding. The retrieval is already finding the right area, so the missing information is the context inside each chunk, not the boundaries. Changing the chunk size or adding semantic splitting would not supply that context. Parent-child retrieval is a second option if the surrounding text is needed at generation time.
Exercise
A teammate raises overlap from 10% to 50% and says it will fix missed answers. What risk does this create?
Show answer
Half of each chunk repeats its neighbor, so the index grows and the top k results are often near-duplicates of one passage. That crowds out other relevant chunks and can lower recall@k. Measure it on the evaluation set before accepting the change.