What Is an Embedding, and Why Does Every AI App Use One?
What Is an Embedding, and Why Does Every AI App Use One?
The problem
Ask a search box for "cheap laptop" and a page that says "affordable notebook" will not match a single word. Old keyword search misses it. Almost every modern AI app, from chat-with-your-documents tools to recommendation feeds, gets around this with one idea: the embedding.
The idea in one sentence
An embedding turns a piece of text into a list of numbers, chosen so that texts with similar meaning get similar numbers.
That list is called a vector. You can think of it as a location. Each number is one coordinate. Two texts that mean nearly the same thing land close together. Two texts about unrelated things land far apart.
So "cheap laptop" and "affordable notebook" end up as neighbours, even though they share no words. Search stops being about matching letters and becomes about measuring distance.
Animation: Texts become points. Similar meanings land close together, and search returns the nearest points.
How many numbers?
Far more than the two or three we can picture. OpenAI's text-embedding-3-small model returns 1,536 numbers for each piece of text by default, and text-embedding-3-large returns 3,072. Nobody can picture a space with 1,536 directions, but the maths of distance works the same as on a flat map. You just add up more coordinates.
Where the numbers come from
Nobody writes these numbers by hand. A model learns them by reading a lot of text and being trained so that words or passages used in similar contexts end up with similar vectors.
The classic demonstration is from 2013. Tomas Mikolov and colleagues at Microsoft Research found that word vectors learned this way captured relationships as directions. Take the vector for "king", subtract "man", add "woman", and the result lands very close to "queen". The step from "man" to "woman" pointed in roughly the same direction as the step from "king" to "queen". Nobody told the model about gender or royalty. It picked up the pattern from how the words were used.
Animation: The step from man to woman points the same way as king to queen, so king minus man plus woman lands near queen.
It was not perfect. The same paper's vectors answered almost 40% of a set of grammar analogy questions correctly. But it showed that meaning could live in geometry.
Modern embedding models are much bigger and work on whole sentences and paragraphs, not single words. The principle is the same.
How "close" is measured
The usual measure is cosine similarity. Instead of the straight-line distance between two points, it looks at the angle between their two arrows from the centre. Arrows pointing the same way score near 1. Arrows at right angles score 0.
Animation: Cosine similarity compares the angle between two arrows: same direction scores near 1, a right angle scores 0.
The angle matters more than the length because a long passage and a short one about the same thing should still count as similar. OpenAI's embeddings are scaled to length 1, which makes the angle the only thing that differs.
Why every AI app uses them
Once text is a location, a lot of useful things become simple geometry.
- Search by meaning. Turn every document into a vector once. When a question comes in, turn it into a vector and return the nearest documents.
- Chat with your documents. This is the heart of retrieval-augmented generation, or RAG. The app finds the passages closest to your question and pastes them into the prompt so the model answers from them. More on when that beats retraining in RAG vs fine-tuning.
- Agent memory. An agent can store past notes as vectors and recall the closest ones later. There is a working Python version in Long-Term Agent Memory in Python With Embeddings and NumPy.
- Duplicates and clusters. Two support tickets with nearly identical vectors are probably the same complaint, however differently they are worded.
- Recommendations. If you liked one article, show the ones whose vectors sit nearby.
A tiny example you can run
This uses made-up three-number vectors so the maths is visible. Real ones have hundreds or thousands of numbers.
pythonimport numpy as np def cosine(a, b): return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b))) cheap_laptop = np.array([0.9, 0.1, 0.2]) affordable_laptop = np.array([0.8, 0.2, 0.2]) banana_bread = np.array([0.1, 0.9, 0.7]) print(round(cosine(cheap_laptop, affordable_laptop), 3)) # 0.991 print(round(cosine(cheap_laptop, banana_bread), 3)) # 0.301
What embeddings get wrong
Similar is not the same as correct. The nearest passage to your question is the one that sounds most related, not the one that answers it. A question about a refund policy can pull up a passage about refund fraud.
They learn everything in the text, including bias. If a training corpus links certain jobs to one gender, the vectors will too.
Exact details blur. Product codes, names and numbers often matter more than meaning. That is why many search systems mix embedding search with plain keyword search.
You cannot mix models. Vectors from one embedding model mean nothing to another. Switch models and you have to embed everything again.
The short version
An embedding is a location for meaning. Turn text into numbers so that similar ideas sit close together, then measure the distance. That one trick powers semantic search, RAG, agent memory and recommendations.
Sources
- Tomas Mikolov, Wen-tau Yih and Geoffrey Zweig, "Linguistic Regularities in Continuous Space Word Representations", NAACL 2013: https://aclanthology.org/N13-1090.pdf
- OpenAI, "Vector embeddings" guide, model dimensions: https://developers.openai.com/api/docs/guides/embeddings?site_locale=en
- Qdrant, OpenAI embeddings notes (normalised to length 1, cosine similarity): https://qdrant.tech/documentation/embeddings/openai/
