How LLM Embeddings Work
How LLM embeddings work, on a real help-center search running on local models: what an embedding is (768 numbers per text), how a model makes one (tokens, a table of word-piece vectors, 12 layers, an average), how cosine similarity compares two, why semantic search found 'they took my money two times' with no shared words but sent 'my card was charged twice' to the wrong article, how a reader model fixes the final pick, and why switching embedding models breaks search, loudly or silently.

The wrong article
Your help search runs on embeddings, and it sends "my card was charged twice" to the card declined article, not duplicate payments. By the end, it finds the right one.
search "My card was charged twice."0.665 Card declined 0.650 Duplicate payments 0.608 Refund timing
Yet "they took my money two times" shares no words with the duplicate payments article, and that article comes up first. In fairness, card declined does say card. Twice.
search "they took my money two times"0.632 Duplicate payments 0.550 Refund timing 0.535 Refund refused
grep -ciwE 'they|took|my|money|two|times' articles/duplicate.md0
cat articles/declined.md# Card declined Your bank stopped the payment. Check the card number, expiry date and billing address.
Text becomes numbers
How can search match meaning, not words? It turns text into numbers. Give an embedding model a sentence, and back come seven hundred sixty-eight of them.
embed "My card was charged twice."768 numbers: 0.0944 0.0386 -0.1705 0.0094 0.0903 -0.0528 -0.0230 -0.0106 ... 760 more length: 1.0000
Here are the first eight. Each number is a dimension, and the whole list is called a vector. No single number means something you could name, like price or topic. The meaning is in the whole list.
![One embedding, 768 dimensions: diagram of [0.0944, 0.0386, -0.1705,..., 768 numbers], 0.0944 = dimension 1, 0.0386 = dimension 2, -0.1705 = dimension 3, 768 numbers = the whole list: a vector, The meaning is in the whole list](/img/how-llm-embeddings-work/dims.jpg)
Think of map coordinates. Two numbers pin any spot on Earth, and spots close together get close numbers. Each embedding pins a meaning, with seven hundred sixty-eight numbers. Similar meanings, close numbers.
How the model reads
So how does a model turn your sentence into numbers? First it cuts the text into tokens: pieces from a fixed vocabulary, each with its own number. This sentence becomes eight: a start marker, five words, the full stop, and an end marker. A word the vocabulary doesn't hold whole gets split. The word embeddings becomes four pieces. With pieces, a fixed vocabulary can spell almost any word.
tokens "My card was charged twice."[CLS] my card was charged twice . [SEP] 8 tokens: 101 2026 4003 2001 5338 3807 1012 102
tokens "Embeddings turn text into numbers."[CLS] em ##bed ##ding ##s turn text into numbers . [SEP] 11 tokens: 101 7861 8270 4667 2015 2735 3793 2046 3616 1012 102
Ollama, the program running our model, shows what's inside. First, a table: one row for each of its thirty thousand, five hundred twenty-two pieces, with seven hundred sixty-eight numbers in each row. Then twelve layers: twelve rounds of the same kind of math, one after another.
ollama show -v nomic-embed-text | grep -E 'block_count|embedding_length|token_embd.weight'nomic-bert.block_count 12 nomic-bert.embedding_length 768 token_embd.weight F16 [768 30522]
Each token starts as its row from that table. Then the twelve layers pass over the whole sentence. In each layer, every token looks at all the others and adjusts its numbers, so card next to charged ends up different from card next to declined. At the end there are still eight lists. The model averages them into one list. That list is the embedding.

