Reranking with the Generative Model in a Single Prefill Pass for RAG

    This article explains how retrieval-augmented generation (RAG) pipelines can eliminate a redundant reranking stage by letting the generative model score candidates during the same prefill pass it already needs for answer generation. It covers the structural waste in the conventional three-stage pipeline, the core method of one shared prefill followed by parallel probes, the computational trade-offs, the implementation details that need tuning, and the scenarios where the approach pays off. Readers will learn how to read relevance scores from probe outputs, reuse the KV cache across reranking and generation, and evaluate the design using ranking quality, latency, throughput, and memory metrics.

    1. The Cost of “Reading Twice” in a RAG Pipeline

    Most teams building retrieval-augmented generation (RAG) systems have worked with the same three-stage pipeline: first, sparse or vector retrieval recalls a batch of candidate passages (typically 20 to 50); then a separate reranking model scores them one by one and keeps the top few; finally, those passages are handed to the generation model together with the question to produce a natural-language answer.

    The pipeline is stable from an engineering standpoint, but it carries one structural waste: the model that generates the answer will eventually read every selected passage in full, while the discarded passages are read once by the reranker and thrown away. The same batch of text goes through a forward pass in two different models, two sets of weights stay resident in GPU memory at the same time, and the latency of both stages adds up.

    Think of it like doing research: you ask an assistant to skim ten books and rank them, then you re-read the three that were selected. The assistant’s pass overlaps heavily with your own reading. If the assistant and you were the same person, that extra round could be skipped entirely.

    Two reranking approaches are common in the industry, each with a cost:

    • Cross-encoder reranking: the query and a single candidate passage are concatenated and fed into the model, which outputs a relevance score. Scoring is accurate and a single pass is cheap (on the order of a few to a few tens of milliseconds in public benchmarks), but a forward pass is required for every candidate, and the model must stay resident in GPU memory on its own.
    • Generative reranking: a large language model directly produces a relevance judgment or a ranking sequence. The performance ceiling is higher, but decoding is expensive — effectively maintaining a second, more costly model.

    Neither route addresses the core conflict: ranking and generation are two separate brains, each digesting the same text.

    2. The Core Idea: One Prefill, Many Probes

    Start with one basic concept.

    Prefill: before a large language model emits its first token, it must run the entire input text through the network once, computing and caching the Key/Value intermediate states (the KV cache) for every position. This step is compute-intensive, and in traditional implementations it is recomputed whenever the input changes.

    The hidden cost of a conventional cross-encoder is this: 20 candidate passages mean 20 prefills of “query + passage.” The generation stage, meanwhile, has to prefill the selected passages regardless. No computation is shared between the two prefills.

    The approach boils down to three steps:

    1. Load once: concatenate all candidate passages into a single input and run one prefill, so the KV states of every passage reside in the cache at the same time.
    2. Parallel probes: after the cache is built, append multiple “probe” queries at once, each carrying an identifier or position marker for one candidate — for example, “does passage 3 contain the answer?” All probes share the same passage cache and differ only in where their attention lands.
    3. Read out scores: take the output at a designated position for each probe (usually the logits at a “yes/no” judgment token, or the output of a scalar scoring head) to obtain a relevance score per passage; sort them, take the top K, and let the continuation of the same forward pass generate the answer directly.

    Illustrative pseudocode (structure only, not a specific framework API):

    1. # 1) Single prefill: all candidates share one KV cache
    2. prompt = build_context(question, candidates) # passages separated by delimiters and position markers
    3. cache = model.prefill(prompt) # compute-intensive, done only once
    4. # 2) Parallel probes: each probe adds only a very short input
    5. probes = [f"Does passage {i} contain the answer?" for i in range(len(candidates))]
    6. scores = []
    7. for p in probes: # can be batched/parallel, reusing cache
    8. logits = model.forward(p, cache=cache) # incremental computation only
    9. scores.append(read_yes_no_logit(logits))
    10. # 3) Rank + generate, sharing the continuation of the same forward pass
    11. topk = argsort_desc(scores)[:K]
    12. answer = model.generate(build_answer_prompt(topk), cache=cache)

    The key gain: each candidate passage is “read” only once, and ranking and generation share the same intermediate state. The separate reranker’s weights, its GPU memory footprint, and those 20 redundant prefills all disappear.

    3. Why This Pays Off Computationally

    Let N be the number of candidates, L the average passage length, and M the generation length:

    • Conventional two-stage: roughly N prefills of length (L + query) on the reranking side, plus prefill for K passages (or one concatenated prefill of K passages) on the generation side, with no sharing between the two.
    • Merged approach: one prefill of roughly N·L, plus the incremental computation of N very short probes, with generation continuing directly from there.

    Because attention computation grows superlinearly with sequence length, concatenating N passages into one long sequence is not always cheaper; the real savings come from eliminating the case where the same passage is computed once under each of two weight sets, and from dropping the GPU memory and scheduling overhead of a resident model. Whether the net result is positive depends on the passage length distribution, the number of probes, and the batching strategy, and needs to be measured.

    Two often-overlooked hidden benefits:

    • Better consistency: the ranking signal and the generation signal come from the same model and the same representation, avoiding the misalignment where “the reranker thinks it is relevant but the generation model cannot make sense of it.”
    • Simpler deployment: one set of weights, one GPU memory budget, one call chain — smaller capacity planning and a smaller failure surface.

    4. Details That Actually Need Tuning in Practice

    • Position and marker design: passages need clear separation and a global position encoding strategy, otherwise long sequences tend to exhibit positional bias where “middle passages get ignored”; probes must be able to point at the target passage unambiguously.
    • How scores are read out: taking the difference between judgment-token logits is the least effort, but it is sensitive to vocabulary and prompt wording; training a lightweight scalar scoring head is more stable, at the cost of a small number of extra parameters.
    • Training objective: both ranking (pairwise/listwise ranking loss) and generation (standard language modeling loss) must be optimized together, with their weights controlled so the ranking head does not “steal” representational capacity.
    • Long-context pressure: putting all candidates in the cache lengthens the input considerably, so chunking, sliding windows, or cache compression strategies are needed.
    • Evaluation criteria: end-to-end answer metrics alone are not enough; report ranking quality (such as hit rate and normalized discounted cumulative gain), time to first token, throughput, and peak GPU memory, so the source of the gains is visible.

    5. Where It Fits

    The idea pays off most clearly in online question-answering scenarios with a moderate number of candidates, not-too-long passages, and sensitivity to latency and GPU memory. When the candidate set is very large or passages are very long, a single extra-long prefill becomes the new bottleneck; in that case it is better to trim candidates with a coarse first-stage ranker and then enable the merged fine-ranking and generation. If you use a hosted inference service in the cloud, look first at whether it supports general capabilities such as long context, cache reuse, and batching, and keep observing latency and resource utilization with logging services and monitoring alerts.

    In one sentence: since the generation model has to read these passages sooner or later, let it finish the ranking in the same prefill pass — what you save is not a single call, but a whole set of duplicated work.