Cosine similarity is a score for how much two texts or images resemble each other, once an AI model has turned both into lists of numbers (embeddings). It looks at the mix of those numbers and ignores their size, so a short customer question and a long help article on the same topic can still match. Search tools and chatbots use it to find the best-matching passages.
How is it calculated?
For two vectors A and B:
1 cos(A, B) = (A · B) / (‖A‖ × ‖B‖)
A · B is the dot product: multiply the numbers at each position and add up the results. ‖A‖ is the length of A: the square root of the sum of its squared numbers. scikit-learn describes it as a normalised dot product. The result is the cosine of the angle between the vectors and lies between -1 and 1: 1 when they point the same way, 0 when they are at right angles, and -1 when they point in opposite directions. When no number can be negative, as with word counts, the score stays between 0 and 1 (Wikipedia).
Why not simply measure the distance?
The classic reason comes from search engines. In Introduction to Information Retrieval, Manning, Raghavan and Schütze represent documents as word counts. Two documents with very similar content can then lie far apart simply because one is much longer. Cosine similarity compensates for that difference in length.
For embeddings, Pinecone gives a rule of thumb: use the measure the embedding model was trained with. The all-MiniLM-L6-v2 model, for example, was trained with cosine similarity. Cosine is a poor fit when the size of the vectors carries meaning. For vectors of length 1 the choice matters little. OpenAI normalises its embeddings to length 1, so the cosine equals the plain dot product and ranks results in the same order as Euclidean distance, according to its embeddings guide, which recommends cosine similarity.
How is it used in AI?
An embedding model turns a text into a fixed number of values: 384 for all-MiniLM-L6-v2, and by default 1,536 for OpenAI's text-embedding-3-small and 3,072 for text-embedding-3-large. In semantic search and RAG, the question is turned into a vector with the same model, and the system returns the stored passages with the highest scores (the top k). A vector database avoids comparing the question with every stored vector: indexes such as HNSW (Malkov and Yashunin) find approximate nearest neighbours, with search effort that grows logarithmically with the number of vectors.
Where does it go wrong?
- A score means little on its own. Ethayarajh (2019) found that the word representations inside the language models BERT, ELMo and GPT-2 occupy a narrow cone, and that in GPT-2's last layer two random words have almost perfect cosine similarity on average. A fixed cut-off score therefore has to be tested per model, on your own data.
- Steck et al. (Netflix, 2024) show that for embeddings from regularised linear models, cosine similarity can give arbitrary results that depend on how the model was trained, and they warn against using it blindly.
- The score is only as good as the vectors. With word counts, "Mary is quicker than John" and "John is quicker than Mary" become the same vector, as the IR book points out, so their cosine similarity is 1.
Example
Suppose a webshop scores each text on three topics: shoes, returns and prices. The question "Can I return these shoes?" gets (1, 2, 0). The long returns page gets (3, 6, 0): three times as much text, the same mix. The price list gets (1, 0, 5). The cosine similarity between the question and the returns page is 1.00, and between the question and the price list 0.09. Measured as straight-line distance, the returns page is 4.47 away and the price list 5.39, nearly as far. Real embeddings have hundreds or thousands of dimensions without such names, and the arithmetic is the same.
Sources
- Manning, Raghavan and Schütze: Introduction to Information Retrieval, 6.3.1 Dot productsWeb
- scikit-learn: sklearn.metrics.pairwise.cosine_similarityWeb
- Pinecone: Vector Similarity ExplainedWeb
- OpenAI: Vector embeddings (API guide)Web
- Ethayarajh: How Contextual are Contextualized Word Representations? (2019)arXiv
- Steck et al. (Netflix): Is Cosine-Similarity of Embeddings Really About Similarity? (2024)arXiv
