Loading ModelMetric Lab
Embeddings · 8 min read

Cosine similarity explained with examples

Cosine similarity measures the angle between two vectors rather than their raw size. That makes it a useful first comparison for embeddings, semantic search, recommendations, and clustering—but only when the vector space and score interpretation have been validated for your task.

What is cosine similarity?

Cosine similarity is the cosine of the angle between two non-zero vectors. It compares direction: vectors pointing the same way score 1, perpendicular vectors score 0, and vectors pointing in opposite directions score −1 when the vector space permits negative values.

For embedding search, a higher score usually means the embedding model considers two pieces of content more similar in meaning. “Usually” matters: score ranges and useful thresholds depend on the model, data, normalization, and task. A score is not a universal probability that two texts match.

What is the cosine-similarity formula?

cosine similarity(A, B) = (A · B) ÷ (||A|| × ||B||) A · B = Σ(Aᵢ × Bᵢ) ||A|| = √Σ(Aᵢ²)

The dot product multiplies matching coordinates and adds them. The norm is the vector’s Euclidean length. Dividing by both norms removes the effect of overall magnitude, leaving the directional relationship.

Cosine similarity is undefined for a zero vector because its norm is zero. In production, reject or investigate empty embeddings instead of converting the division error into a misleading score.

If vectors are already L2-normalized to length 1, cosine similarity equals the dot product. Many vector databases can exploit that equivalence, but check whether your index and query vectors are actually normalized before assuming the score is a dot product.

Worked example: two three-dimensional vectors

Take A = [1, 2, 2] and B = [2, 1, 2]. The vectors have the same length and similar directions, but they are not identical.

QuantityArithmeticResult
Dot product(1 × 2) + (2 × 1) + (2 × 2)8
Norm of A√(1² + 2² + 2²) = √93
Norm of B√(2² + 1² + 2²) = √93
Denominator3 × 39
Cosine similarity8 ÷ 90.8889

The vectors are directionally close, so the score is near 1. The cosine similarity calculator shows the same dot product and norms for any pair of vectors with matching dimensions.

Now scale A to A′ = [10, 20, 20]. Its norm becomes 30 and its dot product with B becomes 80, but 80 ÷ (30 × 3) is still 8 ÷ 9 = 0.8889. Scaling one vector changed its magnitude, not its direction.

How should you interpret a cosine score?

ScoreGeometric meaningPractical reading
1Same directionMaximally aligned in the vector space
0PerpendicularNo directional alignment; not automatically “unrelated”
−1Opposite directionOpposed directions when negative values are meaningful

Embedding models often produce vectors whose useful scores occupy a narrower positive range. Two unrelated documents might score 0.55 in one model and 0.15 in another. A threshold such as 0.8 is therefore not portable across models or datasets.

Choose a threshold from labeled examples or from an evaluation set. For retrieval, measure recall at a chosen number of results and inspect whether the returned chunks answer the question. For duplicate detection, calibrate against pairs that humans label as duplicates. The score supports a decision; it does not define the decision by itself.

When is cosine similarity useful?

Semantic search and RAG. Embed a query and documents in the same embedding space, then rank documents by similarity. The embedding model, chunking policy, metadata filters, and reranker can matter as much as the metric.

Recommendations. Compare user and item vectors to find similar preference directions. If popularity or magnitude carries useful information, cosine may discard a signal that dot product would preserve.

Clustering and deduplication. Cosine can compare high-dimensional representations when vector length is not the main concern. Test it against Euclidean distance or dot product if the model was trained with a different geometry.

A useful rule of thumb is to start with cosine for text embeddings when the provider or model documentation recommends it. That rule fails when the embedding model is explicitly trained for another similarity function, when vectors are not comparable because they come from different models, or when magnitude encodes relevance.

Cosine similarity vs dot product vs Euclidean distance

These metrics answer related but different questions. Cosine asks whether two vectors point in the same direction. Dot product asks for directional alignment while also rewarding larger vector magnitudes. Euclidean distance measures the straight-line gap between coordinates, so both direction and scale affect it.

MetricWhat it emphasizesUseful when
Cosine similarityDirection, with magnitude normalized awayMagnitude is nuisance variation and the embedding space supports cosine
Dot productDirection and vector magnitudeMagnitude carries signal, or vectors are already normalized
Euclidean distanceAbsolute coordinate distanceThe model or clustering method was trained around geometric distance

For unit-normalized vectors, the metrics are mathematically related: the squared Euclidean distance is 2 − 2 × cosine similarity, and the dot product equals cosine similarity. Without normalization, those equivalences disappear. A long vector can have a large dot product with another vector even when its angle is not especially close.

Do not switch metrics just because one produces a more convenient-looking score range. Match the metric used during model training or recommended by the embedding system, and benchmark the full retrieval or ranking task. Index settings also matter: a database configured for inner product is not automatically equivalent to cosine unless vectors are normalized consistently before indexing and querying. Record that choice in your deployment configuration.

How does cosine similarity go wrong?

The most common mistake is comparing embeddings from different models or different preprocessing pipelines. Even if both vectors have the same dimension, their coordinates do not share the same learned meaning. Embed queries and candidates with the same model version and compatible preprocessing.

Another mistake is treating a threshold as a fact about language. Scores depend on the corpus and the model. Build a small labeled set, plot score distributions for positive and negative pairs, and choose a threshold that matches the cost of false matches and missed matches.

Zero vectors and malformed vectors can also quietly damage a pipeline. Check dimension, finiteness, and norm before indexing or comparing. If one side is empty because a document was blank or a service returned an error, fail visibly rather than ranking it as an ordinary result.

Finally, cosine similarity does not understand your business rules. A semantically close chunk can still be unauthorized, stale, or missing a required date. Apply access filters and metadata constraints before or alongside vector ranking, then evaluate the final retrieval behavior.

Similarity checklist: use the same embedding model on both sides, verify dimensions and non-zero norms, normalize only when the system expects it, calibrate thresholds on your own labels, and measure retrieval quality—not just average similarity.

Next steps: calculate and calibrate

Try the cosine similarity calculator with A = [1, 2, 2] and B = [2, 1, 2]. Then compare the score with a small set of real positive and negative pairs. For production retrieval, track the embedding model version, vector dimension, normalization setting, top-k results, and downstream answer quality.

FAQ

What does cosine similarity measure?

Cosine similarity measures the cosine of the angle between two non-zero vectors. It compares direction and largely ignores their overall magnitude.

Can cosine similarity be negative?

Yes. The mathematical range is −1 to 1, although many embedding systems produce mostly positive scores. The useful range depends on the model and data.

Is a cosine score of 0.8 good?

It may be useful, but there is no universal “good” threshold. Calibrate the score on labeled examples from the same embedding model and application.

Does cosine similarity equal dot product?

Only when both vectors are L2-normalized to length 1. Otherwise, dot product also reflects vector magnitude while cosine similarity removes that effect.

Can I compare vectors from different embedding models?

Usually not meaningfully, even when their dimensions match. Compare vectors produced by the same model and compatible preprocessing unless the systems were explicitly aligned.