ID recall punishes an equally near tie at the k-th distance; distance-threshold recall can hide a missing closer item. A tie-aware rule and a worked table.
The question recall is supposed to answer
When you tune an approximate nearest-neighbour index for retrieval, recall at k is the number most teams watch. Build the exact top k for a set of queries by brute force, ask the index for k results, and measure how much of the exact answer came back. The number decides index parameters, memory budgets and whether a new embedding model is ready. It is worth checking that the number measures what you think, because the textbook definition has a problem exactly where retrieval data is most uneven: at the k-th distance, when several items are equally far from the query.
Ties are not rare. Duplicate and near-duplicate documents are common in help-desk knowledge bases. Quantised vectors map distinct embeddings onto the same codes. Distances rounded to a few decimal places collide. Whenever the k-th and (k+1)-th exact distances are equal, the exact top k is not unique, and the ID set you compare against is only one of several correct answers.
scikit-learn's user guide warns about the same thing for its neighbour algorithms: if neighbours k and k+1 have identical distances but different labels, the result depends on the ordering of the training data. A brute-force ground truth has the same property. Its tie-break is whatever order the sort happened to use.
Three recall rules on one query
The companion example uses one synthetic query against seven help articles at the fictional Belgavi's Book Store, with exact distances already computed. KB-101 is at 0.10 and KB-102 at 0.20. Three articles, KB-103, KB-104 and KB-105, are all at 0.30. KB-106 is at 0.45 and KB-107 at 0.60. With k equal to 3, the k-th distance is 0.30, and the exact top three under a sort by distance and then ID is KB-101, KB-102 and KB-103. KB-104 and KB-105 are just as correct as KB-103.
ID recall counts how many returned IDs are in that exact set, divided by k.
Threshold recall is the rule ANN-Benchmarks uses in ann_benchmarks/plotting/metrics.py. It computes a threshold of the k-th true distance plus a small epsilon, 0.001 by default, and counts how many of the first k returned results have a distance at or below it. It never looks at IDs.
Tie-aware recall, the rule this guide proposes, counts distinct returned items within the same threshold, but caps the count so that every item strictly closer than the k-th distance must be present to score 1.0.
def tie_aware_recall(returned, dist, k, epsilon=1e-3):
dk = sorted(dist.values())[k - 1]
distinct = list(dict.fromkeys(returned[:k]))
within = [i for i in distinct if dist[i] <= dk + epsilon]
closer = {i for i, d in dist.items() if d < dk - epsilon}
missing_closer = closer - set(distinct)
return min(len(within), k - len(missing_closer)) / kThe measured table
The example scores five returned lists. These are its printed results, for ID recall, threshold recall and tie-aware recall respectively.
- Exact order (KB-101, KB-102, KB-103): 1.0, 1.0, 1.0.
- Another boundary tie (KB-101, KB-102, KB-105): 0.667, 1.0, 1.0. The index returned an equally near article. ID recall calls this a miss.
- Misses a strictly closer item (KB-101, KB-104, KB-105): 0.333, 1.0, 0.667. KB-102 at 0.20 is missing, and two tied items fill its slot. Threshold recall says perfect, because every returned distance is at or below 0.30.
- A duplicate (KB-101, KB-102, KB-102): 0.667, 1.0, 0.667. Threshold recall counts the same item twice.
- One clearly wrong item (KB-101, KB-102, KB-106): 0.667 under all three rules.
The second row is why ID recall is too harsh, and the third and fourth rows are why distance-only recall is too generous. Neither is a bug in the abstract. ID recall is exactly right when distances are unique. Threshold recall is used to compare many algorithms on standard benchmark datasets, where returned lists are usually distinct and an algorithm that skips the true nearest neighbour while returning several tied ones is unusual. In your own pipeline, with your own deduplication bugs and quantisation, you cannot assume either condition.
Why missing a closer item matters for retrieval
In retrieval-augmented generation, the first result usually carries the most weight. Re-rankers, prompt builders and citation logic often prefer the top few. An index that returns three equally ranked tie items while dropping a clearly closer one has changed the context the model sees, not merely its order. In the example, KB-102 is the refund-to-card article and the tied items are about damaged books, wrong editions and late deliveries. A question about a card refund answered from the late-delivery article looks fluent and is wrong.
Duplicates hurt in a different way. Two copies of the same chunk take two context slots and add nothing. If your ingestion pipeline sometimes indexes a document twice, threshold recall will report an improvement while the useful context shrinks.
Implementing tie-aware evaluation
- Compute ground truth with the same distance function and precision as the index. If the index stores float16 or product-quantised vectors, the exact distances for ground truth should still come from full-precision vectors, but you must decide which you are measuring: the index's search error or its total error including quantisation.
- Store the k-th true distance for each query alongside the ground-truth IDs. Without it you cannot recognise ties later.
- Choose epsilon from the precision of your distances, not from habit. With float32 cosine distances in a few thousand dimensions, 0.001 can be large enough to accept a genuinely farther item. Record the value.
- Deduplicate returned IDs before counting, and report the duplicate rate as its own metric.
- Report the fraction of queries whose k-th distance is tied. If it is high, your ID recall numbers have been understating quality, and comparisons between two indexes may have been decided by tie-breaking.
- Keep ID recall too. When tie-aware recall and ID recall differ on a query, that query is worth looking at.
A note on what epsilon does
The epsilon in ANN-Benchmarks is additive: a threshold of the k-th distance plus 0.001. The same file also defines a relative variant, the k-th distance times one plus epsilon. Both turn a strict tie test into a tolerance band. That is reasonable for floating point, but it also means an item just beyond the k-th distance counts as correct. The example's KB-106 at 0.45 is far outside any reasonable band, so it is wrong under all three rules. An item at 0.3005 would pass. If your distances cluster tightly, check how many returned items fall inside the band but beyond the exact k-th distance, and report that number as well.
Failure modes to test
- Every query in a test set has unique distances. Assert that tie-aware recall equals ID recall, to show the new rule changes nothing when there are no ties.
- A query with a three-way tie at the boundary. Assert that any two of the tied items with the closer ones score 1.0.
- A result list that omits a strictly closer item. Assert that the score is below 1.0 even though every returned distance passes the threshold.
- A result list with a duplicate. Assert that the duplicate counts once.
- Ground-truth distances computed in float64 and index distances in float32. Assert that the tolerance absorbs the difference for genuinely tied items and not for distinct ones.
When this evaluation is unnecessary
If your corpus has been deduplicated, your vectors are full precision and your distances rarely tie, ID recall is fine, and adding a second rule is just noise. Check the tie fraction once, on a representative query set. If it is near zero, keep the simple metric and write down why. If you evaluate retrieval end to end, by checking whether answers cite the right article, the recall subtleties matter less than the answer check itself. Index recall is a diagnostic, not the goal.
Verification checklist
- Ground truth stores IDs and the k-th distance for every query.
- The evaluation reports ID recall, tie-aware recall, the tie fraction and the duplicate rate.
- Epsilon is recorded with the results and justified by the precision of the distances.
- A regression test reproduces the five rows of the table above from the example's synthetic distances.
Run the example yourself
The three recall rules over the synthetic distances, with tests for every row of the table. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.