cosine similarity
Cosine similarity is a measure of how closely two vectors point in the same direction, calculated as their dot product divided by the product of their lengths. That division cancels out magnitude, so only orientation counts toward the score.
The result falls between -1 and 1 for vectors that can hold negative components, which is the usual case for neural embeddings. A score of 1 means the two vectors point the same way, 0 means they sit at right angles and share no direction, and -1 means they point opposite ways.
Vectors built only from non-negative values, such as tf-idf term weights, can never be opposed, so their scores stay between 0 and 1. Scaling both vectors to unit length reduces the formula to a plain dot product, which is why many embedding models emit pre-normalized vectors and why a vector database often stores them that way.
Ignoring magnitude is what makes the measure fit text. Two documents on the same topic can differ wildly in length, leaving their vectors at very different sizes but similar directions. Cosine similarity rates such a pair as alike, where Euclidean distance over the same unnormalized vectors would push them apart.
That property made it the default scoring function for semantic search, retrieval-augmented generation (RAG) pipelines, deduplication, and clustering.
The widget below makes that contrast concrete. Rotating vector B away from the fixed vector A sweeps the score across its whole range, while stretching B leaves the score untouched and moves only the dashed Euclidean distance.
Direction alone carries no notion of quality or relevance, though. Scores are comparable only within a single model’s vector space and not across models, and a high score reflects proximity in that space rather than an answer to the query. Retrieval systems pass their top candidates to a reranker for that reason.
Related Resources
Tutorial
Embeddings and Vector Databases With ChromaDB
Learn how to use ChromaDB, an open-source Python vector database, to store embeddings and run semantic search with hands-on examples.
For additional information on related topics, take a look at the following resources:
- Build a Recommendation Engine With Collaborative Filtering (Tutorial)
- Natural Language Processing With spaCy in Python (Tutorial)
- Vector Databases and Embeddings With ChromaDB (Course)
- Natural Language Processing With Python's NLTK Package (Tutorial)
- Embeddings and Vector Databases With ChromaDB (Quiz)
- Natural Language Processing With Python's NLTK Package (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Oct. 10, 2026