What is Cosine similarity?
Cosine similarity measures how alike two vectors are by the angle between them rather than their length, producing a score from -1 to 1 that is the standard way to compare embeddings in semantic search.
Cosine similarity is the metric most often used to decide how close two embeddings are. It computes the cosine of the angle between two vectors: 1 means they point in exactly the same direction (maximally similar), 0 means they are perpendicular (unrelated), and -1 means opposite. Crucially it ignores magnitude and looks only at direction, which suits text embeddings because the meaning a model encodes lives in the orientation of the vector, not how long it is, so two passages about the same topic score high even if one is much longer. This is why semantic search and RAG rank candidates by cosine similarity (or its close cousin, dot product on normalized vectors): embed the query, embed the documents once in advance, and return the stored vectors with the highest cosine score to the query. Vector databases optimize exactly this nearest-neighbor lookup, usually with approximate algorithms so it stays fast over millions of vectors. A practical detail: if your vectors are normalized to unit length, cosine similarity and dot product become equivalent, which many systems exploit for speed.