A story: the suitcase you can always shrink
Picture a suitcase built like a Russian nesting doll. Inside it sits a smaller suitcase; inside that, a smaller one again; and so on. The clever part: at every level, the most important things are already packed at the front. The big suitcase holds everything; the medium one, the weekend essentials; the smallest, the bare minimum. You pick the size for the trip without ever repacking.
That is exactly the idea behind Matryoshka embeddings. The very same vector can be used in full (maximum quality) or truncated to its first few dimensions (lighter, faster), because it was trained to put the most important information up front.

A reminder: an embedding is a fixed-size vector
An embedding turns a piece of text (or an image) into a list of numbers, a vector. Two texts about the same thing produce nearby vectors. That length, the dimension, is fixed by the model: say 1024 numbers per text. The full mechanism is described in Text embedding models.
And that dimension comes at a direct cost. The larger it is:
- the more memory each vector takes (1024 float32 numbers = 4 KB; across a million documents, that's 4 GB);
- the slower the search, since comparing a query against millions of documents means dot products over that many numbers;
- the heavier a vector database's index is to load and serve.
The problem: a frozen trade-off decided too early
Traditionally you pick the dimension when you train or choose the model. It's a one-shot bet:
- small dimension → fast search and light storage, but lower quality;
- large dimension → top quality, but costs that balloon.
The catch: that trade-off doesn't adapt. In production you can't decide "for this low-stakes query, use 64 dimensions; for that one, use 1024." And if your needs change (more documents, tighter budget), you often have to re-index the whole corpus with a different model, a heavy, expensive operation.
You might think you could just keep the first 256 numbers of a 1024-dim vector to save space. With a classic embedding, that wrecks quality: the information is spread all over the vector, with no order of importance. Cutting it off throws away essential pieces at random.
The Matryoshka idea: rank information by importance
Matryoshka Representation Learning (MRL) (nested representations, named after the Russian dolls) fixes this by changing how the model is trained. The goal: make the first dimensions already carry the essentials of the meaning, so each extra dimension only adds finer and finer detail. The representations are nested like Russian dolls: the 64-dim prefix sits inside the 256-dim one, which sits inside the 1024-dim one.
The practical payoff: you can truncate the vector to any length you like, on the fly, without retraining. You simply keep the first numbers.

Matryoshka Representation Learning, the core idea: a single representation can serve several compute budgets by nesting sub-vectors of growing size inside one vector.
Two numbers from the abstract, with their context, because they are usually quoted without it. On ImageNet-1K, an image dataset, MRL reports "up to 14x" fewer dimensions at the same classification accuracy, and "up to 14x" real-world speed-ups on large-scale retrieval. Those measurements are therefore on images, not text; the paper shows the method extends to text (with BERT), but the headline figures are vision results. The paper also claims MRL adds no inference cost: that is mechanically true, since truncating means ignoring the tail of a vector you already computed.
How it is trained
The trick fits in one sentence: apply the loss to several prefixes at once.
For a classic embedding, you train the model to produce one good full-dimension vector (for example with a contrastive loss, which pulls similar texts together and pushes others apart). For Matryoshka, you pick a list of sizes, say , and for each size you compute the same loss on the matching prefix, then add them up.
This forces the model: the 64-dim prefix must already be a good embedding on its own, the 256-dim prefix must be a better one, and so on. Because the small prefixes are nested inside the large ones, the network naturally learns to push the most useful information toward the front of the vector. That simultaneous constraint is what creates the nesting-doll structure.

If I ask you to summarize a book in one sentence, then one paragraph, then one page (and the sentence must be the start of the paragraph, which must be the start of the page) you'll inevitably put the single most important idea first. MRL imposes exactly that discipline on the model.
Use case: two-stage search
The most telling use is two-stage search, a wide shortlist, then a fine sort over the survivors:
- Fast pre-selection: compare the query against the whole corpus using only the first 64 dimensions. This is very fast and cheap, and it's enough to discard the vast majority of irrelevant documents. You keep a shortlist of the best candidates (say the top 100).
- Precise re-ranking: on that small shortlist only, recompute similarity with the full (1024-dim) vector to sort finely.
You get nearly the quality of the full vector, but only spend the full dimension on a handful of documents, all with one model and one index, nothing to retrain.
This is the same cascade described in Reranking: cross-encoders and ColBERT, with one difference: here both stages use the same model at two vector lengths. There, the second stage is a different, finer, slower model. The two combine perfectly well.
Another case: cost adaptation. A low-budget mobile service serves the 128-dim version; the same service server-side serves the 1024-dim one. Or you lower the served dimension when traffic spikes, to keep up with the load.
After truncation the vector no longer has length 1, since components have been removed. If your pipeline assumes normalized vectors (the common case, because it lets a plain dot product stand in for cosine similarity, as norms and distances explains) you must renormalize the prefix before storing or comparing it. Skipping that step produces skewed scores and no visible error.
A real application
Codestral Embed, Mistral's code embedding model, exposes exactly this dial: an output-dimension parameter up to 3072, with dimensions ordered by relevance, so that keeping the first n values is all it takes to get a vector of size n. The details, and what Mistral claims at dimension 256 with 8-bit numbers, are in Codestral Embed.
Comparison with post-hoc dimensionality reduction (PCA)
Another way to shrink an embedding is to compress it after the fact, changing nothing about training. The classic method is PCA (principal component analysis): you look at a large sample of vectors, find the directions that carry the most variation, and keep only those.
Both approaches answer the same need (smaller vectors), but they differ:
| Criterion | Matryoshka (MRL) | Post-hoc reduction (PCA) |
|---|---|---|
| When it acts | During training | After, on already-produced vectors |
| Need to (re)train the model? | Yes, model trained on purpose | No, works on any existing embedding |
| How to reduce | Truncate (keep the first numbers) | Project via a matrix learned on a sample |
| Quality at small sizes | Generally better | Often decent, sharper degradation |
| Ease of implementation | The work lives in the model | Very simple to bolt on afterward |
| Several sizes at once | Yes, any prefix | One matrix per target size |
If you already have an embedding model (non-Matryoshka) and just want to trim your index a bit, PCA is often enough and far simpler: no retraining, a few lines of code. MRL shines mainly when you control training, when you target aggressive truncation (dividing the size by 8 or 16), and when you want several sizes served by a single vector.
Note too that Matryoshka truncation and quantization (lowering the precision of each number, e.g. to INT8 or binary) are complementary: the first cuts the count of values, the second the size of each. So you can truncate and then quantize, and the factors multiply, going from 1024 dimensions at 32 bits to 256 dimensions at 8 bits divides the footprint by 4 × 4 = 16.
Why it matters
Matryoshka decouples two decisions that used to be welded together: how much quality and how much cost. One model, one stored vector, and cost is then dialed in afterward, per query or per deployment. For anyone running semantic search or RAG at scale, that's a rare lever: improving latency and storage without re-indexing, precisely the heavy operation described in Text embedding models. It's also why several recent embedding models tout Matryoshka compatibility as a headline feature.
One caveat to close on: "without degrading relevance" is not free. Quality does fall as you truncate; what MRL guarantees is that the fall is gradual rather than abrupt. Where to set the dial cannot be guessed: measure recall at each length on your own questions, using the measures in recall@K, precision@K, MRR, NDCG.
Key takeaways
- An embedding is a fixed-size vector; the larger the dimension, the costlier storage and search become, and the cost/quality trade-off is normally frozen in advance.
- Matryoshka Representation Learning trains the vector so its first dimensions already carry the essentials, nested like Russian dolls.
- You can then truncate the vector (1024 → 256 → 64) on the fly, without retraining, simply keeping the first numbers.
- Training applies the same loss to several prefixes simultaneously, which forces the useful information toward the front of the vector.
- Flagship use case: two-stage search (low-dimension shortlist, high-dimension re-ranking) and cost adaptation at deployment.
- PCA reduces dimension after the fact, without retraining: simpler and often enough for a modest trim, but MRL preserves quality better under aggressive truncation and offers several sizes from a single vector.
1. Why can you not simply keep the first 256 numbers of a classical 1024-dimension embedding?
2. How does MRL training force the model to pack the essential into the first dimensions?
3. You truncate a Matryoshka vector from 1024 to 256 dimensions before storing it. Which forgotten step would corrupt the scores without raising any error?
4. The MRL paper reports up to 14 times fewer dimensions at equal accuracy. What reading precaution applies?
5. You already run a classical embedding model in production and just want to lighten your index a little. What does the page recommend?