Reranking in RAG Pipelines
Reranking in Retrieval-Augmented Generation (RAG) pipelines is a second-stage scoring step that reorders an initial set of candidate documents retrieved by a fast first-stage retriever, using a more computationally expensive but semantically accurate model to maximize the relevance of context passed to the language model. It matters because first-stage retrievers (sparse BM25 or dense bi-encoders) optimise for recall at scale, not precision, and the quality of the final generated answer is tightly coupled to the relevance of the top-k documents actually fed into the prompt. The counterintuitive fact: a reranker that sees only the top-20 candidates from a cheap retriever routinely outperforms an expensive cross-encoder applied exhaustively to the entire corpus, because the ranking problem is much easier than the retrieval problem.
First-stage retrieval and second-stage reranking solve fundamentally different problems under fundamentally different computational budgets. A bi-encoder embeds a query and every document independently, enabling sub-linear search via ANN indices, but sacrifices the token-level cross-attention that captures subtle semantic relationships. A reranker receives the query and a candidate document together as a single input, letting every query token attend to every document token, which is why it is far more accurate — but also why you can only afford to run it on O(100) candidates, not millions.
How It Works
Reranking reformulates document scoring as a pointwise or pairwise classification problem. Given a query and a candidate document , a cross-encoder reranker computes a scalar relevance score by concatenating the two sequences and passing them through a transformer:
Explanation: The model reads query and document jointly in a single forward pass, letting every token attend to every other token, and extracts a scalar relevance score from the [CLS] embedding. Here is typically a linear head on top of the [CLS] hidden state of a pretrained language model (e.g. BERT, DeBERTa), and is the raw logit. Documents are then sorted in descending order of and the top- are selected for the prompt. Training uses a pairwise ranking loss. Given a positive document and a negative for query :
Explanation: This margin-based hinge loss penalises any case where the negative document scores within 1 unit of the positive, pushing the model to rank relevant documents strictly above irrelevant ones. A softer, more widely used alternative is the listwise softmax cross-entropy over the candidates returned by the first stage:
Explanation: This InfoNCE-style loss treats the single positive document as the correct class among all N candidates, encouraging the reranker to assign it the highest probability in the softmax distribution. At inference, the full reranking pipeline computes a normalised probability for each candidate:
Explanation: Dividing by temperature controls score sharpness — a small concentrates mass on the top document, while a larger produces a softer distribution suitable for score-weighted fusion. For Reciprocal Rank Fusion (RRF), a training-free alternative that merges rankings from multiple retrievers before reranking:
Explanation: Each retriever contributes a score inversely proportional to the document's rank in its ordered list, with constant (typically 60) dampening the influence of very high ranks and preventing single-retriever domination.
Jimmy Lin is a Professor at the University of Waterloo whose research spans information retrieval and NLP, and he was instrumental in formalising the cascaded multi-stage ranking paradigm that underlies modern RAG pipelines — his work on the MS MARCO leaderboard directly shaped how the community benchmarks first-stage retrieval and second-stage reranking as separate, composable components. His 2021 paper "A Few Brief Notes on DeepImpact, COIL, and a Conceptual Framework for Information Retrieval Techniques" helped practitioners reason about the retrieval-reranking tradeoff in terms of interaction depth vs. computational cost. Surprisingly, Lin started his career studying computational biology before pivoting entirely to information retrieval after a chance encounter with Lucene in graduate school.
Intuition
Imagine you're a hiring manager for a senior engineering role. You receive 10,000 applications, so you first ask HR to screen by keyword matching — anyone without "Python" or "distributed systems" in their résumé gets filtered out. This is your first-stage retriever: fast, coarse, high recall. You end up with 100 finalists. Now you personally read each of those 100 résumés in the context of the specific job description — noticing which candidates actually built distributed systems versus just mentioning the word, which projects are truly relevant versus tangentially related. You're doing cross-attention: the job description and the résumé interact token-by-token in your head. This is the reranker. The brilliant insight is that you never needed to personally read all 10,000 applications — that would have been impossible and unnecessary. HR already handled the impossible part. You just needed to be precise about the 100 survivors. A third analogy: reranking is like a wine sommelier's second pour. The first pour (retrieval) eliminates wines from the wrong region or vintage year with a quick look at the label. The second pour (reranking) is a slow, deliberate tasting — contextual, nuanced, expensive per sample, but applied only to the shortlist.