Generating Embeddings with text-embedding-3
Use OpenAI text-embedding-3-small/large to convert strings into 1536- or 3072-dim vectors.
Generating Embeddings with text-embedding-3 is a free AI Agents lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
OpenAI Embedding Models
OpenAI offers two production embedding models:
- text-embedding-3-small — 1536 dim, cheap, fast
- text-embedding-3-large — 3072 dim, better quality, slower
Use small for most cases; large only when quality matters more than cost.
Your First Embedding
One line of SDK code:
from openai import OpenAI
client = OpenAI()
resp = client.embeddings.create(
model='text-embedding-3-small',
input='Hello, world!'
)
vector = resp.data[0].embedding
print(len(vector)) # 1536Batch Embedding
The API supports lists — much faster than calling one at a time:
texts = ['cat', 'dog', 'pizza', 'sushi']
resp = client.embeddings.create(
model='text-embedding-3-small',
input=texts
)
vectors = [d.embedding for d in resp.data]
# vectors[0] is for 'cat', vectors[1] for 'dog', etc.Token Limits
Each input has a max token limit (8192 for text-embedding-3). Long documents must be chunked first.
Reducing Dimensions
You can ask for fewer dimensions (Matryoshka embeddings) — useful for storage:
resp = client.embeddings.create(
model='text-embedding-3-large',
input='Hello',
dimensions=512 # default would be 3072
)Cost
As of writing:
- text-embedding-3-small: $0.02 / 1M tokens
- text-embedding-3-large: $0.13 / 1M tokens
Embedding 1M words ~ $0.025 with the small model — essentially free.
Embedding Once, Reusing Forever
Embeddings do not change as you re-embed — you can compute them once and store them. Caching is essential for any production system.
Normalising Vectors
OpenAI embeddings are already L2-normalised (length = 1). This means cosine similarity = dot product, saving compute:
import numpy as np
a = np.array(vec_a)
b = np.array(vec_b)
sim = np.dot(a, b) # since |a| = |b| = 1Async Embedding
For high throughput, use the async client:
from openai import AsyncOpenAI
import asyncio
client = AsyncOpenAI()
async def embed_batch(texts):
resp = await client.embeddings.create(
model='text-embedding-3-small',
input=texts
)
return [d.embedding for d in resp.data]
vectors = asyncio.run(embed_batch(['a', 'b', 'c']))Retry on Failure
Embedding calls fail like any HTTP call. Wrap in retry-with-backoff:
from tenacity import retry, wait_exponential, stop_after_attempt
@retry(wait=wait_exponential(multiplier=1, max=10), stop=stop_after_attempt(5))
def safe_embed(text):
return client.embeddings.create(model='text-embedding-3-small', input=text)Storing Embeddings
Three common storage options:
- SQLite with the
sqlite-vecextension - Postgres with the
pgvectorextension - Dedicated vector DB (Pinecone, Qdrant, Weaviate)
Cost Awareness
Roughly how much does it cost to embed 1 million words with text-embedding-3-small?
Recap
Pick text-embedding-3-small, batch your calls, store the vectors, and you have the foundation of semantic search.
Frequently asked questions
Is the “Generating Embeddings with text-embedding-3” lesson free?
Yes — the full text of “Generating Embeddings with text-embedding-3” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Generating Embeddings with text-embedding-3”?
Use OpenAI text-embedding-3-small/large to convert strings into 1536- or 3072-dim vectors. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generating Embeddings with text-embedding-3” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Embeddings Are (Vector Representations)
- Generating Embeddings with text-embedding-3
- Cosine Similarity for Retrieval
- Embedding Models Compared (OpenAI vs Cohere vs OSS)