Cosine Similarity
A ruler that measures how much two directions point the same way
- Cosine similarity is a ruler that measures how much two directions point the same way.
- It doesn't look at how far apart things are. It sets length aside and looks only at angle.
- Length gets set aside so that whichever side is longer doesn't automatically win, whether that length comes from a longer piece of writing or a word repeated more often.
- The more two things point the same way, the closer the value sits to 1; unrelated directions land near 0; opposite directions produce a negative value.
- A high value doesn't mean the same meaning. Opposites used in similar spots can score very close too.
Contents
1The analogy
Two people stand at a fork in the road, each pulling out a compass to fix a direction. One faces northeast and walks a hundred paces; the other faces the same northeast and walks only ten. Ask whether they're heading the same way, and the answer is yes — the number of paces has nothing to do with that question.
Flip it around: if one faces northeast and the other faces south, they aren't heading the same way even if both walked a hundred paces. It's not how far they went that decides the answer — it's which way they were facing.
When the needles line up exactly, that's the closest match. At a right angle, the two directions have nothing to do with each other. Pointing exactly opposite ways, they're opposites. There's no room on this ruler for how many paces were taken.
2In detail
It looks at direction, not how far you walked
Text an AI works with gets turned into a long set of numbers carrying meaning. Think of that set as an arrow pointing some direction. Ask how alike two pieces of text are, and what gets checked is the angle between their two arrows.
Because only the angle matters, doubling an arrow's length doesn't change the answer — stretching an arrow's length doesn't change which way it points. That's what people mean when they say cosine similarity doesn't get thrown off by scale.
There are other rulers that measure straight-line distance instead, and those pull position into the answer along with everything else, so a difference in length shows up directly in the result. There's a reason the direction-based ruler gets reached for more often when working with text.
Why length gets set aside
Picture the same story told in three lines and told in thirty. The content is the same, but the thirty-line arrow ends up much longer. Feed length straight into the calculation and the longer document always floats to the top — search results would fill up with nothing but long pieces of writing.
The same thing happens when one word gets repeated many times. A piece that says the same thing twenty times shouldn't automatically outrank one that says it once.
Looking only at direction makes the problem disappear. A three-line piece and a thirty-line piece carrying the same content point the same way. That's why this ruler is the default for search and recommendations.
What the value tells you
When two directions line up exactly, the value is 1. At a right angle, it's 0. Pointing exactly opposite, it's negative 1. Real documents mostly land somewhere in between.
One thing to watch for: the sets of numbers today's models produce tend to cluster toward one side, so even two unrelated pieces of text often score fairly high rather than near 0. Judging closeness from a single number alone can mislead you.
That's why this ruler works best as a ranking tool — measure a thousand candidates with it and keep the top ten. If you need to draw a line and say "anything above this score counts," that line has to be checked against real data first.
Close doesn't mean the same meaning
Measure "good" against "bad" and the result comes out surprisingly close. The two words mean opposite things, but they're used in almost exactly the same spots — similar neighbors on either side produce similar directions.
Dates and numbers and people's names deserve the same caution. Two different dates can point in very similar directions, so this ruler alone can't tell a March document from a September one.
Negation falls into the same trap. "A refund is available" and "a refund is not available" point in nearly the same direction. This ruler tells you whether two pieces of writing cover the same topic — not whether they say the same thing.
Where it gets used
The most common spot is search. A question becomes an arrow, and it gets lined up against the stored arrows of documents, ranked by closeness. Most of what a meaning-based search hands back was ranked with this ruler.
Recommendation runs the same way. It gathers the direction of things you've spent time on and shows you what points the same way. It's also used to catch near-duplicate documents buried in a pile.
Once there are millions of arrows to compare, checking every single one gets expensive. So candidates get grouped by rough direction first to narrow the field, and only the survivors get measured precisely.
3More precisely
Cosine similarity is the cosine of the angle between two vectors. An angle of zero degrees gives 1, ninety degrees gives 0, and a hundred eighty degrees gives negative 1. Among vectors that have all been scaled to the same length, this value and straight-line distance never flip each other's ranking, which is why some real search systems pick whichever is faster to compute.
The analogy has a limit. A compass needle points somewhere on a flat plane, but a text arrow sits in a space stretched across hundreds or thousands of directions. In that kind of space, two directions picked at random are almost always close to a right angle, which behaves very differently from our everyday sense of direction. A compass's north also means the same thing to everyone, but the space text arrows live in has no fixed reference direction — a value only ever means something between two specific arrows. Switch models and the whole space changes, so an old value and a new one should never be compared side by side either. This ruler isn't a scale that tells you exactly how alike two things are — it's a tool for putting candidates in order.
4Try it yourself
5Common misconceptions
It's easy to think a high similarity score means the same meaning, but actually opposites used in similar spots often score very high too.
It's easy to assume a score of 0.8 always counts as close enough, but actually what counts as a good value shifts by model and by dataset, so it should only be used for ranking.
It's easy to think this measures how far apart two pieces of writing are, but actually it sets distance aside entirely and looks only at direction.
7One-line summary
In shortCosine similarity is a ruler that only measures how much two arrows point the same way, so it isn't thrown off by length — but it will call opposites close too.
Spotted an error or have a better analogy? Suggest an edit · Last updated2026-09-02