---
title: Similarity is an angle: cosine — AI basics, lesson 5
description: Two vectors are similar when they point the same way; cosine similarity measures that, 1 for the same direction and 0 for unrelated, with a toy that ranks live.
url: https://instibrain.ai/guides/ai-basics/similarity-is-an-angle
updated: 2026-11-07
site: https://instibrain.ai/llms.txt
---

lesson 5 of 8 · ai basics

# Similarity is an angle: cosine.

Two vectors are similar when they point the same way. Cosine similarity measures that: 1 is the same direction, 0 is unrelated.

## new words

**Dot product**
Multiply the two vectors number by number, then add the results. One number.

**Length**
The square root of a vector's dot product with itself. [1, 1, 1] has length √3 = 1.73.

**Cosine similarity**
The dot product divided by the two lengths. Always between −1 and 1.

**Top-k**
The k texts with the highest cosine to the question. k is 5 to 20 in practice.

## the lesson

Three word-count vectors over the list invoice, paid, day, bill, settled, month:

```
A = [1, 1, 1, 0, 0, 0]
"Invoices are paid within 30 days"
B = [1, 1, 0, 0, 0, 0]
"The invoice was paid"
C = [0, 0, 0, 1, 1, 1]
"The bill is settled in a month"
```

```
A·B = 1·1 + 1·1 + 1·0 + 0 + 0 + 0 = 2
|A| = √(1+1+1) = 1.732
|B| = √(1+1) = 1.414
cos(A, B) = 2 / (1.732 × 1.414)
= 2 / 2.449 = 0.816
A·C = 0 → cos(A, C) = 0
```

Why the angle and not the distance of lesson 3: a long text has bigger counts than a short one, so distance would call every long document "far". The angle ignores size and keeps direction, which is the meaning.

With real embeddings the values differ by model. Related texts land around 0.7 to 0.9, unrelated ones lower. Only the ranking matters, never the absolute number.

Three vectors from one origin. A and B are 35 degrees apart; A and C, 90.

## try it

Six sentences from an example company handbook, as word-count vectors, so every number is visible. Change the question; the table ranks the sentences again. A real embedding model replaces the counting with 1,536 learned numbers, and the cosine step is the same.

Your question
question  invoice  paid

| Cosine | Sentence | Shared words | Dot | Length |
| 0.58 | Invoices are paid within 30 days of the date we receive them.  finance/payments.md  | invoice, paid | 2 | 2.449 |
| 0.29 | Expense claims need a receipt and are paid with the next salary.  finance/expenses.md  | paid | 1 | 2.449 |
| 0.00 | A supplier bill is settled one month after it arrives.  finance/suppliers.md  | — | 0 | 2.236 |
| 0.00 | Reset your password through the VPN portal, never by email.  it/access.md  | — | 0 | 2.236 |
| 0.00 | Holiday requests go to your lead two weeks ahead.  people/holidays.md  | — | 0 | 2.449 |
| 0.00 | The office is closed on 28 May, Republic Day.  people/calendar.md  | — | 0 | 2.449 |

```
top match: Invoices are paid within 30 days of the date we receive them.
dot = invoice 1×1 + paid 1×1 = 2
|question| = √2 = 1.414
|sentence| = √6 = 2.449
cosine = 2 / (1.414 × 2.449) = 0.577
```

## you should see

- With the question as given: the invoices sentence first at 0.58, the expenses sentence second at 0.29 because it shares "paid", and the supplier sentence at 0.00 though it says the same thing in other words.
- Closing that last gap is the embedding model's whole job.

---

instibrain is made by WebWork AI, Inc. Everyone uses the same models · the difference is what yours knows.
