Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Re-ranking for Document Relevance

Hello! In our last lesson, we enhanced our RAG system by implementing hybrid search, which combines the semantic power of vector search with the precision of keyword search. You learned how to use an EnsembleRetriever to fuse results from both, giving you a more robust initial set of documents.

Today, we'll focus on the final and arguably most impactful step in the retrieval phase. The learning outcome for this lesson is to use a re-ranking model to improve the relevance of retrieved documents before generation.

Even with hybrid search, the initial retrieval stage is optimized for recall—finding all potentially relevant documents. It's fast but not always precise. The top-ranked documents might be on the right topic but may not contain the exact answer to the user's query. This is where a re-ranker comes in. It acts as a second, more powerful filter, sorting the retrieved documents by their true relevance to the query.

This lesson will cover:

  • The two-stage retrieval architecture: a fast "retriever" followed by an accurate "re-ranker".
  • The key difference between the Bi-Encoder architecture (used in retrieval) and the Cross-Encoder architecture (used in re-ranking).
  • How to integrate a re-ranking model into your RAG pipeline.
  • The trade-offs between accuracy, latency, and cost.

Let's begin by understanding why this extra step is so valuable.

The Problem: Similarity vs. Relevance

Your current retrieval system, whether vector-based or hybrid, finds documents based on similarity. A query like "What was the impact of the printing press on science?" will retrieve documents that are semantically similar—they'll mention "printing press," "science," "books," "knowledge," etc.

However, a document could be a biography of Johannes Gutenberg, which is highly similar but might not explicitly discuss the impact on science. Another document might be a short paragraph that directly answers the question. The first retriever might rank the Gutenberg biography higher because it has more keyword and semantic overlap. This is the classic problem of similarity not equaling relevance.

Re-ranking solves this by introducing a second stage designed specifically to evaluate relevance.

RAG Pipeline Comparison: Standard vs. Reranking
This diagram illustrates the benefit of re-ranking. A standard RAG pipeline (top) retrieves a mix of relevant and irrelevant documents, which can confuse the LLM and lead to inaccurate answers. The RAG pipeline with a re-ranker (bottom) uses a specialized model to filter and re-order the initial set, passing only the most relevant documents to the LLM for a precise answer. Source: substack.com.

How Re-rankers Work: Bi-Encoders vs. Cross-Encoders

The magic behind most re-rankers lies in a different model architecture: the Cross-Encoder. To understand its power, let's contrast it with the Bi-Encoder architecture used by your initial retriever.

  • Bi-Encoder (Retrieval): The query and documents are converted into vector embeddings independently. The system then performs a very fast mathematical comparison (like cosine similarity) on these vectors. Because the document embeddings can be pre-computed and stored, this process is incredibly fast and scalable.

  • Cross-Encoder (Re-ranking): Instead of processing them separately, a cross-encoder takes the query and a document together as a single input. This allows the model's attention mechanism to look at the interaction between the query's tokens and the document's tokens directly. It can understand nuance, word order, and intent much more deeply. The output is not an embedding but a single score (e.g., from 0 to 1) representing relevance.

Cross-encoders Vs Bi-encoders Architecture Comparison
This image shows the architectural difference. A Bi-Encoder (left) creates separate embeddings for each sentence and compares them at the end. A Cross-Encoder (right) processes both sentences together, allowing for deeper interaction analysis, which results in a more accurate relevance score. Source: LinkedIn Engineering.

The trade-off is performance. Cross-encoders are much slower because they must run a full model pass for every (query, document) pair. You cannot pre-compute anything. This is precisely why we use a two-stage process:

  1. Stage 1 (Recall): Use a fast Bi-Encoder (or hybrid search) to retrieve a large set of candidate documents (e.g., top 50-100).
  2. Stage 2 (Precision): Use a slower, more accurate Cross-Encoder to re-rank this smaller set and pick the absolute best ones (e.g., top 3-5).

To dive deeper into these concepts, the following video provides an excellent explanation.

Do Reranking Models Actually Improve RAG?

This video from Adam Lucek explains the concept of re-ranking, revisits how bi-encoders work for initial retrieval, and then details the cross-encoder architecture.

Please watch the following segments: What is Reranking? (00:00 - 02:00): This introduces the high-level concept and its purpose in a RAG pipeline. First Stage Retrieval with Bi-Encoders (03:45 - 09:16): A good recap of how your current retriever works, setting the stage for what follows. Reranking with Cross-Encoders (09:16 - 14:59): This is the core of the lesson. It explains the cross-encoder architecture and demonstrates its effectiveness with a code example. Pay close attention to how it can distinguish between documents that are merely similar and those that are truly relevant.

An Alternative: Late Interaction with ColBERT

While cross-encoders are the most common re-ranking approach, other architectures exist that try to balance speed and accuracy. One notable example is ColBERT (Contextualized Late Interaction over BERT).

