data-infra
Glossary ↗Cosine Similarity
Cosine similarity is a mathematical measure of how similar two vectors are, calculated as the cosine of the angle between them in high-dimensional space. It ranges from -1 (exactly opposite) to 1 (identical direction), with 0 meaning the vectors are orthogonal (unrelated). It is, by a wide margin, the most common distance metric used to compare embeddings in AI applications — most vector databases default to it, and most embedding models (OpenAI's, Cohere's, open-source Sentence-Transformers) are trained with cosine similarity as the implicit or explicit objective, meaning the embeddings are specifically optimized to make cosine comparisons meaningful. Why it matters for AI/SaaS builders: whenever a product does "find similar documents," "semantic search," "recommend related items," or "detect duplicate support tickets," cosine similarity is almost certainly the calculation running under the hood, and understanding it demystifies otherwise-opaque relevance scores showing up in vector database responses (a Pinecone query result with `score: 0.89` is a cosine similarity value, not a probability or percentage). How it works: given two vectors A and B, cosine similarity = (A · B) / (‖A‖ × ‖B‖) — the dot product of the vectors divided by the product of their magnitudes. Critically, this formula normalizes out vector length/magnitude and only measures direction — two embeddings can have very different magnitudes (e.g., one representing a short sentence, one a long paragraph) but still score a high cosine similarity if they point the same way semantically. This is why cosine similarity tends to outperform raw Euclidean distance for text embeddings, where magnitude often correlates with irrelevant factors like text length rather than meaning. Many vector databases pre-normalize vectors to unit length at insert time, which makes cosine similarity mathematically equivalent to a dot product (faster to compute), explaining why you'll see both `cosine` and `dotproduct` offered as index metric options with near-identical behavior once vectors are normalized. Worked example: an e-commerce SaaS embeds product descriptions to power "customers also viewed" recommendations. "Wireless noise-cancelling headphones" and "Bluetooth over-ear headphones with ANC" produce embeddings with a cosine similarity of ~0.91 — very close, correctly signaling near-duplicate intent despite zero shared exact keywords — while "wireless noise-cancelling headphones" and "stainless steel water bottle" score ~0.12, correctly signaling unrelated products. The recommendation engine surfaces items above a 0.75 threshold as "similar." Choosing that threshold is itself a product decision, not a fixed rule — teams typically tune it empirically against a labeled set of "actually similar" pairs, since the "right" cosine cutoff varies by embedding model and domain (a threshold tuned for product descriptions won't necessarily transfer to a threshold tuned for support-ticket similarity).
Related terms