Cosine similarity measures how closely two nonzero vectors point in the same direction. It compares their orientation rather than their length, producing a score between −1 and 1.
In text retrieval, those vectors often represent a search query and a passage of text. Comparing their directions can help a system find passages related to the query, even when they use different words. The score describes a relationship between numerical representations—not a probability that a passage is correct or useful.
Cosine similarity is one of several vector similarity measures. Understanding its calculation, its relationship to other measures, and its limitations makes retrieval scores easier to interpret.
What cosine similarity measures
A vector is an ordered set of numbers. Geometrically, it can be pictured as an arrow with a direction and a length. Cosine similarity measures the cosine of the angle between two such arrows.
- 1: The vectors point in exactly the same direction, although their lengths may differ.
- 0: The vectors are perpendicular, or orthogonal.
- −1: The vectors point in exactly opposite directions.
Between these endpoints, higher scores indicate closer directional alignment.
Consider the vectors [1, 2] and [2, 4]. The second is twice as long as the first, but both point in the same direction. Their cosine similarity is 1.
This scale independence is useful when direction carries the information a system wants to compare and vector length is not part of the intended comparison. It is not automatically the right choice for every representation: some models encode useful information in vector magnitude.
The cosine similarity formula
For two nonzero vectors, A and B:
cosine similarity = (A · B) / (||A|| × ||B||)
The notation has three parts:
- A · B is the dot product: multiply corresponding components, then add the results.
- ||A|| is the Euclidean length of A: square each component, add the squares, and take the square root.
- ||B|| is the Euclidean length of B, calculated the same way.
Dividing the dot product by both lengths removes the effect of their overall scale, leaving a comparison of direction.
The vectors must have the same number of dimensions for this calculation. For the result to carry useful meaning, their coordinates must also belong to a compatible representation space. Two embedding models can produce vectors of the same length without making those vectors meaningfully comparable.
Cosine similarity is undefined for a zero vector. A vector containing only zeros has no direction, and its length would make the denominator zero. Software may reject it or apply a fallback convention; that behavior should not be mistaken for a meaningful similarity result.
A worked example
Take two vectors:
A = [1, 2]B = [2, 1]
1. Calculate the dot product
A · B = (1 × 2) + (2 × 1) = 4
2. Calculate each vector’s length
||A|| = √(1² + 2²) = √5
||B|| = √(2² + 1²) = √5
3. Divide by the product of the lengths
cosine similarity = 4 / (√5 × √5) = 4 / 5 = 0.8
The score shows that the vectors are directionally aligned, but not identical in direction. It does not mean they are “80% the same.”
This example uses two dimensions so the calculation is easy to follow. Text embeddings commonly use hundreds or thousands of dimensions, but the calculation follows the same steps.
How cosine similarity supports text retrieval
Text embeddings represent text as vectors. A model trained for retrieval aims to place useful query–passage pairs in positions that its intended scoring method can recognize.
A typical cosine-based retrieval process works as follows:
- Convert documents or passages into embeddings.
- Store those embeddings with references to the source content.
- Encode the user’s query using the model’s prescribed query-encoding method.
- Search for stored vectors with high cosine similarity to the query vector.
- Return candidate passages, potentially followed by filtering or reranking.
For example, a search for “why does my attic collect moisture?” may retrieve a passage about condensation and roof ventilation without requiring an exact phrase match. Whether it does so depends on the embedding model, the passage, the collection, and the retrieval configuration.
The mathematical measure does not itself understand attic moisture. It compares the representations supplied to it. Semantic usefulness comes from how the model represents language and how the surrounding system selects and evaluates results.
This is why vector search is best understood as a retrieval method rather than an answer-verification method. A passage can be close to a query in embedding space while being outdated, incomplete, or factually wrong.
How to interpret cosine similarity scores
Cosine similarity has a defined mathematical range, but it has no universal relevance threshold. A score of 0.8 may be useful in one system and insufficient in another.
Practical interpretation depends on several factors:
- The embedding model: Models produce different vector geometries and score distributions.
- The task: Finding related topics is different from finding near-duplicates or a passage that directly answers a question.
- The collection: A narrowly focused collection may contain many passages with similar vocabulary and closely aligned embeddings.
- The text being embedded: Passage boundaries, added headings, and truncated text can change the representation.
- The scoring interface: A service may expose raw cosine similarity, a distance, or a transformed score.
A high score is not a confidence percentage
A cosine similarity of 0.92 does not mean there is a 92% probability that a passage answers the query. It is a geometric measurement, not a calibrated probability.
Likewise, the highest-scoring passage is only the closest candidate found under the system’s search procedure. If the collection contains no suitable answer, the system can still return a top result.
Zero and negative scores need careful interpretation
A score of zero means the vectors are orthogonal. It does not establish that two texts have no relationship in ordinary language.
A negative score means the vectors point more than 90 degrees apart. It does not necessarily mean that the texts contradict one another. Statements that make opposing claims can still have high similarity because they discuss the same subject.
Thresholds should come from representative examples
If a system uses a minimum score to accept results, test that threshold against queries and passages representative of the actual task. Include useful matches, clearly irrelevant passages, and difficult cases that discuss the right topic without answering the question.
Evaluate both irrelevant results that pass the threshold and useful results that fall below it. Revisit the threshold when the model, collection, or passage preparation changes.
Cosine similarity, dot product, and Euclidean distance
These measures describe different aspects of the relationship between vectors. Their behavior becomes closely related under specific conditions.
Cosine similarity compares direction
Cosine similarity divides out vector length. Multiplying either vector by a positive number does not change the score.
Dot product reflects direction and magnitude
The dot product does not divide by vector lengths. Both directional alignment and magnitude influence its value.
If both vectors are normalized to unit length—a length of 1—their dot product equals their cosine similarity. This allows systems to calculate cosine similarity through a dot product after normalization.
Euclidean distance measures straight-line separation
Euclidean distance measures how far apart two vector endpoints are. Smaller distances indicate closer points.
For two unit-length vectors:
squared Euclidean distance = 2 − (2 × cosine similarity)
Consequently, exact comparisons of unit-length vectors produce the same ordering whether they use descending cosine similarity, descending dot product, or ascending Euclidean distance. Their numerical scores remain different.
Without normalization, these measures need not produce the same ordering. Follow the embedding model’s documented metric and normalization requirements rather than assuming one measure is universally better.
Cosine distance reverses the score direction
Cosine distance is commonly defined as 1 − cosine similarity. Under this definition, smaller values indicate closer directional alignment. Check the system’s documentation before treating a reported “distance” as interchangeable with a similarity score.
Practical checks for retrieval systems
Before relying on cosine similarity scores, check the conditions surrounding the calculation:
- Use compatible embeddings. Follow the model’s query and document encoding instructions, including required prefixes or separate encoder roles.
- Confirm the metric. Know whether the index returns cosine similarity, cosine distance, dot product, or a transformed score.
- Check normalization. Establish whether the model, application, or database normalizes vectors.
- Handle invalid inputs. Detect zero vectors, dimension mismatches, and non-finite values rather than allowing silent failures.
- Evaluate passage preparation. Chunking for retrieval affects what each vector represents and whether a returned passage retains enough context to be useful.
- Test candidate retrieval. Approximate nearest-neighbor indexes trade some search accuracy for efficiency and may miss candidates an exhaustive comparison would find.
- Preserve exact matching where needed. Part numbers, names, and other precise identifiers may benefit from lexical matching alongside vectors in a hybrid retrieval system.
Cosine similarity answers a precise question: how closely do these vectors align in direction? Used with compatible representations and task-specific evaluation, it can support useful retrieval. Determining whether a result is accurate, sufficiently specific, and appropriate still requires evidence beyond the score.