Maximal marginal relevance (MMR) is a greedy selection algorithm that builds a ranked list by repeatedly choosing the candidate that is most relevant to the query and least similar to the items already selected. In the Retrieval & knowledge area of artificial intelligence, it is used to diversify search results and the passages handed to a language model, so that a fixed context budget is not wasted on near-duplicate text.
The method was introduced in 1998 by Jaime Carbonell and Jade Goldstein for document retrieval and summarization. It remains a standard option in vector databases and retrieval-augmented generation (RAG) frameworks because it is simple, training-free and cheap to compute on a small candidate set.
How maximal marginal relevance scores candidates
MMR scores each remaining candidate with a weighted difference between relevance and redundancy. For a query q, a candidate set R and an already selected set S, the next item is the document d in R minus S that maximizes λ · sim1(d, q) − (1 − λ) · max sim2(d, s) over all s in S.
The first term rewards similarity to the query; the second term penalizes the highest similarity to any selected item. Using the maximum rather than the average means a candidate is punished if it closely repeats even one earlier result. The selection is greedy: after each pick, the redundancy term is recomputed for every remaining candidate, and the loop stops when k items are chosen.
The following table describes the components of the formula and what each one controls.
| Component | Meaning | Typical implementation |
|---|---|---|
| sim1(d, q) | Relevance of the candidate to the query | Cosine similarity of embeddings or a retriever score |
| sim2(d, s) | Similarity between two documents | Cosine similarity of document embeddings |
| λ (lambda) | Trade-off weight between relevance and diversity | 0.5 to 0.7 in most RAG setups |
| Candidate pool | Items eligible for selection | Top 20 to 100 results from first-stage retrieval |
| k | Number of items returned | 3 to 10 passages for an LLM prompt |
The lambda parameter and its effect
The lambda parameter sets the balance between relevance and novelty, and it is the single most important configuration choice. At λ = 1, MMR reduces to plain similarity ranking; at λ = 0, it ignores the query after the first pick and maximizes diversity alone.
Because relevance and redundancy scores share the same scale only when both use the same similarity function, the effective behavior of a given lambda depends on the score distribution of the embedding model. A model whose cosine scores cluster between 0.75 and 0.90 needs different tuning from one whose scores spread from 0.2 to 0.8.
| Lambda value | Behavior | Suitable situation |
|---|---|---|
| 1.0 | Pure relevance ranking, no diversification | Precise lookups with a single correct answer |
| 0.7 to 0.9 | Mild deduplication of near-identical passages | Question answering over repetitive documentation |
| 0.5 | Equal weight on relevance and novelty | Default in several vector store libraries |
| 0.2 to 0.4 | Strong diversification, broader coverage | Exploratory search and multi-aspect summaries |
| 0.0 | Diversity only after the first result | Rarely useful; drifts away from the query |
Maximal marginal relevance compared with related techniques
MMR is a post-retrieval diversification step, not a retriever in its own right. It operates on candidates produced by dense retrieval, sparse retrieval such as BM25, or hybrid search, and it is often confused with methods that change ordering for different reasons.
The table below separates MMR from neighboring techniques in the retrieval pipeline.
| Technique | Primary goal | Uses document-to-document similarity |
|---|---|---|
| Maximal marginal relevance | Reduce redundancy while keeping relevance | Yes, against already selected items |
| Cross-encoder reranking | Improve relevance precision | No, scores each pair independently |
| Reciprocal rank fusion | Merge rankings from several retrievers | No, uses rank positions only |
| Metadata filtering | Restrict candidates by attributes | No, applies hard constraints |
| Determinantal point processes | Probabilistic diversity over the full set | Yes, through a kernel matrix |
A common production pattern combines them: retrieve broadly, rerank for relevance with a cross-encoder, then apply MMR as the final step so the reranked list is also diverse. Applying MMR before a cross-encoder can discard passages the reranker would have rated highly.
Using MMR in retrieval-augmented generation
In RAG systems, MMR protects the limited context window from duplicated evidence. Chunking with overlap, mirrored pages and versioned documents frequently produce several chunks that say the same thing, and plain top-k similarity tends to return all of them together.
Practical configuration usually follows a few rules:
- Fetch a candidate pool three to ten times larger than k, because MMR can only diversify among what retrieval supplies.
- Use normalized embeddings so that cosine similarity and dot product agree and scores stay comparable.
- Start with λ between 0.5 and 0.7 and tune it on a labeled evaluation set rather than by inspection.
- Apply MMR after metadata filtering, so diversity is computed only over permitted documents.
The decision table summarizes when MMR is worth enabling.
| Situation | Recommendation | Reason |
|---|---|---|
| Corpus with many near-duplicates | Enable with λ around 0.6 | Removes repeated chunks from the prompt |
| Questions needing several perspectives | Enable with λ around 0.4 | Surfaces distinct aspects of the topic |
| Single-fact lookups | Disable or use λ near 0.9 | Diversity can push out the only correct passage |
| Very large k for long-context models | Enable with a large candidate pool | Prevents clusters of similar chunks |
Computational cost and limitations
The greedy algorithm needs about k × n similarity evaluations for n candidates, which is negligible for pools of a few hundred items but grows quickly for thousands. Implementations therefore run MMR on a pre-filtered shortlist, never on the full vector index.
MMR also has conceptual weaknesses that experienced practitioners account for. It measures diversity only through embedding similarity, so two passages with different wording but identical facts may both survive, while two passages on the same subject with different facts may be wrongly treated as redundant. The greedy procedure is not globally optimal, and the first pick is always the single most relevant item regardless of lambda.
- Over-diversification can replace a second strong passage with a weakly related one.
- Untuned lambda copied from defaults may suit neither the embedding model nor the corpus.
- Small candidate pools leave MMR nothing meaningful to choose between.
A non-technical view
Imagine asking a librarian for five books about a topic and receiving five copies of the same book. Every copy is relevant, yet the pile is not very useful. Maximal marginal relevance is the rule that stops this from happening.
It picks the best match first, then for each next pick asks two questions: does this item answer the request, and does it add something the earlier picks did not already say. The key idea is relevance plus novelty, with one adjustable dial deciding which matters more.
A concrete example
An internal support assistant at a software company answers employee questions from a knowledge base of 40,000 chunks. The corpus contains release notes copied across many product versions, so a question about resetting two-factor authentication returned five almost identical chunks from versions 4.1 through 4.5, and the model never saw the separate page on recovery codes.
The engineering team changed retrieval to fetch 40 candidates by vector similarity, rerank them with a cross-encoder and apply MMR with λ = 0.6 to select six passages. On a labeled set of 300 internal questions, the share of answers containing every required step rose noticeably, and the number of duplicate passages per prompt fell from an average of three to under one. The outcome was more complete answers within the same token budget.