DEV Community

Cover image for Day 36: Choosing an Embedding Model for RAG — Quality, Latency, and Cost
Rimsha Kanwal
Rimsha Kanwal

Posted on

Day 36: Choosing an Embedding Model for RAG — Quality, Latency, and Cost

When developing a Retrieval-Augmented Generation (RAG) system, one of the most important decisions is selecting the right embedding model.

It is tempting to choose a model based on its leaderboard ranking, but a model that performs well on public benchmarks may not perform best on your application-specific data. Public benchmarks are useful for narrowing down candidates, while production decisions should be based on controlled evaluation against your own corpus, queries, and retrieval pipeline.

For production systems, embedding-model selection should balance three factors:

  • Retrieval quality
  • Latency and scalability
  • Storage, inference, and operational cost

1. Comparing Embedding Models

Several embedding models are available for different deployment requirements.

Model Default dimensions Deployment
all-MiniLM-L6-v2 384 Local
BGE-small-en-v1.5 384 Local
BGE-base-en-v1.5 768 Local
BGE-M3 1,024 Local
text-embedding-3-small 1,536 API
text-embedding-3-large 3,072 API

These are default embedding dimensions. Some models support dimensionality reduction, which can reduce storage and search cost, though it may slightly affect retrieval quality.

Each model has different computational requirements, language capabilities, licensing constraints, and deployment considerations. Higher dimensionality does not necessarily produce better retrieval quality; it generally increases vector storage, memory use, indexing cost, and sometimes inference cost.

2. Measuring Retrieval Quality

Rather than relying entirely on benchmark rankings, evaluate embedding models on a representative dataset from your application.

Use realistic user queries, representative documents, domain-specific terminology, and failure cases. A small but well-labeled evaluation set is often more valuable than a large generic benchmark that does not reflect your workload.

Common retrieval evaluation metrics include:

  • Recall@K: The proportion of relevant documents retrieved within the top K results.
  • Precision@K: The proportion of retrieved results that are relevant.
  • MRR: Measures how highly the first relevant document is ranked.
  • nDCG@K: Evaluates ranking quality while considering document relevance and position.

For RAG, prioritize Recall@K when a reranker or generation step can reorder or use retrieved candidates. If a relevant chunk is not retrieved within the top K results, downstream components usually cannot recover it.

Use a controlled evaluation protocol

Keep the following constant across all candidate models:

  • Document corpus
  • Chunking strategy and chunk overlap
  • Query set
  • Embedding normalization
  • Distance metric
  • Vector index configuration
  • Top-K retrieval setting
  • Reranking configuration, if used

Report results across multiple runs where possible, especially when the evaluation set is small. A difference of 0.01 or 0.02 in Recall@5 may not be statistically meaningful.

3. Measuring Latency

An embedding model with better retrieval accuracy may require more computation, larger vectors, or more expensive infrastructure.

For a production RAG system, measure:

  • Query embedding latency
  • Vector search latency
  • Reranking latency, if applicable
  • End-to-end retrieval latency
  • P50 and P95 latency
  • Throughput under concurrent requests
  • Performance at production document scale

Do not treat embedding latency as end-to-end retrieval latency. A complete retrieval path often includes:

End-to-end retrieval latency =
query embedding latency
+ network and queueing latency
+ vector search latency
+ optional reranking latency
Enter fullscreen mode Exit fullscreen mode

P50 latency describes the typical user experience, while P95 latency helps identify tail-latency issues that can affect reliability under load.

Measuring embedding latency

import time
import numpy as np
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")

query = "How does retrieval-augmented generation work?"

# Warm-up
model.encode(query)

latencies = []

for _ in range(100):
    start = time.perf_counter()
    model.encode(query)
    elapsed = (time.perf_counter() - start) * 1000
    latencies.append(elapsed)

print("P50:", np.percentile(latencies, 50), "ms")
print("P95:", np.percentile(latencies, 95), "ms")
Enter fullscreen mode Exit fullscreen mode

This example measures single-query embedding latency, not complete retrieval latency. Actual results depend on hardware, runtime configuration, model size, input length, batch size, and workload.

For batch embedding, tune batch_size on a subset of your data. The optimal batch size depends on the available hardware, model size, precision, and input length.

4. Understanding Storage and Operational Cost

Embedding dimensions directly affect vector storage requirements.

The basic formula is:

Storage = Number of vectors × Dimensions × Bytes
Enter fullscreen mode Exit fullscreen mode

For one million float32 vectors:

Dimensions Raw vector storage
384 1.54 GB
768 3.07 GB
1,024 4.10 GB
1,536 6.14 GB
3,072 12.29 GB

These figures exclude indexing structures, metadata, replication, backups, and vector-database overhead.

For larger knowledge bases, embedding dimension can materially affect infrastructure cost. Hosted embedding models introduce API usage costs, while locally deployed models require hardware, maintenance, monitoring, and compute capacity.

When comparing costs, include:

  • API token or inference cost
  • GPU or CPU capacity
  • Vector storage
  • Index memory
  • Replication and backup costs
  • Reranking cost
  • Operational engineering effort

5. Making a Production Decision

Consider the following hypothetical benchmark:

Model Recall@5 P95 latency Relative cost
Model A 0.78 48 ms Low
Model B 0.86 72 ms Medium
Model C 0.88 145 ms High

Suppose the application requires:

  • Minimum Recall@5 of 0.85
  • P95 end-to-end retrieval latency below 100 ms
  • An operational budget constraint

Model B would be the more suitable choice, assuming it also satisfies the cost requirement. Model C provides slightly better retrieval quality but exceeds the latency requirement.

A practical decision rule is:

Choose the lowest-cost model that meets:
1. Minimum retrieval-quality threshold
2. Maximum P95 end-to-end latency
3. Operational-budget constraint
4. Deployment and compliance constraints
Enter fullscreen mode Exit fullscreen mode

If several models meet every constraint, choose the best quality-cost trade-off rather than automatically selecting the model with the highest raw score.

These values are illustrative, not measured benchmark results.

6. Beyond Embedding Model Selection

Changing the embedding model is not always the most effective way to improve retrieval quality.

Other approaches worth evaluating include:

  • Improving document chunking and overlap
  • Adding metadata filtering
  • Combining dense retrieval with BM25
  • Using cross-encoder reranking
  • Improving document quality, structure, and freshness
  • Fine-tuning or adapting an embedding model to domain-specific language

Evaluate each improvement separately so you can understand its actual contribution to the retrieval pipeline. A reranker, for example, may improve final ranking while adding latency and cost; it should be measured as part of the complete system.

Final Thoughts

Public benchmarks such as MTEB provide a useful starting point for comparing embedding models, but they should not be the sole basis for a production decision.

A practical engineering approach is to:

  1. Establish a baseline with your current retrieval pipeline.
  2. Select a small set of candidate models.
  3. Evaluate them on representative application data under controlled conditions.
  4. Measure retrieval quality, latency, throughput, and cost.
  5. Validate the leading option with production-like traffic or shadow evaluation.
  6. Continue monitoring quality and cost after deployment.

The right embedding model is not necessarily the largest, newest, or highest-ranked model. It is the one that meets your application’s quality, latency, cost, and operational requirements without adding unnecessary complexity.

Top comments (3)

Collapse
 
rimsha_kanwal profile image
Rimsha Kanwal •

Research Papers:

  1. MTEB: Massive Text Embedding Benchmark (2022)
    Research on evaluating embedding models across multiple tasks, datasets, and languages.
    arxiv.org/abs/2210.07316

  2. BEIR: A Heterogeneous Benchmark for Zero-shot Evaluation of Information Retrieval Models (2021)
    Explores retrieval quality, generalization, BM25, dense retrieval, and reranking trade-offs.
    arxiv.org/abs/2104.08663

Collapse
 
ric0c0derlab profile image
Ric0 C0der Lab •

Testing embeddings against a small collection of your own documents seems worth the effort. Generic benchmarks don't always reflect the queries you'll actually run.

Collapse
 
rimsha_kanwal profile image
Rimsha Kanwal •

Absolutely. Testing with your own documents is important because real-world queries often contain domain-specific terminology and context that generic benchmarks may miss. A small, well-designed evaluation set can reveal issues that benchmark scores alone cannot.