RAG & LLMs / 6. CROSS-ENCODER RERANKING
Stage 6: Cross-Encoder Reranking
The accuracy booster — deep pairwise (query, chunk) scoring
EXPLANATION
Retrieval gives you top-10 chunks fast but approximately. Cross-encoder reranking scores each (query, chunk) pair through a full BERT encoder together — so the model sees how query and chunk relate to each other at every attention layer. This is far more accurate than cosine similarity but expensive (can't cache vectors). That's why you do two stages: 1. Bi-encoder retrieves top-10 (fast, approximate) 2. Cross-encoder re-scores all 10 (accurate, exact) 3. Keep top-3, discard the rest 4. Pass those 3 to the LLM This keeps context small and quality high.
DATA FLOW
10 chunks from hybrid retrieval
↓
Cross-Encoder: [CLS] query [SEP] chunk [SEP] → BERT → score
chunk_1 score: 0.95 ██████████ ← highly relevant
chunk_3 score: 0.89 █████████ ← very relevant
chunk_4 score: 0.78 ████████ ← relevant
chunk_2 score: 0.12 █ ← not relevant
chunk_5 score: 0.03 ░ ← irrelevant
↓
Pass top-3 to LLM (smaller context = better answer)CODE