Conceptually, ColBERT works as follows:

  1. Indexing (Offline): For each document in your knowledge base, it generates and stores an embedding for every token.
  2. Querying (Online): When a query comes in, it generates an embedding for each token in the query.
  3. Interaction: It then calculates the maximum similarity for each query token embedding against all token embeddings in a document.
  4. Scoring: These maximum similarity scores are summed up to get the final relevance score for the document.

This "late interaction" is faster than a cross-encoder because much of the work is pre-computed, but it's more fine-grained and accurate than a bi-encoder.

The same video provides a clear explanation of this technique.

Do Reranking Models Actually Improve RAG?

Let's continue with the video by Adam Lucek to understand the ColBERT late interaction model.

Watch the section on Late Interaction (14:59 - 21:34). Focus on how this model differs from both bi-encoders and cross-encoders, offering a compromise between speed and performance.

Integrating a Re-ranker into Your RAG Pipeline

Now for the practical part. As a software engineer, you'll be glad to know that adding a re-ranker can be quite straightforward, whether you're using a commercial API or a self-hosted open-source model.

Using a Commercial API (e.g., Cohere)

Services like Cohere offer powerful, highly optimized re-ranking models accessible via a simple API call. This is often the easiest way to get started.

RAG Reranking Explained: How To Improve RAG Results

This video from pixegami provides a concise, practical guide to integrating Cohere's reranking API into a Python RAG application.

Watch the segment 'Adding Reranking with Cohere's API' (07:00 - 10:05). Observe how the implementation follows the two-stage pattern: first, retrieve more documents than needed (e.g., 15), then pass them to the co.rerank API endpoint to get a sorted list of the best results.

Using an Open-Source Model

For more control or to avoid external dependencies, you can use open-source cross-encoder models from hubs like Hugging Face. The sentence-transformers library makes this very accessible.

RAG Explained: Reranking for Better Answers

This article from Towards Data Science provides a full code walkthrough for adding an open-source cross-encoder to a LangChain-based RAG pipeline. It's a great reference for a self-hosted approach.

Read the sections 'Reranking with a Cross-Encoder' and 'Back to the ‘War and Peace’ Example'. Focus on: The explanation of why a cross-encoder isn't used for the initial retrieval. The code snippets showing how to initialize a CrossEncoder from sentence-transformers. The rerank_with_cross_encoder function, which takes the query and retrieved documents and returns a re-sorted list based on the model's predictions.

The general pattern you'll implement is:

  1. Perform initial retrieval to get k documents (e.g., k=50).
  2. Create pairs of (query, document_text) for each of the k documents.
  3. Pass these pairs to the re-ranker model to get relevance scores.
  4. Sort the documents based on these new scores in descending order.
  5. Select the top n documents from the re-ranked list (e.g., n=5) to use as context for the LLM.

Performance, Costs, and Optimal Configuration

Adding a re-ranking step is a trade-off. You gain accuracy but incur additional latency and potential cost.

Quantifying the Gain

Research shows the accuracy improvement is often substantial.

New Research: Cross-Encoder Reranking Improves RAG Accuracy by 40%

This research summary highlights the significant impact of re-ranking. It's useful for understanding the quantitative benefits.

Review the tables in the sections 'Key Findings' and 'Optimal Configuration'. Note the following: The performance improvement across various benchmarks (e.g., +33% average). The 'sweet spot' for configuration: retrieving 50-100 candidates and re-ranking to a final set of 10 often provides the best balance of accuracy and latency.

The Trade-offs

It's crucial to be aware of the practical implications of adding this step.

RAG Reranking Explained: How To Improve RAG Results

Let's revisit the video from pixegami to hear about the practical trade-offs.

Watch the final section 'Trade-offs and Conclusion' (12:47 - 14:23). It discusses the impact on latency, cost, and system dependencies.

For many applications, a slight increase in latency (~100-300ms) is a small price to pay for a significant jump in answer quality.

Conclusion

You have now learned how to implement the final, critical refinement step in a state-of-the-art RAG retrieval pipeline. By adding a re-ranking stage, you move beyond simple similarity matching to true relevance assessment.

Key Takeaways:

  • Two-Stage Retrieval: Modern RAG combines a fast, high-recall retriever (Bi-Encoder) with a slower, high-precision re-ranker (Cross-Encoder).
  • Architectural Difference: Cross-encoders are more accurate because they analyze the query and document text together, allowing for a deeper understanding of relevance.
  • The Workflow: Retrieve a large candidate set (e.g., 50-100 docs), then use a re-ranker to sort them and select the top few (e.g., 3-10) for the LLM.
  • Trade-offs: Re-ranking significantly boosts accuracy, especially for complex or ambiguous queries, at the cost of increased latency.

With metadata filtering, hybrid search, and now re-ranking, you have a complete toolkit for building a highly effective and precise retrieval system.

In the next module, we will shift gears from building question-answering systems to creating AI Agents. You will learn what defines an agent and how they move beyond simple prompt-response interactions to become autonomous entities that can reason, plan, and use tools to accomplish complex tasks.

Can't find a good explanation? Sign up and we'll make it for you

Sign up