Text embeddings are dense numerical vectors that capture the semantic meaning of text. When you embed two pieces of text that mean similar things, their vectors end up close together in high-dimensional space — even if the words are completely different. This property makes embeddings the foundation for a wide range of applications: semantic search, retrieval-augmented generation, classification, clustering, and recommendation systems.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/openai/openai-cookbook/llms.txt
Use this file to discover all available pages before exploring further.
How embeddings work
The OpenAI embeddings API converts a string of text into a list of floating-point numbers. That list — the embedding vector — encodes the meaning of the input text in a way that mathematical operations can act on. The cosine similarity between two vectors measures how semantically related their source texts are, with values ranging from -1 (opposite meaning) to 1 (identical meaning).Creating an embedding
Install the Python client and set your API key, then callclient.embeddings.create:
input field accepts a string or a list of strings. When you pass a list, the API returns one embedding per item in response.data, which is more efficient than making one request per string.
Available models
OpenAI provides three embedding models. Choose based on the trade-off between performance, dimensionality, and cost.| Model | Dimensions | Max tokens | Best for |
|---|---|---|---|
text-embedding-3-small | 1536 | 8191 | Latency-sensitive applications, high-volume workloads |
text-embedding-3-large | 3072 | 8191 | Highest accuracy, benchmarks, research |
text-embedding-ada-002 | 1536 | 8191 | Legacy workloads already using this model |
For new projects, prefer
text-embedding-3-small or text-embedding-3-large. The text-embedding-3 series outperforms ada-002 on standard benchmarks at lower cost per token.Shortening embeddings
Thetext-embedding-3 models support a dimensions parameter that lets you reduce the output vector length without retraining. Shorter vectors lower storage and retrieval costs while retaining most of the model’s accuracy:
Handling rate limits with exponential backoff
When embedding large datasets, calling the API in a tight loop will trigger rate limits. Use thetenacity library to automatically retry with exponential backoff:
Use cases
Semantic search
Embed a query and a document corpus, then rank documents by cosine similarity to surface the most relevant results — even when the query and document share no words.
Retrieval-Augmented Generation
Retrieve the most relevant chunks from a knowledge base and inject them into a prompt so a model can answer questions grounded in your own data.
Classification
Use embeddings as feature vectors for a classifier such as a random forest or logistic regression. The model learns to separate classes in the embedding space without you writing any feature engineering code.
Clustering
Group similar documents together using k-means or other clustering algorithms on their embedding vectors. Useful for discovering topics in large unstructured corpora.
Recommendations
Find the items most similar to a seed item by embedding all items and retrieving the nearest neighbors. Works for articles, products, support tickets, and more.
Anomaly detection
Flag documents whose embeddings are far from the centroid of their expected cluster. Useful for detecting off-topic content, spam, or unusual user inputs.
Token limits
Eachtext-embedding-3 model accepts up to 8,191 tokens. Inputs longer than this limit will return an error. Use tiktoken to count tokens before embedding:
Next steps
- Semantic text search — build a full search system with cosine similarity ranking
- RAG patterns — retrieve context from your own documents to ground model responses