What Is Cosine Similarity?
Cosine similarity is a measure of how alike two vectors are, calculated as the cosine of the angle between them. It ranges from -1 to 1: a value near 1 means the vectors point in almost the same direction, 0 means they are unrelated, and -1 means they point opposite ways. Because it depends only on direction, it ignores vector length entirely.
- Cosine similarity scores direction, not magnitude — two vectors pointing the same way score 1 no matter how long they are.
- It is the default way AI search compares embeddings: the query’s vector is scored against each passage’s vector, and the highest scores are retrieved.
- The formula is the dot product of the two vectors divided by the product of their lengths.
- OpenAI’s embeddings are normalized to length 1, so their cosine similarity equals a plain dot product and gives the same ranking as Euclidean distance.
How Cosine Similarity Works
Cosine similarity answers a narrow question: do these two vectors point in the same direction? It takes the angle between two vectors and returns its cosine. When the angle is zero — the vectors point exactly the same way — the cosine is 1. When they are perpendicular, meaning unrelated, it is 0. When they point in opposite directions, it is -1. Because only the angle matters, the length of each vector is irrelevant, which is the property that makes cosine similarity well suited to comparing meaning.
That property matters for AI search because a vector embedding encodes meaning as direction in a high-dimensional space. A short passage and a long one about the same subject may have very different vector lengths but nearly the same direction, and cosine similarity correctly scores them as highly similar. This is why it is the default metric for comparing embeddings and for measuring semantic similarity between a query and a candidate passage.
Formula
Cosine similarity of two vectors A and B is their dot product divided by the product of their magnitudes:
A · B Σ (Aᵢ × Bᵢ)
cos(θ) = ─────────────────── = ─────────────────────────
‖A‖ × ‖B‖ √(Σ Aᵢ²) × √(Σ Bᵢ²)
The numerator multiplies the vectors component by component and sums the results. The denominator divides by each vector’s length, which is what strips out magnitude and leaves pure direction. If both vectors are already normalized to length 1, the denominator equals 1 and the whole formula collapses to just the dot product A · B.
Example of Cosine Similarity
OpenAI’s embeddings documentation is a clean worked example of cosine similarity in production. The docs explicitly recommend it: “We recommend cosine similarity” as the way to measure how related two embedding vectors are. They also note a practical shortcut that follows directly from the formula. Because OpenAI embeddings are normalized to length 1, the denominator in the equation is already 1, so cosine similarity can be computed “slightly faster using just a dot product,” and cosine similarity and Euclidean distance “will result in the identical rankings.”
Sentence-BERT shows the same metric doing the heavy lifting in retrieval. In the 2019 SBERT paper, each passage is embedded once, and comparing 10,000 of those embeddings to find the most similar pair takes about 0.01 seconds — because the comparison is nothing more than cosine similarity between precomputed vectors. That is the whole point: embeddings make meaning comparable, and cosine similarity is the cheap arithmetic that does the comparing. Every time an AI answer engine decides which passage best matches a question, a cosine score is what ranks the candidates.
The thing people misread is the sign range. They assume similarity runs 0 to 1 and treat 0.2 as "a bit similar." It isn’t — cosine similarity runs -1 to 1, and for typical text embeddings the interesting action lives in a narrow band up near the top. Real passages are rarely opposite in meaning, so you almost never see negative scores in practice; what separates a strong retrieval match from a weak one is often the difference between 0.82 and 0.71, not between 0.9 and -0.9. If you are eyeballing similarity scores to judge whether your content got matched to a query, calibrate to the distribution the model actually produces, not to an imagined 0-to-100 percentage. The absolute number means little; the ranking against competing passages is what decides retrieval.
Frequently Asked Questions
What is cosine similarity?
Why is cosine similarity used for embeddings?
What is the range of cosine similarity?
Is cosine similarity the same as dot product?
The Bottom Line
Cosine similarity reduces "how alike are these two things" to "how small is the angle between their vectors," scored from -1 to 1. It is the arithmetic that turns embeddings into retrieval: a query vector is compared against every passage vector, and the closest directions win. What matters is not the raw score but where your passage ranks against the others competing for the same query.
Sources
Rank & Cash — the weekly SEO breakdown
One practical teardown a week on ranking in search and getting cited by AI. No fluff.
