Maximal marginal relevance

On this page 7

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.

ComponentMeaningTypical implementation
sim1(d, q)Relevance of the candidate to the queryCosine similarity of embeddings or a retriever score
sim2(d, s)Similarity between two documentsCosine similarity of document embeddings
λ (lambda)Trade-off weight between relevance and diversity0.5 to 0.7 in most RAG setups
Candidate poolItems eligible for selectionTop 20 to 100 results from first-stage retrieval
kNumber of items returned3 to 10 passages for an LLM prompt
Components of the MMR objective

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 valueBehaviorSuitable situation
1.0Pure relevance ranking, no diversificationPrecise lookups with a single correct answer
0.7 to 0.9Mild deduplication of near-identical passagesQuestion answering over repetitive documentation
0.5Equal weight on relevance and noveltyDefault in several vector store libraries
0.2 to 0.4Strong diversification, broader coverageExploratory search and multi-aspect summaries
0.0Diversity only after the first resultRarely useful; drifts away from the query
Typical lambda ranges and resulting behavior

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.

TechniquePrimary goalUses document-to-document similarity
Maximal marginal relevanceReduce redundancy while keeping relevanceYes, against already selected items
Cross-encoder rerankingImprove relevance precisionNo, scores each pair independently
Reciprocal rank fusionMerge rankings from several retrieversNo, uses rank positions only
Metadata filteringRestrict candidates by attributesNo, applies hard constraints
Determinantal point processesProbabilistic diversity over the full setYes, through a kernel matrix
MMR versus neighboring retrieval techniques

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.

SituationRecommendationReason
Corpus with many near-duplicatesEnable with λ around 0.6Removes repeated chunks from the prompt
Questions needing several perspectivesEnable with λ around 0.4Surfaces distinct aspects of the topic
Single-fact lookupsDisable or use λ near 0.9Diversity can push out the only correct passage
Very large k for long-context modelsEnable with a large candidate poolPrevents clusters of similar chunks
When to enable MMR

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.