What Is Reranking in RAG? Making Answers More Accurate
Reranking re-orders the passages found by your first search so the model sees the best ones. Learn how it works, when it helps, and try a tiny two-stage example in Python.
Short answer: reranking in RAG is a second step that re-orders the passages your first search found, so the most relevant ones come first before they reach the language model. The first search is fast but rough; the reranker reads the question and each passage together and scores them more carefully. This usually makes answers more accurate and lets you send fewer, better passages to the model.
What Is Reranking in RAG?
In retrieval-augmented generation (RAG), the system searches your documents, puts the best passages into the prompt, and the model answers from them. Retrieval quality limits answer quality: if the right passage is not in the prompt, the model cannot use it, and if wrong passages crowd it, the model may be misled.
Reranking splits retrieval into two stages:
Retrieve (wide and fast). A vector database or keyword index returns, say, the top 20–50 candidates.
Rerank (narrow and careful). A slower model scores each candidate against the question and keeps the top few, for example the best 3–5.
Why Is the First Search Not Enough?
Embedding search compares a question and each passage after both have been squeezed into separate vectors. That is fast and scales to millions of passages, but compressing a whole passage into one vector loses detail. A passage about “refund policy for gift cards” can sit close to the question “how do I request a refund?” and still not answer it. See similarity search and cosine similarity for how that closeness is measured.
A common reranker is a cross-encoder: it reads the question and one passage together and outputs a relevance score. Because it looks at both at once, it can notice words and relationships that single vectors blur. The cost is speed, which is why you only run it on the short list. Some systems also use an LLM to rank candidates; this can work well but costs more time and money per query.
A Tiny Example You Can Run
This toy uses word overlap in place of a real model, but it shows the two-stage shape. Pretend the first search returned four passages in a slightly wrong order.
docs = {
"d1": "Refunds are issued within 14 days if the item is unused.",
"d2": "Our shipping team packs orders within 2 days.",
"d3": "To request a refund, open the Orders page and press Request refund.",
"d4": "Gift cards cannot be refunded.",
}
query = "how do I request a refund"
# Pretend a fast vector search returned these, in this order (rough similarity).
first_stage = ["d1", "d2", "d4", "d3"]
STOP = {"how", "do", "i", "a", "the", "to", "and", "is", "are", "if", "our"}
def terms(text):
words = text.lower().replace(".", "").replace(",", "").split()
return {w[:5] for w in words if w not in STOP} # crude "stemming"
def rerank_score(question, passage):
q, p = terms(question), terms(passage)
return len(q & p) / len(q)
ranked = sorted(first_stage, key=lambda d: rerank_score(query, docs[d]), reverse=True)
for d in ranked:
print(d, round(rerank_score(query, docs[d]), 2))
print("send to the LLM:", ranked[:2])It prints d3 first with a score of 1.0, then d1 and d4 at 0.5 and d2 at 0.0, so the passage that actually explains how to request a refund moves to the top. In a real system you would replace rerank_score with a cross-encoder or a hosted reranking service.
When Should You Add a Reranker?
Sign | What it suggests |
|---|---|
The right passage is in the top 20 but not the top 3 | Reranking will likely help |
The right passage is not in the top 20 at all | Fix retrieval first (chunking, embeddings, hybrid search) |
Prompts are long and answers wander | Rerank, then send fewer passages |
Responses must be very fast | Measure the extra delay before adding it |
Do not add it on a hunch. Build a small set of real questions with the passages that should answer them, and compare results with and without reranking. The lesson on reranking and retrieval evaluation walks through this, and how to evaluate AI and LLM system performance covers the wider picture.
Common Mistakes
Reranking too few candidates. If the first stage returns only five, there is little to reorder. Retrieve more, then cut down.
Reranking everything. Running a cross-encoder over a whole collection is slow. Rerank only the short list.
Skipping measurement. Without a test set you cannot tell whether answers improved.
Forgetting permissions. Filter out documents the user may not see before reranking, not after.
Key Takeaway
Retrieve wide and fast, rerank narrow and careful, then give the model only the best few passages. Add it when your evaluation shows the right answer is retrieved but ranked too low.
Ready to start? Build retrieval you can measure, with hybrid search, reranking and evaluation, in free hands-on lessons. Begin with the free course AI Engineering with LLMs: Advanced.
Related articles
More in AI Engineering
What Are AI Guardrails? Keeping an AI App Safe
AI guardrails are the checks around a model that keep an app safe and on-topic. Learn the types, a runnable Python example, and why prompts alone are not enough.
4 min readNov 26, 2026What Is Structured Output in LLMs?
Structured output makes an LLM reply in a fixed format such as JSON. Learn why it matters, four ways to get reliable JSON, and a runnable validate-and-retry example.
5 min readNov 20, 2026What Is Chunking in RAG?
Chunking splits documents into pieces a RAG system can search. Learn chunk size, overlap, the main strategies and how to test them, with code.
5 min readNov 16, 2026How to Get an LLM API Key (OpenAI and Gemini) and Understand the Cost
Where to create an OpenAI or Gemini API key, how token-based billing works, how to estimate cost and how to keep your key safe.
4 min readNov 11, 2026What Is Function Calling in LLMs?
Function calling lets an LLM ask your code to run a tool and use the result. Learn the step-by-step flow, a runnable example and the safety rules.
3 min readNov 7, 2026What Is LangChain? When to Use It (and When Not To)
LangChain is a framework for building LLM apps. Learn its core building blocks, a short sketch, and when plain code is the better choice.
4 min readNov 4, 2026
Comments
Sign in to join the discussion.



