DEV Community

VLAD
VLAD

Posted on

Embeddings explained for people who write code

Type "how do I reset my password" into a good search box, and it finds a page titled "account recovery." Zero words in common. Nothing matched on text. So how did it know they mean the same thing?

Your app turned meaning into numbers — and once meaning is numbers, a computer can measure it. That trick is called an embedding. Get this one idea and half the AI buzzwords — "vector search," "cosine similarity," "vector database" — stop being scary.

Prefer to watch? Full walkthrough with the meaning-space animation:

An embedding is a list of numbers

Start with the machine itself. You hand it a piece of text — a word, a sentence, a whole paragraph. It hands back a list of numbers. That list is the embedding. Same text in, same numbers out, every time.

How long is the list? For a common model — OpenAI's text-embedding-3-small — it's 1,536 numbers. That sounds like a lot, until you think of each number as a coordinate.

Two numbers place a point on a map. Three place it in a room. 1,536 place it in a space you can't picture — but the math works exactly the same as the map.

Meaning becomes position

Here's the whole point of that space: the model places text so that similar meaning lands in a similar spot. "cat" and "dog" end up as neighbors. "car" ends up far away.

Nobody wrote that rule. The model learned it — it read a mountain of text and noticed which words keep the same company. Words used the same way get pushed together; words used differently get pushed apart. This is an old idea from linguistics called the distributional hypothesis: a word's meaning is shaped by the words it usually appears next to. Meaning, in this space, is just where you land relative to everything else.

"Cosine similarity" is just an angle

Now the real question in search: are these two things close? You embed the question, you embed every document, and you grab the points nearest the question. But "nearest" means one specific thing — and it's simpler than it sounds.

Draw an arrow from the center of the space out to each point.

  • Two texts with similar meaning? Their arrows point almost the same way — a small angle between them.
  • Unrelated texts? The arrows point off in different directions — a wide angle.

That angle is cosine similarity. A value near 1 means the arrows point the same way (very similar). A value near 0 means they're unrelated. That's the whole comparison — "cosine similarity" is just measuring the angle between two arrows.

In code, it's three lines

Every "AI search" feature you've used is basically this, under a nicer name:

const doc   = embed("account recovery steps")      // → [0.02, -0.91, …] · 1536 numbers
const query = embed("how do I reset my password?")

// cosine: 1 = same direction, 0 = unrelated
const score = cosine(query, doc)                   // ≈ 0.86 — close
Enter fullscreen mode Exit fullscreen mode

Embed your text, embed your query, compare them, keep the closest. (Those scores are illustrative — the real point is high = same direction.)

Trap 1: the numbers only make sense inside one model

These coordinates are only meaningful within a single model. A vector from model A and a vector from model B are not comparable — it's gibberish. Same coordinates, different maps.

So embed your query and your documents with the exact same model, always. Change the model, and you have to re-embed everything you're searching over.

Trap 2: embeddings capture meaning, not exact strings

Embeddings are great at meaning — which makes them bad at things that have no meaning. An error code. A product ID. SKU-4417. There's nothing to place on the meaning-map; it's just an exact string, and pure vector search fumbles it, because nothing is "close in meaning" to a serial number.

That's why real systems run both: keyword search to catch exact strings, vector search to catch meaning, and the results merged. If you've built RAG and watched it miss an obvious error code, this is usually why.

The takeaway

An embedding, start to finish:

  1. Text becomes a list of numbers.
  2. The numbers are coordinates in a meaning-space.
  3. Close in meaning → close in space.
  4. "Similarity" is just the angle between two arrows.

Once you see it as numbers on a map, the buzzwords fall away — and you can actually debug your search instead of trusting it. When a result looks wrong, you're not staring at magic; you're asking a concrete question about distance on a map.

What's the weirdest match your vector search ever returned — the one that made no sense at all? Drop it in the comments — I read them.


I make Vlad's Stack — how the tools you use every day actually work, for people who write code. Full video walkthrough is above.

Top comments (0)