Multi-Stage Retrieval & Cross-Encoder Reranking
Passing the top-5 raw vector results directly to the LLM floods the context with noisy, loosely-relevant chunks that dilute precision. This article implements a two-stage retrieval pipeline: fast bi-encoder candidate generation in PostgreSQL followed by Cross-Encoder reranking to achieve 35%+ higher top-3 relevance without violating P99 latency budgets.
Full-Stack AI Architecture: From Streaming Foundations to Resilient Agentic Systems in Node.js & React
Multi-Stage Retrieval & Cross-Encoder Reranking
The top 10 results from hybrid RRF retrieval contain the right answer — but not necessarily at position 1. Bi-encoders (the embedding models used in Parts 8–10) process query and document independently, producing separate vectors that are compared by geometry. This is fast but sacrifices semantic precision: subtle relevance distinctions between "configure rate limiting" and "configure rate limiting for WebSocket connections" require joint attention over both texts simultaneously.
This article implements the two-stage funnel that production RAG systems use: fast bi-encoder hybrid search generates 50 candidates (Part 10), then a Cross-Encoder reranker computes full attention between the query and each candidate, selecting the 5 most precisely relevant results — improving top-3 precision by 35%+ without violating the 300ms P99 latency budget.
Who this is for: Engineers who have high recall (the right answer is in the top 20) but low precision (the right answer isn't in the top 5 passed to the LLM). Cross-encoder reranking is the targeted fix for this exact failure mode.
1. Bi-Encoder vs Cross-Encoder: The Precision Trade-off
| Property | Bi-Encoder | Cross-Encoder |
|---|---|---|
| Processing | Query and doc independently | Query + doc concatenated |
| Indexable | ✅ Yes — ANN index possible | ❌ No — must score each pair |
| Precision | Moderate — geometric similarity | High — joint attention |
| Latency | Sub-millisecond (index lookup) | 5–50ms per candidate |
| Use in pipeline | Stage 1: Generate candidates | Stage 2: Rerank candidates |
2. The Two-Stage Funnel Design
The numbers are deliberate:
- 50 candidates from hybrid search: high recall — the answer is almost certainly here
- 5 final results from reranker: high precision — only the most relevant pass to the LLM
- 5 × 400 tokens = 2,000 tokens of context: fits within the RAG budget from Part 14
3. FlashRank: Zero-API-Cost ONNX Reranking in Node.js
FlashRank bundles a Cross-Encoder model as a quantised ONNX runtime. It runs in-process inside Node.js — no HTTP calls, no API keys, no cost per rerank.
FlashRank's ONNX model takes 800ms–2s to load on cold start. In serverless deployments (Vercel Functions, AWS Lambda), each cold instance pays this penalty on the first reranking request. Configure minimum instance counts or provisioned concurrency to keep instances warm. In long-lived Node.js servers (ECS, Kubernetes), the model loads once at process startup and costs zero latency on subsequent requests.
4. Integrating into the Retrieval Pipeline
Load the ONNX reranker model during application startup (before the first request arrives), not lazily on first use. ONNX model loading takes 2–5 seconds — loading on first request adds that delay to the first user's P99 latency.
Summary
| Stage | Mechanism | Output |
|---|---|---|
| Hybrid RRF (Stage 1) | Dense ANN + BM25 fused by Reciprocal Rank Fusion | 50 high-recall candidates |
| Cross-Encoder (Stage 2) | Full attention between query + each candidate | 5 high-precision results |
| Timeout guard | Promise.race(reranker, timeout(150ms)) |
Falls back to RRF order if overloaded |
| ONNX in-process | Zero API cost, zero network hop, deterministic | Consistent sub-100ms on modern CPU |
What's Next
Part 12 addresses the root failure of isolated chunk retrieval: chunks without their document context are ambiguous. We implement Anthropic's Contextual Retrieval (prompt-cached chunk prefixing) and the Parent-Document Retriever pattern, cutting retrieval failure rates by over 40%.
This article was developed with AI-assisted deep search, specification cross-referencing, and technical research synthesis.