Cloud Explainers

How LLM Embeddings Work

LLM infrastructure8 min video

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.

How LLM Embeddings Work, the full picture: diagram of Delivery address, Cancel order, Lost parcel, Late delivery, Two-step sign-in, Reset password, Card declined, Refund refused, Returns, Account locked, Duplicate payments, Refund timing
The whole picture, from the video's recap.

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.

your app: help search
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.

your app: help search
search "they took my money two times"0.632  Duplicate payments
0.550  Refund timing
0.535  Refund refused
your app: help search
grep -ciwE 'they|took|my|money|two|times' articles/duplicate.md0
your app: help search
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.

your app: help search
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

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.

your app: help search
tokens "My card was charged twice."[CLS] my card was charged twice . [SEP]
8 tokens: 101 2026 4003 2001 5338 3807 1012 102
your app: help search
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.

the model server
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.

From tokens to one embedding: diagram of 8 tokens, Table rows, 12 layers, Average, 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.

your app: help search
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.

your app: help search
similar "The bank of the river flooded." "The bank raised its interest rates."0.588
your app: help search
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.

your app: help search
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.

your app: help search
similar "My card was charged twice." "My card was charged once."0.908
your app: help search
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.

Same words beat same meaning: chart of charged twice / charged once: 0.908 similarity, charged twice / duplicate payment: 0.68 similarity, refund approved / not approved: 0.881 similarity, river bank / interest rates: 0.588 similarity

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.

your app: help search
tokens "My card was charged once."[CLS] my card was charged once . [SEP]
8 tokens: 101 2026 4003 2001 5338 2320 1012 102
your app: help search
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.

your app: help search
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.

the database
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.

your app: help search
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.

your app: help search
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.

One sentence, two maps: diagram of "charged twice", Our model's map, Gemma's 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.

your app: help search
EMBED_MODEL=embeddinggemma indexEmbedded 12 articles with embeddinggemma: 768 numbers each
Saved in Postgres: table articles, columns model and embedding vector(768)
your app: help search
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.

More videos

Kubernetes10 min

Docker vs Kubernetes: What's the Difference?

HTTP & HTTPS11 min

Every HTTP Status Code You Need to Know

PostgreSQL8 min

How to Fix PostgreSQL 'Relation Does Not Exist' Errors

Git9 min

Git Merge vs Rebase: Which One Should You Use?

APIs10 min

How API Webhooks Work

LLM infrastructure9 min

How LLM Tokens Work

Docker9 min

Docker Containers vs Virtual Machines: What's the Difference?

PostgreSQL9 min

MySQL vs PostgreSQL: Which One Should You Use?

Local LLMs (Ollama)15 min

Ollama: Full Tutorial for Beginners

GitHub Actions15 min

GitHub Actions: Full Tutorial for Beginners

Certificates & PKI (ADCS)8 min

How to Fix Certificate Errors: 'Your Connection Is Not Private'

AWS9 min

How the Amazon Ergo AI Model Works

LLM infrastructure9 min

How to Fix LLM API Rate Limit Errors (429)

LLM infrastructure9 min

RAG vs Fine-Tuning: Which One Should You Use?

Load balancing & Nginx24 min

Nginx: Full Tutorial for Beginners

Go (Golang)5 min

Golang go func(): Starting Goroutines in 5 Minutes

Terraform23 min

Terraform: Full Tutorial for Beginners

Go (Golang)5 min

Golang Generics ([T any]) in 5 Minutes

AWS9 min

How AWS Bedrock Works

Python9 min

How to Fix Python CUDA Memory Errors in PyTorch

Go (Golang)5 min

Golang Type Assertions in 5 Minutes

LLM infrastructure10 min

CPU vs GPU for LLM Inference: What Actually Changes

Google Colab (Jupyter notebooks in the cloud)15 min

Google Colab: Full Tutorial for Beginners

AWS8 min

Amazon Bedrock vs OpenRouter: Which One Should You Use?

Go (Golang)5 min

Golang Channels (chan) in 5 Minutes

Homelab & self-hosting21 min

