Sub topic 7
How many candidates to rerank
This chapter assumes you can run the retrieve-then-rerank pipeline from the previous chapters. It also assumes you know recall@k, MRR, and nDCG. The question here is how many first-stage results (the candidate depth, written k_retrieve) to pass to the cross-encoder.
Why candidate depth matters
A cross-encoder can only reorder what the first stage gives it. If the relevant passage is not in the candidate pool, reranking cannot recover it. Candidate depth therefore sets two limits:
- Quality ceiling. Final recall can never exceed the first stage's recall@
k_retrieve. - Cost floor. Reranking cost grows roughly linearly with
k_retrieve, because each candidate needs one full transformer forward pass over the (query, passage) pair.
A small depth is cheap but risks cutting off relevant passages. A large depth raises the ceiling but costs more latency and compute. It can also let in more near-miss passages that distract the reranker. You are looking for the smallest depth that comes close to the ceiling you can reach.
Two different numbers
Keep two parameters separate.
| Parameter | Meaning | Typical range | Who cares |
|---|---|---|---|
| `k_retrieve` | Candidates the cross-encoder scores | 20 to 200 | Latency, cost, recall ceiling |
| `k_final` | Passages returned or sent to an LLM | 3 to 10 | Answer quality, prompt size |
k_final is set by the downstream consumer. For example, a generator prompt might only have room for five passages. k_retrieve is the tuning knob this chapter is about.
Measure the recall curve of the first stage
The most useful diagnostic is recall of the first stage at several depths. If recall@50 is 0.93 and recall@200 is 0.95, reranking 200 candidates buys you two points of ceiling at four times the cost.
The function below computes recall at several depths for a set of labelled queries. It uses the same definition as the metrics chapter.
def recall_at_k(ranked_ids, relevant, k):
return len(set(ranked_ids[:k]) & relevant) / len(relevant)
def recall_curve(runs, depths):
"""runs: list of (ranked_ids, relevant_set), one per query."""
curve = {}
for k in depths:
values = [recall_at_k(ranked, rel, k) for ranked, rel in runs]
curve[k] = sum(values) / len(values)
return curve
runs = [
(["d4", "d2", "d9", "d1", "d7", "d3"], {"d1"}),
(["d5", "d6", "d8", "d2", "d1", "d9"], {"d2", "d9"}),
(["d1", "d3", "d4", "d5", "d6", "d7"], {"d1"}),
]
for k, value in recall_curve(runs, [1, 2, 4, 6]).items():
print(f"recall@{k}: {value:.2f}")Run this against your real first stage with a few hundred labelled queries. Use depths such as 10, 20, 50, 100, and 200. The curve usually rises quickly and then flattens. The flattening point is your first candidate for k_retrieve.
Measure the final quality at each depth
Recall of the first stage is only the ceiling. What you actually care about is the quality after reranking. Sweep k_retrieve, rerank each pool, and compute your final metric (nDCG@10 or MRR, for example).
The sketch below does that sweep. It assumes you have a retrieve(query, k) function that returns a ranked list of document ids, a rerank(query, doc_ids) function that returns the ids reordered by cross-encoder score, and a list of (query, relevant_set) pairs.
def reciprocal_rank(ranked_ids, relevant):
for i, doc_id in enumerate(ranked_ids, start=1):
if doc_id in relevant:
return 1.0 / i
return 0.0
def sweep_depth(queries, retrieve, rerank, depths):
"""queries: list of (query_text, relevant_set)."""
results = {}
for depth in depths:
total = 0.0
for query, relevant in queries:
candidates = retrieve(query, depth)
reranked = rerank(query, candidates)
total += reciprocal_rank(reranked, relevant)
results[depth] = total / len(queries)
return resultsPlot MRR against depth, and plot latency against depth next to it. You will typically see one of three shapes.
- Rising then flat. The common case. Pick a depth near the start of the plateau.
- Rising then falling. Deeper pools add distractors that the cross-encoder ranks above the true answer. This is a sign of a weak or domain-mismatched model, and it is covered in the failure modes chapter.
- Flat from the start. The first stage already puts relevant passages near the top. The reranker adds little at any depth, so check whether it is worth its cost at all.
Choosing a depth in practice
Work through these steps.
- Fix the latency budget. Decide the end-to-end latency you can afford, and subtract the time for the first stage, network, and any generation step. What remains is the reranking budget.
- Measure cost per candidate. Time the cross-encoder on your hardware with realistic passage lengths. Latency per candidate depends on the model, the passage length, and the batch size.
- Compute the maximum affordable depth. Divide the reranking budget by the cost per candidate.
- Find the quality plateau. Run the sweep above and find where the final metric stops improving meaningfully.
- Pick the smaller of the two. Use the plateau depth if it is within budget. If it is not, take the affordable depth and accept the loss, or choose a smaller model.
Edge cases that change the answer
Duplicates and near-duplicates
If the corpus contains many copies of the same content, such as versioned documentation or repeated boilerplate, the first-stage top 50 may hold only ten distinct passages. Deduplicate before reranking. Otherwise you spend cross-encoder compute scoring the same text several times, and k_final may fill with copies.
Chunked documents
When documents are split into chunks, several chunks of one long document can crowd the pool. This is not always bad, but it reduces the number of distinct documents the reranker sees. If you need document-level results, consider capping the number of chunks per document in the candidate pool.
Queries differ in difficulty
A short, specific query ("reset password") may have its answer at rank 1, while a vague one may need depth 100. A fixed depth is a compromise. Some systems adapt depth, for example by reranking in small batches and stopping early when the scores fall well below the best seen so far. This adds complexity and makes latency variable, so adopt it only when a fixed depth clearly fails.
Hybrid first stages
If the candidate pool comes from fusing lexical and dense results (covered later in this tutorial), each retriever contributes its own top results. A fused pool of 50 can contain relevant passages that neither retriever ranked in its own top 10. Measure the recall curve of the fused list, not of each retriever alone.
Small corpora
If the corpus has only a few hundred passages, you may be able to rerank all of it. Doing so removes the first stage as a source of error. It is only sensible when latency is not a concern.
Common mistakes
- Choosing depth from intuition. Values like 100 are copied from tutorials. Measure on your own data.
- Reporting only final metrics. Without the first-stage recall curve, you cannot tell whether a low score comes from a weak pool or a weak reranker.
- Confusing
k_retrievewithk_final. Raisingk_finaldoes not improve recall of the pool. It only returns more of what the reranker already ordered. - Ignoring tail latency. Average cost per candidate can hide slow batches. Check p95 latency at your chosen depth, not only the mean.
- Evaluating on queries with a single labelled passage. If many passages are relevant but only one is labelled, a deeper pool can look worse because correct but unlabelled passages move above the labelled one.
Exercise
A first stage has recall@20 = 0.80, recall@50 = 0.92, and recall@200 = 0.94. Reranking 50 candidates takes 120 ms and your reranking budget is 150 ms. Which k_retrieve would you choose, and why? What would you check before settling on it?
Show answer
Choose 50. Recall rises from 0.80 to 0.92 between depths 20 and 50, but only to 0.94 at 200, so most of the gain is already captured at 50. Depth 200 would take roughly 480 ms if cost is linear, which exceeds the 150 ms budget for two points of recall. Before settling, run the sweep with the cross-encoder to confirm that the final MRR or nDCG also plateaus near 50, since the first-stage recall curve only gives the ceiling. Also check p95 latency at depth 50, because a 120 ms mean may leave little headroom under a 150 ms budget.