Cosine similarity

Cosine similarity measures how alike two vectors are by the cosine of the angle between them, from 1 for the same direction to minus 1 for opposite, and is the usual way to compare embeddings.

AI systems turn text, images and other items into embeddings, lists of numbers that can be pictured as arrows in a space with hundreds or thousands of dimensions. Cosine similarity compares two such arrows by the angle between them. It is calculated as the dot product of the two vectors divided by the product of their lengths. Arrows pointing the same way score 1, arrows at right angles score 0 and arrows pointing in opposite directions score minus 1. Because only the angle counts, the lengths of the vectors make no difference.

This is how much AI search works. In a retrieval system, each document is turned into an embedding in advance. When a question arrives, it is embedded too, the system computes the cosine similarity between the question and each document, and the highest scores are passed to the language model as context. OpenAI's documentation recommends cosine similarity for its embeddings and notes that they are normalised to length 1, so a plain dot product gives the same result faster and Euclidean distance gives the same ranking.

The idea is older than modern AI. In information retrieval, documents were represented as counts of the words they contain, and cosine similarity measured how close two documents were in topic regardless of how long they were. Because word counts cannot be negative, those scores run from 0 to 1. A long report and a short note about the same subject can score highly, which is exactly what a search engine wants.

The choice of measure matters when vectors are not normalised. Google's machine learning course explains that the dot product grows with vector length, so in recommendation systems popular items, which tend to have longer embeddings, get pushed up. Cosine similarity ignores that. Euclidean distance, the straight-line gap between the ends of two arrows, becomes equivalent to cosine similarity once all vectors have length 1.

Scores are relative, not absolute. A similarity of 0.8 can mean a close match in one embedding model and a loose one in another, so thresholds have to be tuned for each system. A 2024 paper by Harald Steck and colleagues also showed that for embeddings learned by some regularised linear models, cosine similarities can be arbitrary, and advised checking that the numbers mean what users assume before relying on them.

Common questions

What is a good cosine similarity score?

There is no universal cut-off. Scores near 1 mean the vectors point in almost the same direction, but what counts as similar depends on the embedding model and the data. Teams usually set thresholds by testing on examples where they already know which items should match.

What is the difference between cosine similarity and dot product?

The dot product depends on both the angle between two vectors and their lengths, while cosine similarity depends only on the angle. If all vectors are normalised to length 1, the two give identical results, which is why many systems simply use the faster dot product.

Cosine similarity or Euclidean distance: which should I use?

For normalised embeddings it makes no difference to the ranking, since both put results in the same order. OpenAI recommends cosine similarity for its embeddings and says the choice usually does not matter much. For unnormalised vectors, cosine ignores length while Euclidean distance does not.

Why is cosine similarity used in RAG?

Retrieval-augmented generation needs to find the passages most relevant to a question quickly. Embedding the question and the passages and ranking them by cosine similarity is simple, fast and works well when the embedding model places similar meanings close together. Vector databases are built to run these comparisons at scale.

Finding more

Search for "cosine similarity embeddings", "dot product vs cosine similarity" or "vector similarity search" to see how it is used. Reliable sources include documentation from embedding and vector database providers, university or Google machine learning courses, and papers on arXiv about embedding evaluation. Be careful with tutorials that give fixed thresholds such as 0.7 as if they applied to every model.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.