Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 30/ 160
02. RAG

Reranking

Cross-encoders and LLM judges that reorder top-k candidates for higher precision.

IntermediateCross-Encoder · Top-k Reorder3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

A fast search finds a pile of maybe-relevant passages, and a slower, smarter judge reads each one next to the question and puts the best ones on top.

Think of it like…

A recruiting funnel: a quick keyword screen shortlists 50 CVs from thousands (fast, rough), then a senior interviewer reads each shortlisted CV against the job description (slow, careful) and picks the best five.

Where you’ve seen it

A contract assistant's hybrid retrieval returns the correct clause within the top 50 for 97 percent of questions but within the top 5 only 71 percent of the time. Adding a cross-encoder reranker over the top 50 raises top-5 hit rate to 90 percent and answer accuracy from 74 to 84 percent. Latency rises by 180 ms on a GPU-hosted model, which fits their 2-second budget; they cap candidate text at 512 tokens each to control cost.

Words you’ll hear

Bi-encoder
Embeds query and document separately; fast, precomputed, approximate.
Cross-encoder
Scores (query, document) jointly; more accurate, can't be precomputed.
Candidate depth
How many first-stage results are passed to the reranker (e.g. 30–100).
Listwise reranking
An LLM orders a whole list of candidates in one prompt.

Visual diagram

Loading interactive diagram

Theory & deep dive

Why cross-encoders are more accurate

A bi-encoder compresses each document into a single vector before any query exists, so subtle relations between the query and specific words in the document are lost. A cross-encoder concatenates query and document and runs full self-attention over both, so every query token can attend to every document token: it can tell that "termination for convenience" in the query matches a clause about ending the contract without cause, or that a passage mentions the right term but negates it. The price: one forward pass per candidate per query, nothing precomputed, so latency and cost scale with candidate count.

Options: hosted rerank APIs (Cohere Rerank, Voyage rerank, Jina and similar), open models you host (the BGE-reranker family, mixedbread, Qwen-based rerankers, ms-marco cross-encoders), and LLM-based reranking (pointwise: score each candidate; listwise: order a list in one prompt), which is flexible and strong but slower and costlier. Late-interaction models such as ColBERT sit between bi- and cross-encoders, keeping token-level vectors for richer matching with precomputation.

A reranking call

pairs = [(query, chunk.text) for chunk in candidates]; scores = reranker.predict(pairs); top = [c for _, c in sorted(zip(scores, candidates), reverse=True)][:6] sends 50 pairs and keeps the six best.

Key takeaway: Cross-encoders read query and passage together: accurate, per-candidate cost.

Design choices and evaluation

Pick candidate depth from recall curves: measure first-stage recall@k for k = 10, 30, 50, 100 and choose the smallest k where recall plateaus near your target (often 30 to 100). Reranking more candidates costs latency linearly. Limit each candidate to what the model handles (often 512 tokens), batch requests, run on GPU or use a hosted API with parallelism, and set a timeout with fallback to the first-stage order. Choose the final cut-off (top 5 to 10) by answer quality and token cost; some systems also drop candidates below a score threshold, which needs calibration per model.

Evaluate with labelled queries: compare nDCG@10 and recall@5 with and without reranking, then end-to-end answer accuracy and citation correctness. Check for failure modes: long-document truncation cutting the answer, domain mismatch (a general reranker underperforming on legal or code text, which fine-tuning or a domain model can fix), position bias in LLM rerankers, and added latency at the tail (p95 rather than average). Rerankers can also enforce business logic such as freshness or authority boosts after relevance scoring.

Choosing candidate depth

First-stage recall: @10 = 78%, @30 = 93%, @50 = 97%, @100 = 98%. Depth 50 captures nearly all the gain; depth 100 doubles reranking cost for one more point.

Key takeaway: Choose depth from recall curves, truncate, set timeouts, and measure end to end.

How it works, step by step

Step 1 of 4
Evaluation

1. Measure first-stage recall by depth

Compute recall@k for several depths to see how many candidates the reranker needs.

@10 78% · @30 93% · @50 97%
Key numbers at this step
Choose
knee of the curve

When to use it & trade-offs

What it is

Reranking is a second retrieval stage that reorders a short candidate list (say the top 50 from hybrid search) with a more accurate but slower model, then keeps the best 5 to 10. A cross-encoder reads the query and each candidate together in one forward pass and outputs a relevance score, which lets it model fine-grained interactions that a bi-encoder (separate query and document embeddings) can't. LLM-based rerankers (pointwise or listwise) do the same with a general model. First stage buys recall; reranking buys precision.

Why it matters: Fast retrieval is approximate: the right passage is often in the top 50 but not the top 5, and the LLM can only use a handful of chunks (and is distracted by irrelevant ones). A reranker is one of the highest-return additions to a RAG pipeline, typically lifting answer accuracy several points for tens to a couple of hundred milliseconds of latency. Choosing the candidate depth, model and cut-off is an important design decision.

When to reach for it

  • RAG pipelines where the answer is in the top 50 but not the top 5
  • Reducing prompt tokens by sending fewer, better chunks
  • Applying business ranking rules after relevance scoring

When to avoid or be careful

  • Reranking thousands of candidates per query
  • Passing full long documents to the reranker without truncation
  • No fallback when the reranker times out

Reranker options

Hosted rerank API
Best for: Fast adoption and variable traffic

Strengths: No GPUs; strong quality; simple integration.

Watch out: Per-request cost; data sent to a vendor; added network latency.

Self-hosted open cross-encoder
Best for: Privacy, volume and domain fine-tuning

Strengths: Fixed cost; tunable; data stays in your environment.

Watch out: You run GPU inference.

LLM reranker (pointwise or listwise)
Best for: Complex relevance criteria and low volume

Strengths: Flexible, instruction-following.

Watch out: Slowest and costliest; position bias; non-determinism.

Interview signals & numbers to know

When this topic comes up

  • “The right passage is retrieved but ranked low”
  • “Bi-encoder vs cross-encoder”
  • “LLM answers are distracted by irrelevant chunks”
  • Two-stage retrieval
  • nDCG improvements

Benchmarks & rules of thumb

30 – 100 -
typical candidates to rerank
5 – 10 -
chunks usually kept for the LLM
100 – 300 ms -
typical added latency

Common mistakes & follow-ups

Common interview mistakes

  • Reranking only the top 10 results, which can't recover answers ranked 15th.
  • Using a reranker trained on web search for code or legal text without testing.
  • Ignoring p95 latency and timing out under load.

Likely interviewer follow-ups

  • Why is a cross-encoder more accurate than a bi-encoder?
  • How do you choose how many candidates to rerank?
  • When would you use an LLM as a reranker?
Interview tip: Describe the two-stage idea in one sentence (recall then precision) and back it with first-stage recall@k numbers. It shows you design from data, not habit.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Reranking with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Hybrid Search
Next topic
Query Rewriting