News / Learn / Retrieval-augmented generation (RAG)
What is reranking and when does it help?
You'll be able to explain what reranking does in a RAG pipeline and decide when adding it will improve answers.
In plain English
Reranking is a second pass over search results. A fast first search pulls back many candidate chunks of text. A slower, more careful model then reads each candidate alongside the question and sorts them by how well they match. Only the best few go to the language model.
Why it matters
The first search in a RAG system is built for speed, so it can miss the most relevant text or rank it low. Reranking fixes the order before the model writes an answer. Better context usually means fewer wrong or unsupported answers, and it lets you send fewer tokens to the model.
How it works
A typical retrieval step works in two stages. The first stage, often vector search, scores every document in the index with a cheap method such as cosine similarity between embeddings. It returns maybe 50 to 100 candidates. This stage is fast but compares the question and each chunk as separate vectors, so it can miss fine-grained relevance.
The second stage is the reranker. It takes the question and one candidate at a time, reads them together, and outputs a relevance score. Cross-encoder models do this. Because they process each pair in full, they are more accurate but too slow to run over a whole corpus. Running them over a short candidate list is affordable.
After scoring, you sort by the new score and keep the top few, perhaps 3 to 10. Those chunks go into the prompt. The language model never sees the discarded candidates, so the context window stays small and focused.
Reranking helps most when the first stage returns many plausible results and the right one is buried, when questions are phrased differently from the source text, or when precision matters more than recall. It helps less when the corpus is small, when keyword or vector search already ranks well, or when latency is tight and you cannot afford an extra call.
A concrete example
Retrieve broadly, then rerank down to the top few. This sketch uses a generic scoring function in place of a real reranker model.
def rerank(query, candidates, score_fn, keep=5):
# score_fn(query, text) returns a relevance score, higher is better
scored = [(score_fn(query, c["text"]), c) for c in candidates]
scored.sort(key=lambda pair: pair[0], reverse=True)
return [c for _, c in scored[:keep]]
# Stage 1: fast retrieval returns 50 candidates
candidates = vector_search(query, top_k=50)
# Stage 2: a slower, more accurate scorer picks the best 5
top_chunks = rerank(query, candidates, score_fn=cross_encoder_score, keep=5)
prompt = build_prompt(query, top_chunks)A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Reranking too few candidates The reranker can only reorder what the first stage returns. If the right chunk is not in the candidate list, reranking cannot find it. Retrieve a wider set first, such as 20 to 100, then cut down.
- Treating the reranker score as a probability Reranker scores are useful for sorting but are not calibrated confidence values. Use them to compare candidates, and set any cutoff by testing on your own data.
- Adding reranking without measuring it Build a small set of questions with known correct sources. Compare retrieval hit rates and answer quality with and without the reranker before you keep it.
- Ignoring latency and cost Each rerank call adds time and sometimes a fee. Measure the extra latency per request and confirm the quality gain justifies it for your use case.
Going deeper
Study cross-encoder versus bi-encoder architectures, then learn hybrid search, which combines keyword and vector retrieval before reranking. Next, read about evaluation metrics for retrieval such as recall at k and mean reciprocal rank.
FAQ
Does reranking replace the first search step?
No. Reranking runs after a first search. The first step finds candidates cheaply, and the reranker orders them carefully. You need both.
Is reranking the same as fine-tuning a model?
No. Reranking uses a separate model to score relevance at query time. Fine-tuning changes the weights of a language model. The two are unrelated steps.
How many chunks should I keep after reranking?
There is no universal number. Start with a handful, such as 3 to 10, and tune it by testing answer quality against the context window and cost you can accept.
Will reranking stop hallucinations?
Not by itself. Better context makes grounded answers more likely, but the model can still invent content. Pair retrieval improvements with instructions to cite sources and evaluation to catch errors.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.