Measuring closeness
We said similar meanings get close numbers. But with seven hundred sixty-eight of them, how do you measure close? Picture each list as an arrow from the center. The model makes every arrow the same length, exactly one unit, so only the direction counts. Take two lists. Multiply their first numbers together, then their second numbers, and so on, and add it all up: that's called the inner product, or here, the cosine similarity. One means the same direction, zero means unrelated. Reset my password, against forgot my login: point six six seven.
similar --show "How do I reset my password?" "I forgot my login."0.0057 x 0.0334 + 0.0126 x -0.0025 + -0.1335 x -0.1239 + ... 765 more pairs = 0.667
Take a guess. Which sits closer to "the bank of the river flooded"? "The bank raised its interest rates", or "water rose over the riverside"? The riverside: point seven one seven, against point five eight eight. Sharing the word bank didn't win. The layers read bank next to river, and moved it toward water.
similar "The bank of the river flooded." "The bank raised its interest rates."0.588
similar "The bank of the river flooded." "Water rose over the riverside."0.717
A map of meaning
So where does "my card was charged twice" land among the articles, and why near the wrong one? First, the app embeds all twelve help articles once, and saves each list in a Postgres database, using pgvector: an add-on that stores vectors and finds the nearest ones fast. Next to each vector, it also saves the name of the model that made it. That column matters later.
indexEmbedded 12 articles with nomic-embed-text: 768 numbers each Saved in Postgres: table articles, columns model and embedding vector(768)
pgvector's own guide agrees: for lists like these, the inner product gives the same answer with fewer calculations.
Squeeze each article's numbers down to just two, keeping neighbors close, like coordinates, so we can draw them, and they settle into neighborhoods. Sign-in trouble over here. Deliveries up here. Money down here, with card declined just outside it.
Your question goes through the same model, and lands right here: between card declined and duplicate payments. A search simply fetches the nearest articles. Card declined wins by a hair: point six six five, against point six five zero.
Why it missed
So why is card declined closer? Test it with a sentence that means something else. Is "my card was charged twice" closer to "my card was charged once", or to "duplicate payment on my account"? To charged once, by far: point nine zero eight, against point six eight zero.
similar "My card was charged twice." "My card was charged once."0.908
similar "My card was charged twice." "Duplicate payment on my account."0.680
Here's the pattern: same topic and same words score high, even when the facts are opposite. "The refund was approved", against "not approved": point eight eight one. The bank pair was two different topics, river and money. So the model captures what a text is about, not what actually happened.

Remember, the embedding is an average of the token lists. Seven of the eight tokens are the same, so one changed word barely moves the average. And the words you share pull too: card pulls toward card declined. Swap card for account, and duplicate payments comes first, but now account locked jumps to second.
tokens "My card was charged once."[CLS] my card was charged once . [SEP] 8 tokens: 101 2026 4003 2001 5338 2320 1012 102
search "My account was charged twice."0.684 Duplicate payments 0.610 Account locked 0.595 Card declined
So don't let the closest match make the final call. Let the search find the neighborhood: the closest three. Then hand those three to something that reads: a small language model, Llama 3.2. It reads all three, and picks duplicate payments. That step is called reranking. Reading is slow, so the reader only gets three; search is fast enough to check thousands.
rerank "My card was charged twice."Search found: 1. Card declined (0.665) 2. Duplicate payments (0.650) 3. Refund timing (0.608) llama3.2:3b read all three and picked: 2. Duplicate payments
Switching models
Your search works now. But someday you'll want a smaller model, because each vector here takes three thousand seventy-six bytes, about four per number. A million articles is about three gigabytes. Half the numbers, half the space.
psql -d help -c "SELECT title, pg_column_size(embedding) AS bytes FROM articles ORDER BY title LIMIT 3"title | bytes -----------------+------- Account locked | 3076 Cancel an order | 3076 Card declined | 3076 (3 rows)
And switching is where search breaks. Say new questions go to all-MiniLM, a smaller model, but the saved articles stay as the old model made them. Search stops with an error: the lists are different lengths, seven hundred sixty-eight numbers against three hundred eighty-four. At least that error is loud.
EMBED_MODEL=all-minilm search "My card was charged twice."ERROR: different vector dimensions 768 and 384
Now try EmbeddingGemma, which also makes seven hundred sixty-eight numbers. No error at all. And for a double charge, the top answer is reset your password, with a score of point zero four five. That's practically zero, a random pick.
EMBED_MODEL=embeddinggemma search "My card was charged twice."0.045 Reset your password 0.014 Duplicate payments 0.013 Lost parcel
Same sentence, two models, seven hundred sixty-eight numbers each. How alike are the two lists? Point zero three three: unrelated. Each model learns its own map of meaning. The first number in Gemma's list and the first number in ours measure completely different things. It's like reading coordinates off the wrong map.

So switching models means embedding every article again with the new one, and checking that saved model name before you search, so both sides always match. Re-indexed with Gemma, the same question finds duplicate payments first.
EMBED_MODEL=embeddinggemma indexEmbedded 12 articles with embeddinggemma: 768 numbers each Saved in Postgres: table articles, columns model and embedding vector(768)
EMBED_MODEL=embeddinggemma search "My card was charged twice."0.576 Duplicate payments 0.233 Card declined 0.216 Two-step sign-in
Recap
So: an embedding is a list of numbers that places a text by what it means. Search fetches the nearest. It hears the topic, not the details, so let a reader make the final call, and use the same model for your articles and your questions.
If you want to see inside more of the AI tools you use, measured in a real lab, subscribing brings the next one.
Every command here ran on local models, on one computer, and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
Demos: Ollama with four local models, Postgres with pgvector, and a made-up shop's twelve help articles. Every command and output was real. Watch the full video on YouTube.