What is cosine similarity?
Cosine similarity measures how closely two vectors point in the same direction, ignoring their length entirely, which makes it one of the most commonly used comparisons for embeddings produced by machine learning models.
How is cosine similarity actually calculated between two vectors?
Cosine similarity is computed by taking the inner product of two vectors and dividing it by the product of their individual lengths, which produces a value based purely on the angle between the two vectors rather than how long either one happens to be. A value close to 1 means the two vectors point in nearly the same direction, a value close to 0 means they’re roughly perpendicular with no particular directional relationship, and a value close to negative 1 means they point in nearly opposite directions.
Why does ignoring magnitude matter so much for embeddings?
Many embedding models are trained so that a vector’s direction, not its length, is what encodes meaning — two text embeddings can point in nearly the same direction and represent very similar content even if one vector happens to have a longer raw magnitude than the other, often for reasons that have nothing to do with meaning, such as differences in the length of the original input text. Euclidean distance, covered elsewhere in this glossary, would treat that magnitude difference as making the two vectors farther apart, penalizing something the embedding model never intended to be meaningful. Cosine similarity sidesteps this entirely by discarding magnitude from the comparison altogether, which is exactly why it tends to be the default choice for this kind of embedding.
How does cosine similarity relate to cosine distance and inner product?
Cosine distance is simply cosine similarity converted into a value that behaves like an ordinary distance, decreasing as vectors become more alike rather than increasing — the two terms are closely related and sometimes used loosely to mean the same underlying comparison. Inner product, covered on its own page in this glossary, is closely related mathematically too: when every vector being compared has already been normalized to the same length, ranking by inner product produces exactly the same ordering as ranking by cosine similarity, which is why many systems normalize vectors once upfront and then use the cheaper inner-product calculation everywhere afterward instead of computing a full cosine similarity for every single comparison.
Cosine similarity is one of the most frequently used comparisons in this site’s coverage of vector search, particularly for text and other learned embeddings. From here, the pages on inner product and on choosing the right distance function for a given kind of data are the most useful next steps for understanding when to reach for cosine similarity versus its alternatives.