OpenAI Embeddings: Basic Usage in Python
Obtain vector representations of text via the OpenAI API. We cover the text-embedding-3 models, the dimensions parameter, prices and limits, retries (built into the SDK and via tenacity), and batch processing.
How do you create an embedding with the OpenAI API?
Call client.embeddings.create(model="text-embedding-3-small", input=text) and read response.data[0].embedding: a list of 1536 floats. Pass a list of strings to embed a batch in one request. OpenAI returns vectors normalized to length 1, so a plain dot product gives cosine similarity. Cache each vector: you pay per input token, so each text should be embedded once.
OpenAI Embeddings: Complete Guide
8 lessons on the OpenAI embeddings cookbook: semantic and code search, reranking, classification, clustering, Q&A and recommendations. Each lesson has a coding task checked by AI and a quiz.
The rest of this lesson opens after a free sign-up
Sign up with email or Google: the first two lessons of every course stay open for good, and the whole catalog is open for 7 days. No card needed.
text-embedding-3-small vs text-embedding-3-large
Numbers from the OpenAI embeddings guide. Usage is priced per input token; “pages per dollar” assumes about 800 tokens per page.
| Model | Default dimensions | Max input, tokens | ≈ Pages per $1 | MTEB |
|---|---|---|---|---|
| text-embedding-3-small | 1536 | 8192 | 62,500 | 62.3% |
| text-embedding-3-large | 3072 | 8192 | 9,615 | 64.6% |
| text-embedding-ada-002 (older) | 1536 | 8192 | 12,500 | 61.0% |
The embedding workflow from the lesson
- Split the textUp to 8192 tokens per input; count with tiktoken (cl100k_base)
- Embed in batchesA list of strings in input, retry with exponential backoff
- Cache the vectorsCompute once, store in Parquet or a vector database
- CompareDot product = cosine similarity on normalized vectors