Tailscale: Full Tutorial for Beginners

LLM infrastructure9 min

How RAG Works

Git11 min

Every Git Setting to Change on Day One

Linux9 min

WSL vs a Linux Virtual Machine: Which One Should You Use?

PostgreSQL9 min

Every PostgreSQL Setting to Change After You Install It

Docker9 min

What Happens When You Install Docker on Ubuntu

Python8 min

How to Fix Python 'Address Already in Use' Errors

Go (Golang)5 min

Golang Interfaces and Mocks in 5 Minutes

Git7 min

How to Fix Git 'Detached HEAD' State

Linux21 min

Linux Firewall with ufw: Full Tutorial for Beginners

Go (Golang)5 min

Golang defer Statements in 5 Minutes

Auth (OAuth, JWT)9 min

How to Fix OAuth 'invalid_grant' Errors

Databases & SQL11 min

The SQL Roadmap for Beginners

Go (Golang)5 min

Golang make() for Slices and Maps in 5 Minutes

Networking7 min

How to Fix Network 'No Route to Host' Errors on Linux

Linux6 min

How to Fix Linux 'Too Many Open Files' Errors

Django (Python web framework)11 min

Every Django Concept a Beginner Needs to Know

Linux24 min

SSH on Linux: Full Tutorial for Beginners

Go (Golang)5 min

Golang for range Loops in 5 Minutes

Databases & SQL10 min

SQL Databases: SQLite vs PostgreSQL

Django (Python web framework)10 min

The Django Roadmap for Beginners

Go (Golang)5 min

Golang Structs in 5 Minutes

Caching & Redis22 min

Redis: Full Tutorial for Beginners

Go (Golang)5 min

Golang context.Context in 5 Minutes

Databases & SQL7 min

How to Fix MySQL Database Error 1045: Access Denied for User

Go (Golang)5 min

Golang Method Receivers in 5 Minutes

Python8 min

How to Fix Python SSL: CERTIFICATE_VERIFY_FAILED Errors

Django (Python web framework)9 min

How to Fix Django Static Files Not Loading in Production

OpenRouter (one API for many AI models)8 min

OpenRouter vs Direct API Keys: Which One Should You Use?

Go (Golang)5 min

Golang if err != nil Checks in 5 Minutes

Go (Golang)8 min

How to Fix Golang 'go.mod File Not Found' Errors

AI agents & MCP22 min

MCP Servers: Full Tutorial for Beginners

Go (Golang)5 min

Golang Error Return Values in 5 Minutes

Python8 min

How to Fix Python ModuleNotFoundError: No Module Named

Go (Golang)5 min

Golang Pointers in 5 Minutes

Kubernetes8 min

How to Fix a Kubernetes Pod Stuck in CrashLoopBackOff

Homelab & self-hosting9 min

How to Fix a Homelab ESXi Host Not Responding

PostgreSQL24 min

PostgreSQL: Full Tutorial for Beginners

Load balancing & Nginx9 min

How a Load Balancer Works with HAProxy

Python9 min

How Python Virtual Environments Work

Homelab & self-hosting9 min

How to Fix Cloudflare Tunnel Errors in a Homelab

APIs9 min

How to Fix API CORS Errors: No 'Access-Control-Allow-Origin'

Auth (OAuth, JWT)9 min

How Authenticator App Login Codes Work (TOTP)

Git22 min

Git and GitHub: Full Tutorial for Beginners

Homelab & self-hosting10 min

How a Homelab Hypervisor Works (VMware ESXi)

Kubernetes9 min

How to Fix Kubernetes ImagePullBackOff Errors

DNS8 min

How to Fix DNS 'Temporary Failure in Name Resolution' on Linux

AWS8 min

How to Fix AWS CLI 'Unable to Locate Credentials'

Homelab & self-hosting10 min

Proxmox vs ESXi for Your Homelab: When Each One Wins

PostgreSQL10 min

PostgreSQL UUID vs Auto Increment: Which Primary Key Should You Use?

Kubernetes21 min

Kubernetes: Full Tutorial for Beginners

Networking9 min

