What is cosine similarity?
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, ranging from -1 to 1 and ignoring magnitude.
Checked
Embedding models place text as points in a high-dimensional space where similar meanings point in similar directions. Cosine similarity measures the angle between two of those vectors: 1 means the same direction, 0 means unrelated, -1 means opposite.
It is the default distance for most text embeddings and the metric behind most RAG retrieval and semantic caches. It is also widely misread.
Cosine, dot product and Euclidean distance
For vectors scaled to length 1, the three metrics agree. OpenAI's embeddings guide notes that its embeddings are normalised to length 1, so cosine similarity can be computed as a plain dot product, and cosine similarity and Euclidean distance give identical rankings. Databases still make the choice explicit.
pgvector
<=> is cosine distance, which is one minus cosine similarity. <#> returns the negative inner product, because Postgres only supports ascending index scans on operators. <-> is Euclidean (L2) distance.
Azure Cosmos DB
Cosine is the default distance function, ranging from -1 to +1, alongside dot product and Euclidean.
Where cosine similarity misleads
Scores are not probabilities
A similarity of 0.82 does not mean 82% relevant. Different models produce different score ranges, so a threshold tuned on one model breaks on the next.
It can be arbitrary
Harald Steck, Chaitanya Ekanadham and Nathan Kallus showed in Is Cosine-Similarity of Embeddings Really About Similarity? (arXiv, 2024-03-08) that for some learned embeddings, cosine values depend on regularisation choices and can be arbitrary.
Magnitude is thrown away
Cosine ignores vector length. For models that encode information in length, dot product is the metric the model was trained for.
Metric mismatch
An index built for one metric and queried with another returns quietly wrong neighbours. The model card states which metric to use.
What it is confused with
Cosine distance
Distance is one minus similarity, so lower is closer. Mixing the two flips a threshold's meaning.
Relevance
Cosine measures closeness in embedding space, not whether a passage answers the question. A reranker judges relevance directly.
How Motiontic's engineers apply it
Motiontic's engineers use the metric each embedding model was trained with, and avoid fixed similarity thresholds unless they are calibrated on labelled data for that exact model. When thresholds are needed, for example in a semantic cache, they are re-tuned on every model change.
Cosine similarity, dot product and Euclidean distance compared
| Cosine similarity | Dot product | Euclidean distance | |
|---|---|---|---|
| Measures | Angle between vectors | Angle and length together | Straight-line distance |
| Range | -1 to 1 | Unbounded | 0 to unbounded |
| Closer means | Higher | Higher | Lower |
| Uses vector length | No | Yes | Yes |
| On normalised vectors | Same ranking as the others | Equals cosine | Same ranking as cosine |
| pgvector operator | <=> (distance) | <#> (negative) | <-> |
Common questions
What is the difference between cosine similarity and dot product?
Dot product mixes direction and length; cosine similarity uses direction only. For vectors normalised to length 1, such as OpenAI embeddings, the two give the same result.
What is a good cosine similarity threshold?
There is no universal value. Score ranges differ by model, so any threshold has to be calibrated on labelled examples for the specific model in use.
What is the difference between cosine similarity and cosine distance?
Cosine distance is one minus cosine similarity. Similarity is higher for closer vectors, distance is lower. pgvector's <=> operator returns distance.
Should embeddings use cosine or Euclidean distance?
Use the metric the model card recommends. For normalised embeddings the rankings are identical, so the choice mainly affects how scores read, not which results come back.
Related
Embeddings
Meaning turned into vectors, so similar things can be found by distance.
Vector database
Storage and nearest-neighbour search for embeddings, now in many shapes.
Matryoshka embeddings
Embeddings that can be cut short, like nested dolls, without starting over.
Semantic cache
Reuse an answer when a new question means the same as an old one.
AI product development
New products and major AI features, built from scratch alongside the client's team.
Running agents that have to hold up?
Describe the agent, the systems it touches and what it must never get wrong. The first call is with a lab engineer, who will say plainly whether it is ready for production, and whether Motiontic is the right team.