DEV Community

Cover image for RAG retrieval: how AI finds one line in 100 pages
System Design Simple
System Design Simple

Posted on Originally published at systemdesignsimple.com

RAG retrieval: how AI finds one line in 100 pages

RAG answers from documents in two steps: a retriever ranks chunks of the text against the question, and only the top few go into the model's request. The retriever decides what the model can see. In our synthetic 100-page lab, 167 chunks, about 75,440 estimated tokens, the top 3 chunks averaged about 1,357 tokens, 1/56 of the text.

How BM25 scores a chunk

BM25 is the ranking function behind keyword search in Lucene and Elasticsearch. For each query term a chunk contains, it adds the term's inverse document frequency, so rare terms count more, scaled by a term frequency that saturates and is normalised for chunk length. The usual defaults are k1 = 1.2 and b = 0.75. It matches words, not meaning, and that is exactly where it breaks.

The rewording gap: 29 of 30 vs 4 of 30

In our lab, keyword BM25 put the answer chunk in the top 3 for 29 of 30 same-word questions, 96.7%, but only 4 of 30 reworded ones, 13.3%, the same 30 facts both times. One question shows it plainly: "How early do I have to ask for vacation days?" against a page that says "Annual leave requests" ranked the answer chunk 58th of 167. The words do not overlap, so BM25 has nothing to score.

A synonym TF-IDF retriever did better but not well: 50% recall at 3 overall, 9 of 15 on paraphrases its hand-written synonym list was tuned on, and 6 of 15 on paraphrases written after the list was frozen.

What embeddings, hybrid search and rerankers change

Embeddings reduce the vocabulary problem, because an embedding model learns from data that words like vacation and leave are related. They bring their own weak spots: exact numbers, identifiers, unfamiliar names and negation, and keyword search is often stronger on exact identifiers. That is why many systems run both and fuse the rankings, for example with reciprocal rank fusion. Our lab did not test any embedding model.

A reranker can only reorder what the retriever returned.

Chunk size, overlap and top k

Smaller chunks rank more precisely but split answers across boundaries. Larger chunks keep answers whole but dilute the score and cost more tokens. Overlap softens the split. Top k is the budget: more chunks raise recall but cost tokens and focus, and models use facts buried in very long inputs less reliably.

Our take

Retrieval is the part of RAG that decides the answer, and keyword search fails quietly on reworded questions. Test with questions written in different words from the document, report recall at k, and treat a hand-tuned synonym list as a demo, not a fix. The numbers above come from a seeded Node.js simulation, seed 404, on a synthetic handbook with 30 planted facts. Tokens are estimated as characters divided by 4, retrieval only, and no model generated an answer.

Read the full article with the worked example, the BM25 math and the cheat sheet: https://systemdesignsimple.com/how-ai-finds-one-line-in-100-pages/

Top comments (0)