Sub topic 15
Cross-encoders versus LLM-based rerankers
This chapter assumes you have read the earlier chapters on the cross-encoder architecture, the retrieve-then-rerank pipeline, latency and batching, and the metrics recall@k, MRR, and nDCG. It compares the cross-encoder you have been using with rerankers built on general-purpose large language models (LLMs). It then gives criteria for choosing between them.
What an LLM reranker is
A cross-encoder is a small transformer, often 20 to 300 million parameters. It was fine-tuned to output one relevance score for a (query, passage) pair. An LLM reranker uses a much larger decoder model, typically several billion parameters. It is either prompted to judge relevance or fine-tuned to do so.
Both read the query and the passage together, so both can model fine-grained interaction between them. The differences are in size, in how the model is asked for a ranking, and in what you pay for it.
Three ways to use an LLM for ranking
Pointwise
You prompt the model with one query and one passage and ask whether the passage answers the query. Then you read the model's probability for the token "yes" (or "true") and use it as the score. This has the same shape as a cross-encoder: one forward pass per candidate, scores that you sort. You can batch it the same way. Its output is a score, not generated text, so it is easy to parse.
Listwise
You put the query and several numbered passages into one prompt. The model generates an ordering such as 3 > 1 > 2. The model can compare passages against each other, which pointwise scoring cannot do. The costs are larger prompts, token-by-token generation, and output that you must parse and validate.
Prompts are limited by the context window, so long candidate lists are handled with a sliding window. You rank the last 20 passages, move the window up by 10, and repeat. Relevant passages bubble toward the top. This takes several sequential LLM calls, so the calls cannot all run in parallel.
Pairwise
You show the model two passages and ask which is more relevant. Sorting n candidates needs on the order of n log n comparisons at best, or n² if you compare every pair. This is accurate but expensive, so it is rare in production.
Fine-tuned LLM rerankers also exist. They take an LLM backbone and train it on ranking data, as covered in the chapters on training objectives. They behave like cross-encoders with a larger backbone: pointwise scores, a fixed input format, and no prompt fragility.
A listwise reranker you can run
The code below shows the part of listwise reranking that most often breaks in practice: turning model text back into a valid ordering. The llm argument is any function that takes a prompt string and returns a string, so you can plug in any provider. The demo uses a fake model that returns a messy answer with a duplicate and an out-of-range number.
import re
def build_prompt(query, passages):
lines = [
f"Rank the passages below by relevance to the query: {query}",
"Answer with passage numbers only, most relevant first, like: 2 > 1 > 3",
"",
]
for i, p in enumerate(passages, start=1):
lines.append(f"[{i}] {p}")
return "\n".join(lines)
def parse_ranking(text, n):
order = []
for token in re.findall(r"\d+", text):
idx = int(token)
if 1 <= idx <= n and idx not in order:
order.append(idx)
# Append anything the model skipped, in original (first-stage) order.
order += [i for i in range(1, n + 1) if i not in order]
return order
def rerank_listwise(query, passages, llm):
answer = llm(build_prompt(query, passages))
order = parse_ranking(answer, len(passages))
return [passages[i - 1] for i in order]
def fake_llm(prompt):
return "3 > 1 > 3 > 7" # duplicate and out-of-range ids
passages = [
"Our API returns JSON by default.",
"Reset your password in Account Settings.",
"Rotate API keys from the Security page.",
]
for p in rerank_listwise("How do I rotate an API key?", passages, fake_llm):
print(p)The function ignores duplicates and invalid ids. It appends missing passages in their first-stage order, so a malformed answer degrades to the baseline order instead of dropping candidates. Production code should also log every parse failure, because a rising failure rate is an early sign of a prompt or model problem.
Side-by-side comparison
| Property | Cross-encoder | Pointwise LLM | Listwise LLM |
|---|---|---|---|
| Model size | Tens to hundreds of millions of parameters | Billions | Billions |
| Cost per query | One short forward pass per candidate | One longer forward pass per candidate | One long prompt plus generated tokens, often several calls |
| Output | Score per passage | Score per passage | Ordering as text |
| Deterministic | Yes | Yes, with fixed settings | Not guaranteed, even at temperature 0 on hosted APIs |
| Needs training data | Yes, for a domain fit | No for zero-shot | No for zero-shot |
| Failure on bad output | None | Rare | Parse errors, missing or repeated ids |
| Sensitive to candidate order | No | No | Yes |
The last row matters. A cross-encoder scores each pair independently, so shuffling the input list changes nothing. A listwise model sees all passages at once. Its answer can depend on where a passage sits in the prompt, and long prompts tend to weaken attention to passages in the middle. A common mitigation is to run the ranking twice with different input orders and combine the results, for example with reciprocal rank fusion. That doubles the cost.
Where each one wins
Cross-encoders are strong when you have a stable domain, a latency budget in the tens to hundreds of milliseconds, and some labeled data. They are cheap to host, predictable, and easy to evaluate. Their weakness is reach. A model trained on web search data may not understand your legal or medical vocabulary until you fine-tune it.
LLM rerankers are strong in three situations:
- No labeled data. You can describe relevance in the prompt, such as "prefer the most recent policy" or "the passage must state the numeric limit".
- Reasoning-heavy queries. The question needs multi-step inference to connect it to the passage, and a small cross-encoder cannot make that inference.
- Criteria that change. Editing a prompt is faster than collecting data and retraining.
Their weaknesses are latency, price, variability, and data exposure. Sending passages to a hosted model may be disallowed for private documents. A self-hosted model removes that concern and adds GPU operations.
A three-stage pipeline
You do not have to pick one. A common design uses the cheap models to shrink the list before the expensive model sees it.
The cross-encoder has already removed most of the noise, so the LLM prompt holds 20 short passages instead of 100. That keeps the prompt within the context window and lowers the cost per query. It also lets the LLM spend its effort on the hard distinctions among near-relevant passages.
Deciding whether the LLM stage is worth it
Use the evaluation method from the metrics chapter. Do not decide from a few examples.
- Build a labeled query set from your real traffic, with at least a few dozen queries and relevance judgments.
- Record recall@k, MRR, and nDCG for the first-stage baseline, then for the cross-encoder.
- Add the LLM stage over the cross-encoder's top 20 and record the same metrics.
- Record p50 and p95 latency and cost per 1,000 queries for each configuration.
- Compare the gain per added second and per added dollar, not the metric alone.
An LLM stage that improves nDCG@10 by a small margin while multiplying latency by ten is rarely a good trade for interactive search. It may be a good trade for an offline pipeline, or for a question-answering system where a single wrong passage produces a wrong answer.
Check the failure modes before shipping:
- Candidate pool. Reranking cannot recover a relevant passage that first-stage retrieval missed. If recall@100 is low, fix retrieval first. This holds for LLM rerankers as much as for cross-encoders.
- Truncation. Long passages get cut off in both kinds of model, only at different lengths.
- Prompt drift. A changed prompt or a model version update can shift rankings silently. Pin versions and rerun the evaluation set after each change.
- Position bias. Test with shuffled candidate order and compare the results.
Exercise
Your cross-encoder pipeline reaches recall@100 of 0.97 but nDCG@10 of only 0.55. A colleague proposes replacing the first-stage retriever with a larger LLM reranker over the top 1,000 candidates. Name one problem with that plan, and suggest a cheaper experiment to try first.
Show answer
Reranking 1,000 candidates with an LLM is very expensive and exceeds the context window of listwise prompts, so it would need many windowed calls or one model call per passage. The high recall@100 also shows the retriever is not the bottleneck: relevant passages are present but ranked too low. A cheaper experiment is to keep the first stage and the cross-encoder, then add an LLM stage over only the cross-encoder's top 20. Measure nDCG@10, latency, and cost against the current setup. Fine-tuning the cross-encoder on in-domain data is another low-cost option.