Cosine similarity measures how closely two vectors point in the same direction, ignoring their length, by computing the cosine of the angle between them.
Cosine similarity is the dot product of two vectors divided by the product of their magnitudes. The result ranges from 1 for identical direction, through 0 for orthogonal vectors, to -1 for opposite directions.
That direction-only sensitivity is why cosine similarity dominates embedding search. Two documents may differ in length, but if their embedding vectors point in the same direction, they likely carry similar meaning. Magnitude often reflects how strongly the model feels about the content rather than what the content is, so stripping it away isolates semantic orientation.
Think of it like this. Think of two hikers standing on a map. Cosine similarity asks whether they are facing the same direction, regardless of whether one is standing closer to the trailhead.
Each vector is normalized to unit length. The cosine of the angle between them is computed as the dot product of the normalized vectors. For sparse vectors, only overlapping nonzero dimensions contribute to the dot product. Cosine distance is 1 minus cosine similarity for use in nearest-neighbor indexes.
"Cosine similarity ignores magnitude." That is intentional for semantic use cases, but magnitude sometimes carries signal. "Cosine and Euclidean distance are interchangeable." They behave very differently in high dimensions. "Cosine similarity needs centered vectors." Standard embeddings are compared as-is; PCA or normalization is a separate preprocessing choice.
Excellent for semantic similarity and normalized embeddings, but insensitive to magnitude and affected by the curse of dimensionality. Works best when vectors are normalized and the direction encodes the signal of interest.