ModelRefs / Cosine Similarity — AI Glossary
Cosine Similarity — AI Glossary
A measure of the angle between two vectors, used to score relevance between query and document embeddings in vector search.
Overview
Cosine similarity = dot(a,b)/(|a||b|), ranging from -1 to 1. For normalized embeddings (unit vectors) it equals dot product, enabling efficient GPU computation. Most vector databases use cosine or dot product as the default distance metric for ANN search. Correlated with but not equivalent to semantic similarity—quality depends on the embedding model.
Reference details
| Topic | rag |
|---|---|
| Last reviewed | 2026-06-24 |
Related terms
How it works
- The dot product of two vectors is divided by the product of their magnitudes, which removes length and leaves only direction.
- The result is the cosine of the angle between them, bounded between -1 and 1, so it is comparable across pairs regardless of vector scale.
- Because magnitude is discarded, a long document and a short one about the same topic can score equally — which is usually what retrieval wants.
- If vectors are pre-normalised to unit length the denominator is 1, so the measure reduces to a dot product and the division can be skipped entirely.
- Vector indexes exploit that: they normalise at insert time and compute dot products, which is why the configured metric must match how the embeddings were produced.
Example: Where the normalisation saving comes from
For 768-dimensional vectors, a dot product is 768 multiplications and 767 additions. Cosine adds two magnitude computations — each another 768 multiplications plus a square root — and a division, roughly tripling the arithmetic per comparison. Normalising once at insert time moves that cost off the query path entirely, so a search over a million vectors does a million dot products rather than a million cosine computations. The ranking is identical; only where the work happens changed.
Commonly confused with
Measures the angle between two vectors and ignores their magnitude, which is what makes it the default for embedding search. On vectors already normalised to unit length it ranks identically to a dot product, so the two are often used interchangeably and that is fine — but only under normalisation, and Euclidean distance ranks differently in general. Check which one your index is configured for; mismatching it against the embedding model's training objective quietly degrades recall.
When to use it
Reach for it when:
- Comparing embeddings where topic matters and document length should not.
- Choosing or auditing a vector index's distance metric against how the embeddings were trained.
- Explaining why normalised dot product and cosine are interchangeable in ranking.
Reach for something else when:
- Magnitude carries meaning, such as counts or scores, where it should not be discarded.
- Assuming it is interchangeable with Euclidean distance, which ranks differently on unnormalised vectors.
- As a measure of correctness — a high similarity means close in embedding space, not true.
Continue your research
Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to Cosine Similarity — AI Glossary.
Frequently asked questions
What is Cosine Similarity?
A measure of the angle between two vectors, used to score relevance between query and document embeddings in vector search.
What concepts are related to Cosine Similarity?
Closely related concepts include embedding, ann, dense retrieval.