Skip to content
AgenticAIWiki
  1. Learn
  2. Part 4
  3. 23. Advanced RAG
  4. 23.7

Lesson 23.7 · 7 of 7 · 5 min read · Reviewed 24 Nov 2026

Reciprocal rank fusion step by step

By the end, you can

  • Compute reciprocal rank fusion scores by hand.
  • Explain what the constant k does and why RRF ignores raw scores.

Lesson 23.2 introduced reciprocal rank fusion (RRF) as the way to merge keyword and vector results. Here we work through it slowly, so you can compute it by hand and debug it in code.

The formula

RRF(d) = sum over every list L that contains d of  1 / (k + rank_L(d))
  • rank starts at 1 for the top result.
  • k is a constant, often 60.
  • If a list doesn’t contain d, that list adds nothing.

Raw scores are thrown away. That is the point: a BM25 score of 14.2 and a cosine similarityCosine similarity: A measure of how similar two vectors are, based on the angle between them, from -1 to 1. of 0.83 are not comparable, but “ranked 2nd” means the same thing in both lists.

Worked example

Query: “reset a locked account”. Two lists, k = 60.

Doc Keyword rank Vector rank
A: Unlock your account 1 4
B: Password reset steps 3 1
C: Account security FAQ 2 -
D: Two-factor recovery - 2

Step 1, the contributions:

Doc Keyword part Vector part RRF total
A 1/61 = 0.01639 1/64 = 0.01563 0.03202
B 1/63 = 0.01587 1/61 = 0.01639 0.03227
C 1/62 = 0.01613 0 0.01613
D 0 1/62 = 0.01613 0.01613

Step 2, sort by total: B, A, then C and D tied. B wins narrowly because both lists placed it near the top. C was second in keyword search but drops below A and B because vector search never found it.

Ties are common, so break them in a fixed way: for example, by best single rank, then by document id. Otherwise your results can reorder between runs.

What k does

k Rank 1 Rank 10 Ratio
1 1/2 = 0.500 1/11 = 0.091 5.5x
60 1/61 = 0.0164 1/70 = 0.0143 1.15x

With a small k, a first place dominates. With k = 60 the curve is flat, so a document that appears in several lists beats one that tops a single list. That flatness makes RRF robust when one retriever is noisy.

In code

def rrf(lists, k=60):
    scores = {}
    for ranked in lists:
        for rank, doc in enumerate(ranked, start=1):
            scores[doc] = scores.get(doc, 0) + 1 / (k + rank)
    return sorted(scores, key=lambda d: (-scores[d], d))

Note start=1. Starting at 0 is the most common bug, and it slightly over-weights first places.

When to use it

  • Merging keyword and vector search (23.2).
  • Merging the lists from several query phrasings (23.6).
  • Any time you want a merge with no weights to tune.

If one retriever is clearly better, a weighted RRF (multiplying each list’s part by a weight) or a rerankerReranking: Re-scoring an initial set of retrieved results with a more accurate model to put the best ones first. (23.3) on the fused shortlist can do better. Measure on your test set (chapter 26).

Key points

  • RRF gives each document 1 / (k + rank) from every list it appears in, and adds them up.
  • It uses only ranks, so lists with different score scales (BM25, cosine, a reranker) can be merged without tuning.
  • A larger k flattens the curve so agreement across lists matters more; k = 60 is a common default.
  • A document missing from a list simply gets nothing from that list.
Hands-on labRuns in the Playground

Compute RRF by hand and code

Implement reciprocal rank fusion, reproduce the lesson's worked example, and see how k changes the result.

You will have: An rrf() function that prints each document's score and the fused order for k = 60 and k = 1.

Steps

  1. Enter the two ranked lists from the lesson: keyword A, C, B and vector B, D, X, A.
  2. Implement rrf() with ranks starting at 1 and a fixed tie-break on document id.
  3. Print each document's score to five decimal places for k = 60.
  4. Repeat with k = 1 and compare which document wins.

Done when

  • With k = 60 the order is B, A, C, D (X last).
  • B's score prints as 0.03227.
  • With k = 1, B still wins, but rank 1 is worth 5.5x rank 10 instead of about 1.15x.

Open in the Playground No API key needed.

Starter code (Python)
KEYWORD = ["A", "C", "B"]          # A: Unlock your account, C: Account security FAQ, B: Password reset steps
VECTOR = ["B", "D", "X", "A"]      # D: Two-factor recovery, X: an unrelated page

def rrf_scores(lists, k=60):
    scores = {}
    # TODO: for each list, for rank starting at 1, add 1 / (k + rank) to the doc's score
    return scores

def fused(scores):
    # TODO: sort by score descending, then by id for ties
    return list(scores)

s = rrf_scores([KEYWORD, VECTOR])
for d in fused(s):
    print(d, round(s[d], 5))
Show a solution
KEYWORD = ["A", "C", "B"]          # A: Unlock your account, C: Account security FAQ, B: Password reset steps
VECTOR = ["B", "D", "X", "A"]      # D: Two-factor recovery, X: an unrelated page

def rrf_scores(lists, k=60):
    scores = {}
    for ranked in lists:
        for rank, doc in enumerate(ranked, start=1):
            scores[doc] = scores.get(doc, 0) + 1 / (k + rank)
    return scores

def fused(scores):
    return sorted(scores, key=lambda d: (-scores[d], d))

for k in (60, 1):
    s = rrf_scores([KEYWORD, VECTOR], k)
    order = fused(s)
    print(f"k={k}: " + ", ".join(order))
    for d in order:
        print(f"  {d} {s[d]:.5f}")
    print(f"  rank 1 vs rank 10 at k={k}: {(1 / (k + 1)) / (1 / (k + 10)):.2f}x")
Quick checkWith k = 60, document X is ranked 1 by keyword search and absent from vector search. Document Y is ranked 3 in both. Which ranks higher after RRF?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.