Principle:1ay1 Agentty Maximal Marginal Relevance
| Knowledge Sources | |
|---|---|
| Domains | Information_Retrieval, Diversification |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Diversification algorithm that re-ranks retrieval results to balance relevance to the query against novelty relative to already-selected results, preventing the context window from being filled with near-duplicate chunks.
Description
A retrieval system often returns multiple chunks from the same function or file that are all relevant but nearly identical — the same function body found by BM25 and HNSW, the same doc paragraph appearing under two headings. Injecting all of them wastes context on redundancy.
Maximal Marginal Relevance (MMR) iteratively selects results that maximize a weighted combination of query similarity (relevance) and dissimilarity to already-selected results (novelty). The parameter λ controls the trade-off: λ=1 is pure relevance (no diversification); λ=0 is pure novelty (maximum diversity).
MMR is typically followed by near-duplicate removal and chunk stitching (merging adjacent chunks from the same file that were split by chunking but retrieved together).
Usage
Use this principle when:
- Retrieval results cluster around the same source, wasting context on redundancy
- You want to cover more of the codebase in a bounded context budget
- Diversity and relevance both matter for downstream generation quality
Theoretical Basis
# Abstract algorithm (NOT real implementation)
def mmr(query, candidates, lambda_param=0.7, k=10):
selected = []
remaining = candidates
while remaining and len(selected) < k:
best = max(remaining, key=lambda d:
lambda_param * sim(d, query) -
(1 - lambda_param) * max((sim(d, s) for s in selected), default=0)
)
selected.append(best)
remaining.remove(best)
return selected