How TCP Sockets Work

PostgreSQL7 min

How to Fix PostgreSQL 'Could Not Connect to Server'

Claude & the Claude Developer Platform9 min

Claude Code Hooks Will Finally Make Sense After This

DevOps & CI/CD9 min

DevOps with Ansible Will Finally Make Sense After This

Networking9 min

How SNMP Network Monitoring Works

Git8 min

How to Fix GitHub 'Permission Denied (publickey)' Errors

Docker21 min

Docker: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Password Authentication Failed'

Linux8 min

Why One Linux Server Handles More Users Than You Think

Networking7 min

How to Fix Network 'Connection Refused' Errors

DevOps & CI/CD9 min

DevOps Pipelines: Jenkins vs GitHub Actions

Linux11 min

Linux Nginx Reverse Proxy: Full Tutorial for Beginners

Networking11 min

Every Networking Term a Beginner Needs in 12 Minutes

Docker6 min

How to Fix Docker 'Permission Denied' on docker.sock

Git7 min

How to Fix Git 'Updates Were Rejected' When You Push

Linux7 min

How to Fix nginx 502 Bad Gateway Errors on Linux

Linux8 min

How to Fix Linux apt 'Could Not Get Lock' Errors (Ubuntu)

Networking9 min

How Email Works on the Internet (SMTP) in 10 Minutes

Python8 min

The Python Roadmap for Beginners

Go (Golang)21 min

Golang REST API with Gin: Full Tutorial for Beginners

Networking10 min

How a WireGuard VPN Network Works

Linux9 min

Linux Makefiles Will Finally Make Sense After This

Auth (OAuth, JWT)9 min

SAML vs OAuth vs OpenID Connect: What's the Difference?

Python7 min

How to Fix Python cron Jobs That Never Run

Linux22 min

Linux Bash Scripting: Full Tutorial for Beginners

Networking10 min

How PXE Network Boot Works

Active Directory9 min

How Active Directory Works

Linux10 min

How to Fix a Linux systemd Service That Won't Start

Linux9 min

Linux Logs: syslog vs journald

Auth (OAuth, JWT)9 min

JWT vs Session Cookies: Which One Should You Use?

Docker10 min

Docker vs Docker Compose: Which One Should You Use?

Docker9 min

How to Fix Docker Compose Containers That Can't Talk to Each Other

Django (Python web framework)22 min

Django Full Tutorial for Beginners

Linux9 min

How Linux systemd Works

Networking8 min

How to Fix 'Windows Cannot Access' Network Share Errors (SMB)

APIs10 min

REST vs gRPC: Which One Should You Use?

Certificates & PKI (ADCS)9 min

How to Fix SSL Certificate Chain Errors (Missing Intermediate)

Python9 min

Every Python Concept for Beginners in 10 Minutes

Databases & SQL9 min

Every SQL Join Type in 10 Minutes

OpenRouter (one API for many AI models)9 min

Why OpenRouter Free Models Hit Their Limit So Fast

Gemini API & Google AI Studio9 min

Why Your Gemini API Free Tier Runs Out So Fast

OpenRouter (one API for many AI models)10 min

OpenRouter Will Finally Make Sense After This

Caching & Redis10 min

How Redis Caching Works

Gemini API & Google AI Studio10 min

Gemini API vs Vertex AI: Which One Should You Use?

Gemini API & Google AI Studio8 min

How to Fix a Leaked Gemini API Key

Go (Golang)9 min

Why Golang Uses Goroutines Instead of Threads

Gemini API & Google AI Studio9 min

How to Fix Gemini API Error 429: Resource Exhausted

Claude & the Claude Developer Platform10 min

Claude Token Pricing Will Finally Make Sense After This

Python11 min

Why Python Web Scraping Returns Nothing (and the Fix)

Python10 min

Python uv vs pip: Which One Should You Use?

Python22 min

Python Automation: Full Tutorial for Beginners

Auth (OAuth, JWT)9 min

How OAuth Login Works

Gemini API & Google AI Studio10 min

Google AI Studio Will Finally Make Sense After This

Docker10 min

