What is cosine similarity for embeddings?

Also called cosine distance, cosine similarity score

Definition

Cosine similarity is a measure of how closely two vectors point in the same direction, computed as their dot product divided by the product of their lengths, with values from -1 to 1.

Cosine similarity is a measure of how similar two vectors are based on the angle between them, ignoring their lengths. It is a standard score for comparing embeddings: a retriever embeds a query, computes its cosine similarity with stored document vectors, and returns the highest-scoring ones. A score of 1 means the vectors point the same way, 0 means they are orthogonal, and -1 means they point in opposite directions. Cosine distance, used by vector stores such as pgvector and by semantic chunking, is 1 minus cosine similarity.

How cosine similarity is calculated#

Cosine similarity is the dot product of two vectors divided by the product of their lengths. The dot product multiplies matching components and sums them; each length is the square root of the sum of that vector's squared components. Dividing by the lengths removes magnitude, so only direction affects the score.

cosine_similarity(A, B) = (A · B) / (‖A‖ × ‖B‖)

For A = (1, 2) and B = (2, 4), the dot product is 1×2 + 2×4 = 10, the lengths are √5 and √20, and their product is 10, so the similarity is 1.0. B is twice as long as A, but both point the same way. A vector database computes the score against millions of stored vectors through an approximate nearest neighbor index. PostgreSQL's pgvector exposes cosine distance as the <=> operator and recovers similarity as 1 minus that distance.

Cosine similarity vs dot product vs Euclidean distance#

When vectors are normalized to length 1, cosine similarity equals the dot product and ranks results in the same order as Euclidean distance. The three metrics differ only for vectors of unequal length: the dot product rewards magnitude, Euclidean distance counts differences in magnitude as distance, and cosine similarity ignores magnitude.

For unit vectors the denominator of the cosine formula is 1, which leaves the dot product. Squared Euclidean distance between unit vectors equals 2 minus 2 times their cosine similarity, so sorting by either gives the same ranking. OpenAI's embeddings guide states that its embeddings are normalized to length 1, so cosine similarity can be computed slightly faster with a dot product and cosine and Euclidean distance produce identical rankings. The same guide warns that an embedding shortened by hand must be normalized again.

Metric Formula Range Affected by vector length
Cosine similarity A · B / (‖A‖ × ‖B‖) -1 to 1 No
Dot product Sum of Aᵢ × Bᵢ Unbounded Yes, rewards longer vectors
Euclidean distance Square root of the sum of (Aᵢ − Bᵢ)² 0 and up Yes

In practice, normalize once at indexing time and then use the cheapest inner-product operator. The pgvector documentation recommends inner product for best performance when vectors are normalized to length 1, and the Faiss index guide handles cosine with its inner-product index on vectors normalized beforehand. Use the metric the embedding model was trained with when its documentation names one.

A cosine score also works as a threshold. A semantic cache returns a stored answer only when the similarity between a new query and a cached one passes a cutoff. Tune that cutoff on labeled pairs and re-tune it whenever the embedding model changes, because each model spreads its scores differently. Cosine scores and BM25 scores sit on different scales, which is why hybrid search fuses ranks instead of raw scores.

What is RAG shows where similarity search fits in a retrieval pipeline.

Cite this definition

The AI Engineering Editors. "What is cosine similarity for embeddings?" The AI Engineering, updated Nov 06, 2026. https://theaiengineering.com/glossary/cosine-similarity

Title
What is cosine similarity for embeddings?
Author
The AI Engineering Editors
Publisher
The AI Engineering
Updated
View as Markdown

Part of the RAG and vector search hub · Updated