
RAG from Basics to Advanced (Part 3): What Actually Happens Inside Retrieval
Bi-encoders vs. cross-encoders, hybrid search, and why chunk order matters the practical retrieval decisions behind a production RAG system.
Series: RAG from Basics to Advanced (8 articles)
- …
- 3RAG from Basics to Advanced (Part 3): What Actually Happens Inside Retrieval This article
This is Part 3 of a series that builds up Retrieval Augmented Generation (RAG) from the ground up.
In Part 2, we walked through the core RAG pipeline: embed the query, retrieve the top-K chunks, build a prompt, generate an answer. That's the right mental model to start with but it glosses over a detail that matters a lot in a real system: the retrieval step is rarely just "search and return."
This part fills in that gap. It's written at the level an AI engineer actually needs the decisions you make when building a production RAG system, and why you'd make them rather than the internals of the algorithms underneath. If you want the graph-traversal-level mechanics of ANN indexes, that's a different, more infra-focused conversation; this is about what to do and when.
Why Retrieval Isn't Just "Search and Return"
Here's the tension: fast retrieval and accurate retrieval pull in opposite directions.
A vector database can search millions of embeddings in milliseconds because it uses Approximate Nearest Neighbor (ANN) search algorithms like HNSW or IVF that trade a small amount of accuracy for a massive speed gain. That's the right trade-off for scanning a huge corpus. But "approximate" is the operative word the top result from an ANN search is a good guess, not a guarantee.
So production systems don't stop at one retrieval pass. They retrieve broadly and cheaply first, then spend extra effort narrowing that set down precisely. That's the whole idea behind the next few sections.
Bi-Encoders vs. Cross-Encoders
This distinction is one of the most practical things to understand about retrieval, and it's a genuinely common interview question.
Bi-encoder this is what powers the embedding-based retrieval from Part 2. The query and each document are encoded independently, into separate vectors, and similarity is computed afterward (cosine or dot product).
- Fast- documents can be embedded and indexed ahead of time; at query time, you only need to embed the query.
- Scales to millions of documents.
- Less precise- because the query and document never "see" each other during encoding, subtle relevance signals can be missed.
Cross-encoder the query and a candidate document are fed together into the same model in one pass, so the model can directly attend to both at once.
- Much more accurate it's directly modeling the relationship between the two texts.
- Much slower there's no pre-computation possible. Running this against millions of documents per query isn't feasible.
Why this matters practically: neither approach alone is good enough. Bi-encoders scale but aren't precise enough on their own; cross-encoders are precise but don't scale. So production retrieval is typically a two-stage pipeline:
Stage 1 (Bi-encoder): retrieve a broad candidate set — e.g., top 50–100 — cheaplyStage 2 (Cross-encoder): rerank just that candidate set, keep the top 5–10This is the "reranking" step you'll see mentioned in most production RAG setups (Cohere Rerank, or open-source cross-encoders from
sentence-transformers are common choices). You're not trying to search the whole corpus precisely. you're trying to search it cheaply, then get precise on a small shortlist.The one-line version: bi-encoders cast a wide net cheaply; cross-encoders make that net precise. You need both stages because neither one does the whole job well by itself.
Hybrid Search: Combining Keyword and Semantic Retrieval
Part 2 touched on this briefly pure semantic search can miss exact identifiers (product codes, specific names, error codes) that a plain keyword match would catch instantly. That's not a rare edge case; it's a recurring, real production issue.
The fix is hybrid search: run both a semantic (vector) search and a traditional keyword search (BM25-style) in parallel, then merge the two ranked lists into one.
The tricky part is that a cosine similarity score and a BM25 score live on completely different scales, so you can't just add them together. A common way to merge them is Reciprocal Rank Fusion (RRF) instead of combining the raw scores, it combines the ranks each result got in its own list. A document that ranks well in either list (or both) ends up ranked well overall, without needing to reconcile mismatched scoring scales.
You don't need to hand-derive the formula to use this effectively most vector databases and search platforms (OpenSearch, Weaviate, Elasticsearch) offer hybrid search with fusion built in. What matters is knowing why you'd turn it on: whenever your queries are likely to include exact terms names, codes, IDs alongside natural language.
Why Chunk Order in the Prompt Matters
This one is easy to overlook and easy to fix once you know about it.
LLMs don't treat every part of a long prompt equally well. In practice, models tend to pay more attention to content near the start and end of the context than to content buried in the middle a pattern often called "lost in the middle." You don't need the attention-mechanism explanation for why this happens to act on it; what matters is the practical implication: if your most relevant chunk lands in the middle of the prompt, the model may under-use it, even though retrieval did its job correctly.
The fix is straightforward: after reranking, arrange the chunks so the most relevant ones sit at the edges of the context (start and end), not buried in between.
Optimized order:[Most relevant] → [Least relevant] → [Second most relevant]It's a small step, but it's cheap to implement and directly affects answer quality worth doing by default once you're reranking anyway.
Putting It Together: The Extended Flow
Here's the flow from Part 2, with these retrieval-stage details filled in:

Summary
None of this changes the core idea from Part 2 retrieval still feeds generation. What changes is the honest picture of what "retrieval" involves in a system that actually needs to be accurate at scale: a fast first pass to narrow the field, a slower precise pass to rerank it, an optional fusion step for exact-term matches, and a final reordering step before the prompt ever reaches the LLM.
The practical takeaway:
A single embedding search is a reasonable starting point for a prototype. A production RAG system almost always needs a second pass reranking, hybrid fusion, or both before the results are good enough to hand to the LLM.
Next in this series (Part 4): Chunking strategies. How documents get split before indexing, why chunk size and overlap change retrieval quality, and the trade-offs between fixed-size, semantic, and recursive chunking.
Series: RAG from Basics to Advanced (8 articles)
- …
- 3RAG from Basics to Advanced (Part 3): What Actually Happens Inside Retrieval This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article