Skip to content
mentoor

Topic 2 of 5 · 4 min read

Second-stage rerankers and cross-encoders

Goal: Add a reranking stage to a pipeline with a chosen candidate count and final k, and justify both numbers against latency.

Retrieve wide with a fast, cheap search, then let a slower and more accurate reranker pick the few chunks that go into the prompt.

Check yourself · 1 of 3

Why is a cross-encoder reranker run on 50 candidates and not on every chunk in the index?

Choose one answer

Why it matters

First-stage retrieval compares a query and a chunk through two separate vectors, made independently. That is fast enough to search millions of chunks, but it is a rough match. The right chunk is often in the top 50 and not in the top 5. A reranker reads the query and one candidate together and scores how well that chunk answers that query. A cross-encoder (a model that takes query and passage as one input) is the classic design, and it is too slow to run over a whole corpus. So the pattern is two stages: cast a wide net, then re-score a small catch.

How to do it

  1. Choose the candidate count N the first stage returns, for example 50 or 100.
  2. Send the query and those N chunks to a reranker: an open cross-encoder you host, a hosted rerank API such as Cohere Rerank, or an LLM prompted to score relevance.
  3. Keep the top k, usually 3 to 10, and put only those in the prompt.
  4. Pick N and k from data: raise N until recall of the right chunk stops improving, and lower k until answers get worse.
candidates = hybrid_search(query, n=50)            # fast, wide
scored = reranker.rerank(
    query=query,
    documents=[c.text for c in candidates],
    top_n=5,                                       # narrow
)
context = [candidates[r.index] for r in scored]

Latency and cost

Reranking work grows with N: every candidate is a model call or a slot in a batch. Doubling N roughly doubles reranker time and, for a hosted API, the bill. Measure p50 and p95 latency with your real N, because a reranker that adds 200 ms in a notebook can add far more under load. An LLM as reranker is the most flexible and the slowest. Cap the total: set a timeout, and fall back to the first-stage order if the reranker fails.

Before and after

Query: "can contractors access the staging database?". Hybrid retrieval returns, at ranks 1 to 3, three chunks about the staging environment in general and, at rank 14, the access-policy chunk that answers the question. With N = 50 and k = 5, the reranker moves the policy chunk to rank 1, and the answer cites it.

What goes wrong

  • Reranking only the top 5. If the right chunk was rank 14, the reranker never sees it. N must be wide.
  • Setting N far too high, which adds latency for candidates that never make the top k.
  • Truncated passages: rerankers have an input limit, and a long chunk cut off at the front loses the sentence that matters.
  • No fallback when the rerank service times out, so a slow dependency becomes an outage.

Examples

Choosing N and k from a sweep

A team tests 100 questions with known answer chunks.

  • N = 10: the answer chunk is inside in 71 cases.
  • N = 30: 88 cases.
  • N = 50: 93 cases.
  • N = 100: 94 cases, with double the rerank time.

They pick N = 50. The gain from 50 to 100 is one case and does not pay for the latency.

Reranker with a fallback

try:
    ranked = reranker.rerank(query, texts, top_n=5, timeout=1.5)
except (TimeoutError, ServiceError):
    ranked = first_stage[:5]   # degrade, do not fail

A slow rerank service now costs some answer quality, not the whole request.

Key points

  • A reranker reads the query and one chunk together, so it scores relevance more accurately than comparing two separate vectors.
  • Rerankers are too slow for a whole corpus, so they work on the candidates a fast first stage returns.
  • Choose the candidate count N so the right chunk is usually inside it, and the final k to fit the prompt.
  • Reranking cost and latency grow with N, so measure p50 and p95 with your real N.
  • Always set a timeout and a fallback to the first-stage order.

Common mistakes

  • Passing only the first-stage top 5 to the reranker, so it cannot rescue a chunk ranked 14.
  • Picking N and k by habit instead of sweeping them on a query set.
  • Ignoring the reranker's input length limit and silently truncating long chunks.
  • Calling the rerank service with no timeout and no fallback.

Check yourself · 2 of 3

The answer chunk usually sits around rank 15 in first-stage results. Which setting will let a reranker fix that?

Choose one answer

Check yourself · 3 of 3

Your rerank API call sometimes takes 6 seconds. What is the best protection for the user-facing request?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic