The Embedding Engineer selects the right embedding model, designs the embedding pipeline, and ensures production performance - batch sizing, caching, and refresh strategies. Embedding model selection matters: the difference between a poor and a good embedding model on your specific domain can be 10–20 percentage points in retrieval recall.
Model Selection (MTEB Benchmark)
| Model | Dimensions | MTEB avg | Cost per 1M tokens | Best for |
|---|---|---|---|---|
| text-embedding-3-large | 3072 | 64.6 | $0.13 | Best quality, high-stakes retrieval |
| text-embedding-3-small | 1536 | 62.3 | $0.02 | Best cost/quality ratio (recommended) |
| nomic-embed-text-v1.5 | 768 | 62.4 | Free (self-hosted) | Privacy, offline, high volume |
| bge-large-en-v1.5 | 1024 | 63.6 | Free (self-hosted) | Best open-source English quality |
| multilingual-e5-large | 1024 | 58.8 | Free (self-hosted) | Multilingual (100+ languages) |
Production Embedding Pipeline
import OpenAI from 'openai';
import { createHash } from 'crypto';
const openai = new OpenAI();
class EmbeddingService {
private cache = new Map<string, number[]>();
// Cache by content hash - same text always gets same embedding
private cacheKey(text: string): string {
return createHash('sha256').update(text).digest('hex');
}
async embed(texts: string[]): Promise<number[][]> {
const uncached: { index: number; text: string }[] = [];
const results: number[][] = new Array(texts.length);
// Check cache first
texts.forEach((text, i) => {
const key = this.cacheKey(text);
if (this.cache.has(key)) {
results[i] = this.cache.get(key)!;
} else {
uncached.push({ index: i, text });
}
});
if (uncached.length > 0) {
// Batch embed uncached texts - max 2048 inputs per API call
const chunks = [];
for (let i = 0; i < uncached.length; i += 100) {
chunks.push(uncached.slice(i, i + 100));
}
for (const chunk of chunks) {
const response = await openai.embeddings.create({
model: 'text-embedding-3-small',
input: chunk.map(c => c.text),
dimensions: 1536,
});
response.data.forEach((item, j) => {
const { index, text } = chunk[j];
const key = this.cacheKey(text);
this.cache.set(key, item.embedding);
results[index] = item.embedding;
});
}
}
return results;
}
}
Cosine Similarity Search
function cosineSimilarity(a: number[], b: number[]): number {
let dot = 0, magA = 0, magB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
magA += a[i] * a[i];
magB += b[i] * b[i];
}
return dot / (Math.sqrt(magA) * Math.sqrt(magB));
}
// For production: store in pgvector or Pinecone - never do in-memory search on >100k vectors