Cosine Similarity for Retrieval
Cosine similarity measures the angle between two vectors — the standard distance metric for semantic search.
Cosine Similarity for Retrieval is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Measuring Similarity
How close are two vectors? Three common distance metrics:
- Cosine similarity — angle between vectors
- Dot product — projection
- Euclidean (L2) distance — straight-line distance
For text embeddings, cosine is the default.
Cosine Similarity Formula
For vectors A and B:
cos(A, B) = (A . B) / (|A| * |B|)
Result is between -1 and 1:
- 1 = identical direction
- 0 = orthogonal (unrelated)
- -1 = opposite
In Python with NumPy
Straightforward implementation:
import numpy as np
def cosine_similarity(a, b):
a = np.array(a)
b = np.array(b)
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(cosine_similarity(vec_apple, vec_orange)) # high
print(cosine_similarity(vec_apple, vec_car)) # lowPre-normalised Vectors
If your embeddings are already normalised (OpenAI ones are), cosine = dot product, and you skip the division:
def cosine_normalized(a, b):
return np.dot(a, b) # fasterTop-K Retrieval
To find the K most similar documents to a query, compute similarity to every doc and sort:
def topk(query_vec, doc_vecs, k=5):
scores = [(np.dot(query_vec, v), i) for i, v in enumerate(doc_vecs)]
scores.sort(reverse=True)
return scores[:k]Scaling Up
Naive top-K is O(N) per query — fine for 10k docs, slow at 10M. For large corpora, use approximate nearest neighbour (ANN) indexes: HNSW, IVF, FAISS.
Why Not Euclidean?
L2 distance treats vector LENGTH as meaningful. For embeddings, the direction matters more than the magnitude — that is why cosine wins.
(For normalised vectors, L2 and cosine give the same ranking, so it does not matter.)
Dot vs Cosine
If vectors are already normalised, dot product = cosine and is faster (no division). Most production systems use dot product on normalised vectors.
A Tiny End-to-End Retrieval
docs = ['Python is a programming language', 'Pizza is Italian food', 'The Eiffel Tower is in Paris']
doc_vecs = [embed(d) for d in docs]
query_vec = embed('What is Python?')
scores = [(cosine_similarity(query_vec, v), d) for v, d in zip(doc_vecs, docs)]
scores.sort(reverse=True)
for s, d in scores:
print(f'{s:.3f} {d}')
# 0.83 Python is a programming language
# 0.45 Pizza is Italian food
# 0.41 The Eiffel Tower is in ParisHybrid Retrieval
Combine cosine similarity with keyword search (BM25) for the best of both — semantic + exact-match. Most production systems use hybrid retrieval today.
Threshold-Based Filtering
Drop results below a similarity threshold to avoid feeding the LLM irrelevant context:
results = [r for r in retrieved if r.score > 0.7]Cosine Range
What is the range of cosine similarity values?
Recap
Cosine similarity is the standard metric for embedding retrieval. Combine with hybrid search and ANN for production-scale.
Frequently asked questions
Is the “Cosine Similarity for Retrieval” lesson free?
Yes — the full text of “Cosine Similarity for Retrieval” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Cosine Similarity for Retrieval”?
Cosine similarity measures the angle between two vectors — the standard distance metric for semantic search. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cosine Similarity for Retrieval” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.