What Happens When You Run a Docker Container

Claude & the Claude Developer Platform8 min

How to Fix Claude Code Billing Your API Key Instead of Your Plan

Auth (OAuth, JWT)9 min

Why Passkeys Can't Be Phished Like Passwords

PowerShell10 min

PowerShell vs Bash: Which One Should You Learn?

Linux10 min

The Linux Roadmap for Beginners

Django (Python web framework)9 min

Django vs FastAPI: Which One Should You Use?

Databases & SQL10 min

Why SQLite Is the Most Used Database in the World

Databases & SQL11 min

How a NoSQL Database Like MongoDB Works

Python11 min

How to Fix Python pip error: externally-managed-environment

Databases & SQL9 min

How to Fix MySQL Too Many Connections Database Errors

Go (Golang)9 min

Golang Concurrency Will Finally Make Sense After This

AWS10 min

How Amazon S3 Works

Kubernetes10 min

How Kubernetes Works

Git10 min

How Git Works

Docker10 min

How to Fix Docker Port Already Allocated Errors

PostgreSQL10 min

How PostgreSQL Works

Git8 min

How to Fix Git Merge Conflicts

Linux9 min

How to Fix No Space Left on Device in Linux

Networking10 min

How the Internet Works

Databases & SQL9 min

SQL vs NoSQL: When Each One Wins

Auth (OAuth, JWT)8 min

How to Fix JWT Token Expired Errors (401 Unauthorized)

Claude & the Claude Developer Platform9 min

Claude API vs Claude Pro: Why Your Subscription Doesn't Include It

AI agent skills9 min

Why AI Agent Skills Beat Giant Prompts

Linux10 min

Linux Cron Jobs Will Finally Make Sense After This

Cybersecurity10 min

Security Logs Will Finally Make Sense After This

Cybersecurity8 min

How to Fix a Security Leak: An API Key Pushed to GitHub

DNS9 min

DNS Will Finally Make Sense After This

Cybersecurity9 min

Never Ignore a Security Update

Auth (OAuth, JWT)9 min

Two-Factor Authentication Will Finally Make Sense

Networking8 min

Never Expose a Database to the Internet

Cybersecurity10 min

SSH Keys vs Passwords: Which Is Safer for Security?

Linux10 min

The 5 Levels of Linux Boot: From Power On to Login

Networking12 min

The 5 Levels of Network Firewalls

Cybersecurity10 min

Password Security: Why Hackers Crack Weak Passwords

Linux9 min

Why Your Linux Server Runs Out of Memory

Networking10 min

HTTP vs HTTPS: What Actually Changes on Your Network

Linux12 min

Every Linux File Permission You Need to Know

Linux10 min

Linux Processes vs Threads: When Each One Wins

Networking11 min

The OSI Model Will Finally Make Sense After This

Git11 min

The 7 Levels of Git

Networking10 min

Every Network Troubleshooting Command You Need

Cybersecurity10 min

Hashing vs Encryption in Cybersecurity: When Each One Wins

Networking11 min

Why the Internet Ran Out of IP Addresses

Linux11 min

Linux Package Managers: apt vs Snap vs Flatpak

Python9 min

Why Python Is Slow (and When It Doesn't Matter)

Networking11 min

TCP vs UDP: When Each One Wins

Linux8 min

Stop Running Linux as Root (Do This Instead)

Linux11 min

Linux Users and Groups Will Finally Make Sense

Cybersecurity9 min

Why Phishing Beats Good Security (and How to Spot It)

Docker12 min

The 7 Levels of Docker

AI agents & MCP12 min

AI Agents Will Finally Make Sense After This

Git8 min

Stop Using git push --force (Use This Instead)

AI agent skills10 min

Agent Skills vs MCP: When Each One Wins

Networking12 min

Networking Will Finally Make Sense After This

Linux12 min

Every Linux Command You Actually Need

Cybersecurity12 min

The 7 Levels of Cybersecurity

System design12 min

Every System Design Concept You Need, in One Diagram

PostgreSQL10 min

Why PostgreSQL Ignores Your Index