Lesson 3 of 3 · Embeddings & Semantic Search
Generating Embeddings
An e-commerce company needed to embed their entire product catalog -- 2.3 million product descriptions -- for a recommendation engine. Sending them one at a time would take 26 hours and cost 40% more than necessary. By batching inputs (up to 2,048 texts per request) and using base64 encoding, they processed the entire catalog in 3 hours with a single script running on a $5/month VPS.
This lesson covers the API mechanics: how to make embedding requests, handle batch processing, choose encoding formats, and avoid the common mistakes that waste time and money.
The Endpoint
The simplest possible request:
Input Formats
The input parameter accepts multiple formats:
Single String
Array of Strings (Batch)
Batching is the single most important optimization. Each API call has fixed overhead (HTTP connection, request parsing, response serialization). Sending 100 texts in one batch is dramatically faster than 100 individual requests.
The API accepts up to 2,048 inputs per request, but the total token count across all inputs must stay under the model's limit. In practice, batch sizes of 100-500 texts work well for most content. If your texts are long (500+ words each), use smaller batches to stay under token limits.
Array of Token Arrays
For advanced use cases, you can send pre-tokenized input:
This is useful when you have already tokenized the text for other purposes (token counting, truncation) and want to avoid re-tokenization.
Encoding Format
The encoding_format parameter controls how the embedding vector is returned.
float (default)
Returns an array of floating-point numbers. Human-readable. Larger payload.
base64
Returns the same vector as a base64-encoded string. Smaller payload, faster transmission, but requires decoding.
Batch Processing at Scale
For large datasets (100K+ texts), you need a robust batch processing pipeline with rate limiting, error handling, and progress tracking.
Common Mistakes
Mistake 1: Embedding Empty Strings
Empty strings produce valid embeddings -- but they are meaningless. Always filter out empty or whitespace-only inputs before embedding.
Mistake 2: Not Normalizing Input
The same text with different whitespace or casing can produce slightly different embeddings. Normalize before embedding:
Mistake 3: Ignoring Truncation
Texts longer than 8,192 tokens are silently truncated. For long documents, chunk explicitly and embed each chunk separately rather than letting the API discard content.
Mistake 4: Re-embedding Unchanged Content
Embeddings are deterministic for the same model and input. If the text has not changed, the embedding has not changed. Cache aggressively.
Batch inputs for efficiency -- up to 2,048 per request. Cache embeddings by content hash to avoid redundant API calls. Use base64 encoding for high-volume production systems. Validate and clean inputs before embedding.
Send one text at a time when you have multiple texts to embed. Let long texts silently truncate -- chunk them explicitly. Re-embed content that has not changed. Embed empty strings or whitespace.
Benchmark Embedding Models
beginnerUse ← → to navigate, Space to mark complete