◉GEMINI LABJP
●ARGON — gemini-4-argon is generally available as of 10/13, aimed at complex coding, agents, math and science●DEEP RES — deep-research-pro-preview-12-2025 shuts down 10/23, 6 days left. Move to deep-research-preview-04-2026●NANO — gemini-nano-banana-2.1 is GA (10/06). gemini-3.1-flash-image is deprecated with no shutdown date yet●MCP — One write-up cut an 85,000-token tool result to 310 tokens by wrapping it in an ADK FunctionTool●FLASH — gemini-3.7-flash and 3.5-flash now route automatically to newer Flash models (10/08)●NEW — Stop Re-Reading Every Crash Report: Gre…●ARGON — gemini-4-argon is generally available as of 10/13, aimed at complex coding, agents, math and science●DEEP RES — deep-research-pro-preview-12-2025 shuts down 10/23, 6 days left. Move to deep-research-preview-04-2026●NANO — gemini-nano-banana-2.1 is GA (10/06). gemini-3.1-flash-image is deprecated with no shutdown date yet●MCP — One write-up cut an 85,000-token tool result to 310 tokens by wrapping it in an ADK FunctionTool●FLASH — gemini-3.7-flash and 3.5-flash now route automatically to newer Flash models (10/08)●NEW — Stop Re-Reading Every Crash Report: Gre…
Articles/Advanced
⬡ Advanced/2026-06-01Advanced

Trimming Gemini Embeddings from 3072 to 768 Dimensions: A Matryoshka Approach to Cutting Vector DB Cost and Latency

gemini-embedding-001 returns 3072-dimensional vectors, but thanks to Matryoshka representation you can keep only the leading dimensions with almost no quality loss. This is a design for trimming to 768 to cut vector DB storage and latency, including the re-normalization pitfall and coarse-to-fine search code.

Gemini API249Embedding3Vector SearchRAG14Cost Optimization13Production33

✦ Premium Article

Trimming Gemini Embeddings from 3072 to 768 Dimensions: A Matryoshka Approach to Cutting Vector DB Cost and Latency

A vector search feature shows its true nature not the day you build it, but half a year later. Across the apps I have run since 2014, I use embeddings for semantic clustering of store reviews and for categorizing wallpaper images. For the first few thousand records, nothing goes wrong. Then one day, once the count has climbed past 100,000 and toward 300,000, you notice that the vector database bill and the search response-time graph have both crept quietly but unmistakably upward.

When that happened to me, the first thing I reached for was not a faster database, nor a different index type. It was making each individual vector smaller. gemini-embedding-001 returns 3072-dimensional vectors by default. Trim that to 768 and storage drops to roughly a quarter, while distance computation gets lighter almost in proportion to the dimension count. And this is not blind truncation. Thanks to a mechanism called Matryoshka representation, keeping only the leading dimensions preserves most of the meaning.

In this article I want to make that "trim the dimensions" decision something you do with evidence rather than a hunch. We will build it up with working code, all the way through the easily missed step of re-normalization and a two-stage search that filters coarsely with 768 and refines with 3072.

Where 3072 Dimensions Quietly Add Up

The cost of embeddings is often imagined as something that happens only at the moment you call the API. In reality, the costs after generation last far longer.

The clearest one is storage. A single 3072-dimensional vector stored as float32 takes 12,288 bytes, roughly 12 KB. At 300,000 records, the vectors alone come to about 3.5 GB. Many managed vector databases bill on stored volume and resident memory, so that 3.5 GB lands directly on your monthly cost. Drop to 768 and each record is about 3 KB, totaling roughly 0.9 GB. That is about a 75% reduction in storage.

The second is latency. Nearest-neighbor distance computation, whether dot product or cosine similarity, costs work proportional to the dimension count. Even graph-based indexes like HNSW repeat a dimension-sized dot product on every hop, so cutting dimensions to a quarter brings the per-hop work, in principle, down toward a quarter as well. In my own setup, the p95 latency of review search dropped visibly.

The third is memory bandwidth. This one is easy to overlook, but in large-scale search the bandwidth the CPU spends loading vectors becomes the bottleneck itself. The fewer bytes per record, the more candidates you can evaluate in the same window of time.

So 3072 dimensions may be "the best number," but it is not "always the optimal number." The question is how far you can trim without losing quality.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Understand why Matryoshka representation lets the leading dimensions alone carry the meaning, giving you a real basis for trimming 3072 to 768
✦Lock down the L2 re-normalization that is mandatory after truncation, and see in code exactly why skipping it breaks search silently
✦Implement coarse-to-fine search that filters with 768 and re-ranks with 3072, cutting storage and latency while protecting accuracy
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

◈ API / SDK2026-05-15
Three Places Gemini API Embedding Broke on Me — and What Actually Fixed Them
Notes from wiring Gemini API Embedding into an auto-categorization pipeline: INVALID_ARGUMENT from dimension mismatches, 429 rate limits that lost 400 items of work, and weak retrieval quality — plus normalization, checkpointing, and a recall@5 harness that made improvements measurable.
⟐ Dev Tools2026-03-30
Firebase Genkit × Gemini API in Production — Design Choices and Gotchas from a Month of Indie Backend Work
Field notes from a month of running Firebase Genkit and Gemini API behind an indie app review-summarization backend. Flow and Tool design, Cloud Functions vs. Cloud Run, real cost and latency numbers, and seven undocumented gotchas.
⬡ Advanced2026-09-24
I Stopped Asking Gemini 'Is This Right?' and Started Hiding My Answer Instead
Ask Gemini to confirm a label you already chose and it tends to agree. Here is why I compared three ways of asking, chose blind re-classification, how my answer still leaked in, and the routing code that sends only disagreements to a human.