Handbooks / RAG & AI Agents / Chapter 4
Chunking and Retrieval
7 pages · ~5 min✓ Reviewed
Builds on Embeddings and Vector Similarity. Next up: Tool Calling.
RAG & AI AgentsPage 1Retrieval Mental Model
1. What retrieval is
- Retrieval picks the top-k chunks from an index for the prompt
- Right chunk missing → answer cannot be grounded, even with a great LLM
2. Pipeline and two stages
- Rewritequery
- Filtermetadata
- Retrievedense + keyword
- Re-rankcandidates
- Top fewchunks
- Generateanswer
RAG & AI AgentsPage 2Chunk Size and Overlap
1. Size and overlap
- Chunk size = tokens per embedding
- Overlap = share of previous chunk repeated at the next chunk's start
- Start: 256-512 tokens, 10-20% overlap
- Tune on recall@k with your own queries
2. Overlap
- Stops a fact being split across a boundary
- Size 500, overlap 50: chunk 2 starts at token 450
- Cost: bigger index, near-duplicate hits
3. Split order
- Headingsfirst
- Paragraphs, code blocksnext
- Fixed token windowfallback
RAG & AI AgentsPage 3Metadata Filtering and Hybrid Search
1. Filtering: pre vs post
- Metadata filter limits candidates by source, date, tenant_id, doc type, language
- Fields must be stored at ingest
2. Hybrid search and RRF
- Query
- BM25 + densein parallel
- Merge ranksRRF
- Top-k
- Use hybrid when queries hold IDs, acronyms, error codes
- RRF score = sum of
1/(k+rank) over lists, k ≈ 60 - RRF needs no score normalisation
- Weighted sum needs normalised scores
3. Mistakes
RAG & AI AgentsPage 4Re-ranking and Query Rewriting
1. Re-ranking: Two Stages
- Retrieveabout 50 candidates
- Re-rankcross-encoder rescores
- Keeptop 3-5
- A re-ranker rescores the candidates and reorders them
- It only reorders stage 1 results: a missing answer stays missing
- Fix recall first, then precision
RAG & AI AgentsPage 5Measuring Retrieval: recall@k and MRR
1. Labelled set
- (query, relevant IDs) pairs, ideally 50-200 real queries
- No set = tuning is guesswork
3. Tuning loop
- Label set50-200 queries
- Baselinerecall@k, MRR
- Change ONE thingchunk size, hybrid
- Re-measurekeep if better
RAG & AI AgentsPage 6Retrieval Tuning Cheat Sheet
1. Chunk and Filter
- 256-512 tokens, 10-20% overlap
- Split on structure first, then token windows
- Pick the size by recall@k, not habit
- Pre-filter on metadata, always
tenant_id
2. Hybrid, Re-rank, Rewrite
- BM25 + densemerge with RRF
- Top ~50bi-encoder
- Re-scorecross-encoder
- Keep 3-5
pythonscore = sum(1 / (60 + rank) for rank in ranks)
RRF: ranks of one chunk across lists
- Re-ranker cannot recover a chunk that was never retrieved
- Rewrites: standalone follow-up, expansion, HyDE, multi-query
- Keep a rewrite only if the metrics improve
RAG & AI AgentsPage 7Check yourself
A search takes the top 5 chunks, then filters on tenant_id = acme. Only 1 of the 5 belongs to acme. What comes back, and what is the fix?
- Just 1 chunk, fewer than k, because filtering ran after the top-k cut.
- Pre-filter inside the ANN search so all 5 come from acme.
RRF with k=60. Doc A is rank 1 in BM25 and rank 3 in dense. Doc B is rank 2 in both. Which wins?
- A: 1/61 + 1/63 ≈ 0.03227.
- B: 1/62 + 1/62 ≈ 0.03226.
- A wins, narrowly.
The right chunk sits at rank 8 in stage 1. You re-rank only the top 5. Why does the answer stay wrong?
- A re-ranker only reorders what stage 1 returned.
- The chunk never reached it. Retrieve about 50 candidates, then re-rank.