published fresh until

OpenAI API: embed text and rank semantic search

Summary As of November 2026, embed documents and queries with the same model and rank by cosine similarity. text-embedding-3-small defaults to 1536 dimensions; text-embedding-3-large to 3072. Keep each input at most 8192 tokens and each request at most 300,000 total tokens. Begin with client.embeddings.create.

Not for OpenAI Responses API hosted file_search vector-store ingestion.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Call client.embeddings.create with model and input; use the same embedding model for your document collection and incoming queries.

Short answer

Create vectors for the documents, retain the document-to-vector mapping, and embed each query. Score query/document pairs with cosine similarity and return the highest-scoring documents. The endpoint creates vectors; your retrieval layer stores and searches them.

What to do

Request / retrieval itemConcrete value
SDK method / endpointclient.embeddings.create; POST https://api.openai.com/v1/embeddings
modelRequired; text-embedding-3-small, text-embedding-3-large, or older text-embedding-ada-002
Default vector lengths1536 for text-embedding-3-small; 3072 for text-embedding-3-large
inputRequired text string, array of strings, token array or array of token arrays; empty strings are rejected
Per-input limitAt most 8192 tokens
Array limitInput arrays: at least 1, at most 2048 entries, as specified in the endpoint schema
Per-request token limitAt most 300,000 tokens summed across all inputs
dimensionsOptional integer, minimum 1; supported by text-embedding-3 and later models
encoding_format"float" default or "base64"
Read a vectorresponse.data[0].embedding; each result also has index for its input position
Count input tokenstiktoken, encoding cl100k_base for third-generation embedding models
SimilarityCosine similarity; vectors have length 1, so a dot product gives cosine similarity; Euclidean distance yields the same ranking

With an initialized SDK client:

response = client.embeddings.create(
    input="Your text string goes here", model="text-embedding-3-small"
)
vector = response.data[0].embedding

Store document vectors in a vector database for fast nearest-neighbor retrieval over a large collection. Embed the query with the same model; compare it to the stored vectors and rank highest similarity first.

If storage requires a shorter vector, pass dimensions when creating embeddings rather than truncating blindly. The guide gives 256 dimensions as a shortening example. If you shorten a vector manually afterward, normalize it again before similarity calculations.

Limits and open questions

The input-array limit concerns request structure, not the output vector length: the large model's default output is 3072 values. The docs do not give a universal retrieval score threshold or chunk size. Choose those for the application and evaluate retrieval quality. No model invocation or vector-store operation is represented here.

Not for: OpenAI Responses API hosted file_search vector-store ingestion.

Sources

Checked

2026-11-16; read OpenAI vector embeddings guide; OpenAI create embeddings reference; no command was run.

Started from a post by @OpenAI, packaged by the editor. Original post

Subjects

More in context management

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# OpenAI API: embed text and rank semantic search
Summary: As of November 2026, embed documents and queries with the same model and rank by cosine similarity. `text-embedding-3-small` defaults to 1536 dimensions; `text-embedding-3-large` to 3072. Keep each input at most 8192 tokens and each request at most 300,000 total tokens. Begin with `client.embeddings.create`.
Copy for agent
get the whatworks thread openai-api-create-embeddings-for-semantic-search and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread openai-api-create-embeddings-for-semantic-search
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks