In a RAG system, an AI model answers from passages that a search step hands it. If the best passage comes tenth, or not at all, the answer suffers. A reranker is a second, slower model that takes the search’s shortlist and reads each passage properly against the question before putting them in order. Search wide with something fast, then let a sharper model choose the best few. Here you’ll watch a reranker fix a search’s mistakes on a real-world-sized problem, make a few of its own, and find out how long the shortlist needs to be.
Two ways to compare a question with a passage#
Vector search, the usual first step in RAG, uses an embedding model. It turns each passage into a list of numbers once, ahead of time, and turns the question into one more list when it arrives. Finding the closest passages is then just arithmetic on stored numbers, fast enough for millions of passages. Models built this way are called bi-encoders, because question and passage are encoded separately. The cost of that speed is that the model squeezes a whole passage into one summary before it has seen the question, so it can’t tell which details will matter.
A reranker is usually a cross-encoder: a model that reads the question and a passage together, as one piece of text, and outputs a single relevance score. Every word of the question can attend to every word of the passage (the attention mechanism at work), so it notices that the passage mentions E-147 and not E-471. But nothing can be computed in advance: each question means running the model once per passage. In 2019 the authors of Sentence-BERT put numbers on the gap: finding the most similar pair among 10,000 sentences took about 65 hours with a cross-encoder, and about 5 seconds with their bi-encoder.
So systems use both. The fast search picks a shortlist of candidates, and the reranker reorders just those. The same year, Rodrigo Nogueira and Kyunghyun Cho used BERT this way on Microsoft’s MS MARCO search benchmark and took the top of its leaderboard, improving on the previous best by 27%.
A reranker at work#
Here’s the help centre from the hybrid search and retrieval quality explainers, grown to 50 passages: thermostats, doorbells, cameras, plugs, billing and delivery. Thirty-five customer questions are each marked with the passages that answer them. The first search is the BGE small embedding model, and the reranker is GTE, a cross-encoder with about 150 million parameters. Both are free to download and run.
Predict first
A customer asks “what does E-147 mean”. Vector search ranks the passage that explains it eighth, behind a camera, a smoke alarm and the passage about error E-471. After the reranker reads the top 10, where does it come?
For E-147 the reranker lifts the answer from eighth to first. For “make the lamps come on while I’m on holiday”, vector search prefers the heating’s holiday mode; the reranker sees that the question is about lamps and picks the smart plug’s away mode. Across all 35 questions, reranking the top 10 puts the right passage first for 7 questions that vector search got wrong.
It also breaks one: for “the heating won’t come on”, vector search had the right passage (boiler not responding) first, and the reranker demotes it to third, below a passage that says the heating carries on without the internet. And for “does it work with Siri” it can’t make the leap from Siri to Apple Home, and drops that answer from second to sixth. A reranker is another model, with its own mistakes; measure it on your own questions before you trust it.
How many candidates to rerank#
Every extra candidate costs one more run of the reranker per question, so you want the shortest shortlist that still contains the answer. That depends mostly on the first search: the reranker can only reorder what it’s given.
Predict first
Keyword search finds a passage that answers the question somewhere in its results for 83% of the questions. If you rerank all of its results with the better reranker, how many questions end with the right passage first?
With BGE small alone, the right passage comes first for 66% of questions. Rerank the top 10 and that rises to 83%. The dashed line shows why 10 is enough here: by 10 candidates, every question’s answer is somewhere on the shortlist. Reranking 50 costs five times as much and gains nothing more. Keyword search puts the right passage first for only 49% of questions; reranking lifts that to 74%, but the dashed line stops at 83%, because six questions share no useful words with their answers.
The first search decides what can be found; the reranker decides what comes first. That’s why rerankers are so often paired with hybrid search, which catches both exact codes and paraphrases, and why the shortlist is usually dozens of passages rather than three.
Now switch to the smaller MiniLM reranker, a popular default with 23 million parameters, trained on MS MARCO’s web searches. Over BGE’s results it still helps, from 66% to 77%. Over MiniLM’s vector search, which already gets 80% right on its own, it makes things worse: 77%. A reranker trained on someone else’s kind of question isn’t guaranteed to understand yours.
When a reranker earns its place#
- Your first search often has the answer, but not at the top. If the right passage is usually in the top 20 but rarely first, reranking is the cheapest fix.
- You can only pass a few passages to the model. Smaller prompts are cheaper and models answer better with fewer distractions, so the order of the top 3 to 5 matters.
- Details matter: error codes, product names, “can” versus “can’t”. Reading the question and passage together is what catches them.
- Not when latency is everything, or when the first search already puts the right passage first almost every time. Measure first.
In code#
With sentence-transformers (pip install sentence-transformers) and the same two models as the figures. Both download on first use and run on a laptop:
import numpy as np
from sentence_transformers import CrossEncoder, SentenceTransformer
passages = [
"P1 smart plug. The P1 can switch lamps and small appliances up to 13 amps from the app or on a schedule.",
"P1 energy use. The app shows how much electricity each P1 plug has used today, this week and this month.",
"P1 plug schedules. Each P1 can follow up to 20 schedules a day, and switch lamps on at random while you're away.",
"L1 smart bulbs. L1 bulbs can change colour or shade of white, and connect to the app over Bluetooth.",
"Eco mode. When everyone has left home, eco mode lowers the temperature using your phone's location.",
]
question = "which of my plugs uses the most electricity"
# Stage 1: fast vector search over precomputed passage embeddings
retriever = SentenceTransformer("BAAI/bge-small-en-v1.5")
P = retriever.encode(passages, normalize_embeddings=True)
q = retriever.encode("Represent this sentence for searching relevant passages: " + question, normalize_embeddings=True)
candidates = np.argsort(-(P @ q))[:4]
print("vector search order:", [int(i) for i in candidates])
# Stage 2: the reranker reads the question together with each candidate
reranker = CrossEncoder("Alibaba-NLP/gte-reranker-modernbert-base")
for hit in reranker.rank(question, [passages[i] for i in candidates]):
print(f"{hit['score']:.3f} passage {int(candidates[hit['corpus_id']])}")
# vector search order: [0, 1, 2, 4]
# 0.911 passage 1
# 0.769 passage 0
# 0.761 passage 2
# 0.368 passage 4Vector search puts the general plug passage first; the reranker sees that the question is about electricity use and promotes the energy passage. In a real system, stage 1 is a vector database returning your top 20 to 50, and you pass the reranker’s top few to the model.
What to remember
- Vector search compares summaries made separately; a reranker (a cross-encoder) reads the question and each passage together, which is more accurate and far slower.
- So search wide and fast, then rerank a shortlist: here, reranking BGE’s top 10 took right-passage-first from 66% to 83%.
- A reranker can only reorder what the first search found, and makes its own mistakes; test it, and the shortlist length, on your own questions.