How is cosine similarity calculated?
Take the dot product of two vectors and divide it by the product of their lengths. The scikit-learn documentation describes this as an L2-normalised dot product: once both vectors are projected onto the unit sphere, their dot product equals the cosine of the angle between them [2]. The score approaches 1 when the vectors point the same way.
A concrete case: the vectors (1, 2) and (2, 4) lie on the same line, the second simply twice as long. Their cosine similarity is 1, while Euclidean distance treats the two points as some way apart.
Why is it used with embeddings?
Google's machine learning course compares three ways of measuring similarity between embeddings. Euclidean distance is the distance between the ends of the vectors and shrinks as similarity grows. Cosine depends only on the angle and ignores vector length. Dot product is the cosine multiplied by the lengths of both vectors [1].
That difference has a practical consequence. Items that appear often in the training data tend to have long embedding vectors, so a dot-product measure can let those popular items skew the results [1]. Because cosine similarity leaves length out, it compares direction alone.
Where else is it used?
It has a long history in information retrieval. scikit-learn notes that cosine similarity is a popular choice for comparing documents represented as tf-idf vectors [2]. If the vectors are already normalised, cosine similarity gives the same result as a plain dot product, and the library points to its faster linear kernel for that case [2].