Cosine Similarity
Cosine similarity is a metric that measures the cosine of the angle between two vectors in a multi-dimensional space. In AI applications, it quantifies how semantically similar two pieces of content are: a score of 1.0 means the vectors point in identical directions (maximum similarity); 0 means they are perpendicular (no similarity); −1 means they point in opposite directions (maximum dissimilarity). For text embeddings constrained to positive values, scores typically range from 0 to 1.
How It Works
Given two vectors A and B, cosine similarity is:
cos(θ) = (A · B) / (||A|| × ||B||)
The dot product A · B measures alignment; dividing by the product of the vectors' magnitudes normalizes for length. This normalization is the defining property of cosine similarity: it compares direction, not magnitude.
In practice: embed two pieces of text using the same embedding model, compute cosine similarity on the resulting vectors, and the score tells you how closely related the content is in semantic space. Vector databases execute this computation at scale across millions of stored vectors using approximate nearest-neighbor algorithms.
When to Use Cosine Similarity vs. Other Metrics
| Metric | What it measures | When to prefer it |
|---|---|---|
| Cosine similarity | Directional alignment (meaning) | Text/embedding retrieval — length-normalized, stable across document sizes |
| Euclidean distance (L2) | Absolute distance | Image embeddings, cases where magnitude carries information |
| Dot product | Alignment without normalization | When embeddings are already unit-normalized (equivalent to cosine) |
For most NLP and RAG use cases, cosine similarity is the default because text embeddings encode meaning in direction, not magnitude. A short tweet and a long article on the same topic will have similar cosine similarity to a query; Euclidean distance would penalize the difference in vector magnitude caused by length.
Common Use Cases
- Vector search — ranking retrieved documents by cosine similarity to the query vector.
- Semantic search — filtering candidates below a minimum similarity threshold before presenting results.
- Duplicate detection — flagging CRM records with cosine similarity above 0.95 as likely duplicates.
- Hybrid retrieval — cosine scores are combined with BM25 keyword scores via a weighted sum.
Related Terms
Knowlee's Approach
Knowlee uses cosine similarity as the primary relevance signal when retrieving account context from the knowledge graph. Retrieval results are ranked by similarity score, and a minimum threshold is applied to avoid surfacing loosely related context that would dilute the signal passed to the generation model. For how similarity-based retrieval compounds into durable business intelligence, see The Enterprise Knowledge Graph Moat.