How RAG actually works, part 3: cosine similarity, the heart of it all
By Guus van de Wal · · AI · 4 min
Cosine similarity is the one formula that makes semantic search work. Derive it, code it, and check it by hand on a three-dimensional toy example.
Part 3 of 10 in How RAG actually works, a hands-on tour of vectors, embeddings and retrieval.
Cosine similarity, the heart of it all
If two vectors point in nearly the same direction, the texts mean nearly the same thing. The standard way to measure "same direction" is cosine similarity: the cosine of the angle between two vectors.
- Pointing the same way → cosine ≈ 1 (very similar)
- Perpendicular → cosine ≈ 0 (unrelated)
- Opposite → cosine ≈ −1 (rare with real text embeddings)
Why the angle and not the straight-line distance? Because direction encodes meaning while length often just encodes things like text length or emphasis. Two passages about the same topic should count as similar even if one is a sentence and one is a paragraph. Comparing direction ignores that noise.
The formula is short. For vectors a and b:
cosine(a, b) = (a · b) / (‖a‖ × ‖b‖)
where a · b is the dot product (multiply matching elements, sum them up) and ‖a‖ is the vector's length (square root of the sum of squares). In code:
function dot(a: number[], b: number[]): number {
let sum = 0;
for (let i = 0; i < a.length; i++) sum += a[i] * b[i];
return sum;
}
function magnitude(a: number[]): number {
return Math.sqrt(dot(a, a));
}
function cosineSimilarity(a: number[], b: number[]): number {
return dot(a, b) / (magnitude(a) * magnitude(b));
}
A worked micro-example with 3-dimensional vectors so you can check it by hand:
const cat = [0.9, 0.1, 0.0];
const kitten = [0.8, 0.2, 0.0];
const airplane = [0.0, 0.1, 0.9];
cosineSimilarity(cat, kitten); // ≈ 0.98 → very similar
cosineSimilarity(cat, airplane); // ≈ 0.09 → basically unrelated
"Cat" and "kitten" point almost the same way; "cat" and "airplane" are nearly perpendicular. That's semantic search in one function. A useful production tip: if you normalise every vector to length 1 when you store it (divide by its magnitude once), then cosine similarity collapses to just the dot product, which is faster and is exactly what vector databases do internally.