Matryoshka embeddings

Also known as: Matryoshka representation learning

On this page 7

Matryoshka embeddings are dense vector representations trained so that every leading prefix of the vector is itself a usable embedding: the first 64, 256 or 512 dimensions of a 1,024-dimensional vector still encode meaning well enough for search. The technique, formally called Matryoshka representation learning (MRL), takes its name from Russian nesting dolls, because coarse representations sit inside finer ones.

In the retrieval & knowledge part of artificial intelligence, Matryoshka embeddings let one model serve several accuracy and cost budgets at once. A team can store short vectors for fast candidate search, keep full vectors for precise scoring and choose the dimension after training, without retraining or running a second embedding model.

How Matryoshka representation learning shapes a vector

Matryoshka representation learning works by applying the training loss to several nested prefixes of the same output vector instead of only to the full vector. During training, the model computes its embedding once, then slices it to a fixed set of sizes, for example 64, 128, 256, 512 and 768 dimensions, and evaluates the objective on each slice.

The total loss is a weighted sum of the per-prefix losses. For an embedding model trained with contrastive learning, each prefix receives its own in-batch contrastive loss; for a classifier, each prefix feeds its own linear head. Because the small prefixes are penalized whenever they fail, gradient descent pushes the most important information into the earliest dimensions, while later dimensions add finer detail.

This ordering is the decisive difference from a conventional embedding model. In an ordinary model, information is spread across dimensions with no meaningful order, so cutting a vector in half removes an arbitrary portion of its signal and usually damages retrieval badly. In an MRL model, truncation removes the least important refinements first.

The following table summarizes the components of a typical Matryoshka training setup and their role.

ComponentTypical settingRole
Nesting dimensions64, 128, 256, 512, full sizePrefix sizes that receive a loss
Per-prefix lossContrastive or classification lossForces each prefix to be useful alone
Loss weightsEqual or tuned per prefixBalances small and large prefixes
NormalizationL2 normalization after slicingKeeps cosine similarity valid
Base encoderUnchanged transformerNo extra inference cost
Components of a Matryoshka training setup

Truncation, normalization and the quality curve

Using a Matryoshka embedding at a smaller size requires two steps: keep the first k dimensions and then re-normalize the shortened vector to unit length. Skipping re-normalization distorts dot-product scores, because the truncated vector no longer has length 1, although pure cosine similarity computes the normalization implicitly.

Retrieval quality declines gradually rather than abruptly as dimensions are removed. Published evaluations of MRL-trained text models generally show that halving or quartering the dimension costs only a few percent of ranking quality, while very short prefixes of 32 or 64 dimensions lose noticeably more. The exact curve depends on the model, the domain and the metric, so it must be measured on the target data.

The table below shows the storage side of the trade-off for float32 vectors, which is exact arithmetic rather than a benchmark result.

Dimensions keptBytes per vectorStorage for 10 million vectorsTypical use
1,0244,096About 41 GBFinal scoring, highest quality
5122,048About 20 GBBalanced production search
2561,024About 10 GBLarge corpora, memory limits
64256About 2.6 GBCoarse shortlist, edge devices
Storage per vector for float32 Matryoshka prefixes

Because the vector index grows linearly with dimension, the same reduction applies to RAM for an HNSW graph, to disk for an IVF index and to the cost of every distance computation during approximate nearest neighbor search.

Matryoshka embeddings compared with other compression methods

Matryoshka embeddings reduce the number of dimensions, whereas quantization reduces the precision of each dimension. The two approaches are complementary, and many production systems combine a truncated prefix with int8 or binary embeddings to multiply the savings.

The comparison below places MRL beside the most common alternatives for shrinking an embedding index.

MethodWhat it reducesNeeds retrainingMain drawback
Matryoshka truncationDimension countModel trained with MRLQuality drops at very small sizes
PCA after trainingDimension countNo, fitted per corpusProjection must be fitted and stored
Scalar quantizationBits per valueNoSmall precision loss
Binary embeddingsBits per valueNo, works best with tuned modelsNeeds rescoring for high recall
Product quantizationBits per vector blockNo, codebooks are trainedApproximate distances, tuning effort
Ways to shrink an embedding index

A frequent question is whether principal component analysis achieves the same result on an ordinary model. PCA can order dimensions by variance, but variance is not the same as retrieval relevance, and the projection must be fitted on a sample and applied to every query. MRL builds the ordering into the model itself, so any consumer can truncate without extra artifacts.

Adaptive retrieval with nested vectors

The most valuable application of Matryoshka embeddings is adaptive retrieval, also called funnel or coarse-to-fine search. The system first searches the whole corpus with a short prefix, keeps a shortlist of candidates and then rescores the shortlist with the full vector, all from a single model output.

This pattern keeps most of the recall of full-size search while running the expensive comparisons on only a few hundred candidates. It sits naturally before a cross-encoder reranking stage in a RAG pipeline, which then reorders the final top results by reading query and passage together.

The decision table below matches common situations to a sensible dimension strategy.

SituationRecommended choiceReason
Small corpus, quality firstFull dimension onlyStorage is cheap, every point of recall counts
Hundreds of millions of documents256 dimensions plus int8Index must fit in memory
Latency-critical semantic searchShort-prefix shortlist, full rescoringFast scan with precise final order
On-device or offline search64 to 128 dimensionsTight memory and power budgets
Uncertain requirementsStore full vectors, index a prefixAllows later change without re-embedding
Choosing a Matryoshka dimension strategy

Limitations and common mistakes

Matryoshka embeddings carry several practical constraints that teams often discover only in production. The most serious is that truncation works only for models trained with MRL; slicing an ordinary embedding model yields sharply worse results even though the code runs without errors.

  • Vectors of different sizes are not comparable. Queries and documents must be truncated to the same prefix length before computing similarity.
  • Only trained prefix sizes are reliable. Sizes between nesting points usually work, but sizes below the smallest trained prefix are not guaranteed.
  • Re-normalization is easy to forget, which silently skews dot-product scores in indexes that assume unit vectors.
  • Small prefixes lose fine distinctions first, such as near-duplicate products, version numbers or rare domain terms, so hybrid search with BM25 remains valuable.
  • Benchmarks do not transfer automatically. The quality curve on a public leaderboard may differ from the curve on legal, medical or code corpora.

A further subtlety concerns index migration. Changing the stored prefix length means rebuilding the vector index, but if the full vectors were kept, no new calls to the embedding model are required, which is a major operational advantage.

In plain words

A Matryoshka embedding is a list of numbers that describes the meaning of a text, arranged so that the most important numbers come first. If you keep only the beginning of the list, you still get a rough but useful description.

Think of a photo saved at several resolutions inside one file. A thumbnail is enough to find the right picture quickly, and the full image is there when you need detail. One model gives both a fast version and a precise version, so a search system can save memory and money without giving up much accuracy.

A real-world example

An online retailer runs semantic search over 40 million product descriptions using an MRL-trained model with 1,024 dimensions. Storing full float32 vectors would need about 164 GB of memory for the index alone, which exceeds the budget for its search cluster.

The engineering team indexes only the first 256 dimensions in int8, which needs about 10 GB, and keeps the full vectors on cheaper disk storage. Each query first retrieves 500 candidates from the compact index, then the system rescores those candidates with the full 1,024-dimensional vectors and passes the top 50 to a cross-encoder.

Offline evaluation on 5,000 labeled queries shows that this funnel keeps recall within about one percentage point of full-size search while cutting index memory by roughly 94 percent and lowering median query latency. The team later tests 128-dimensional prefixes for a mobile app without re-embedding a single product.