Modern AI & LLMs Guide 1 of 7 · tap to switch
Tokenization & Word Embeddings
How LLMs turn raw text into subword token IDs and map meaning into geometry where King - Man + Woman lands right on Queen.
When you type "Unbelievable coffee prices!" into ChatGPT, Claude, or Gemini, the neural network inside cannot read letters, words, or emojis directly. Neural networks only do math with numbers!
How do you turn a sentence into numbers cleanly? Memorizing every word and typo in human history would require an impossibly huge dictionary, while feeding one letter at a time stretches short sentences into painfully long chains. Instead, modern models slice text into reusable word pieces, like Lego bricks: common words like " coffee" stay whole as 1 piece, while longer words like "Unbelievable" snap into smaller familiar pieces ("Un" + "believ" + "able"), and every piece gets an integer ID.
This step is called Subword Tokenization, usually built with Byte-Pair Encoding (BPE). It is called Byte-Pair because during setup, the computer scans billions of sentences and repeatedly merges the most frequent side-by-side pairs of characters or pieces until it builds a reusable ~100,000-piece vocabulary.
Click through the 4 text samples below to inspect the exact pieces and IDs the model receives, including the real secret behind the famous “strawberry” riddle!
From BPE Tokenization to Word Embeddings
Wait: token ID 6891 (" coffee") and token ID 6892 could be completely unrelated words! Remember the Categorical Encoding trap? Plain integer IDs carry zero sense of similarity.
To represent meaning, imagine placing every word onto a giant map using coordinates. On a tiny 2-number map, "king" might sit at (6.2, 7.4) right next door to "queen" at (7.8, 6.6), while "cat" sits across town at (-7.4, 6.8) and "Paris" sits far below at (-7.6, -6.8). Real language models use the exact same idea with a much richer map (often 1,536 to 12,288 coordinates per word!) so every shade of meaning has room.
Each token’s list of coordinates is called a Word Embedding, and the giant learned lookup table storing one coordinate row for every token ID is called an Embedding Matrix. During training on trillions of words, tokens appearing in similar contexts naturally drift into tight neighborhoods. To measure how close two words sit on this map, models use Cosine Similarity: how closely two arrows pointing from the center line up in the same direction, from 0% (unrelated) to 100% (same direction).
Click any word dot on the 2D meaning map below (or use the quick-pick buttons) to inspect its 3 closest neighbors!
Doing arithmetic with ideas: King - Man + Woman = Queen
Even more astonishing: a meaning map does not just group similar words together; relationships between concepts turn into consistent directional steps! If stepping right turns "Man" into "Woman", taking that exact same step from "King" lands you right on "Queen".
Pick one of the three classic discoveries below (Royalty, Capital Cities, or Verb Tenses) and walk through Steps 1 -> 2 -> 3 to watch the exact same arrow solve each analogy!
Static vs. Contextual Embeddings: the "bank" test
Now try a tricky puzzle. Where on the meaning map should the word "bank" live? In "I sat peacefully by the river bank," it belongs next to "river", "water", and "shore". In "I deposited cash at the bank," it belongs next to "money", "vault", and "loan".
Early embedding dictionaries like Word2Vec (Mikolov et al., 2013) were Static Embeddings: every spelling got one frozen map pin forever, mashing "river bank" and "money bank" into one awkward midpoint! Modern language models fix this with Contextual Embeddings: the lookup table gives "bank" a starting spot, and then the model reads nearby words (using a spotlight mechanism called Self-Attention, which we will explore visually in the very next guide) to slide "bank" smoothly into the right neighborhood.
The lab below starts on the old frozen map so you can see "bank" stuck in the middle. Switch sentences first, then toggle on Contextual (Reads Neighbors) to watch "bank" glide cleanly to the matching neighborhood!
See Byte-Pair Encoding (BPE), Embedding Matrices & Distributional SemanticsThesis / Reference
1. Byte-Pair Encoding (Sennrich et al., 2016; Radford et al., 2019): Starting from the 256 raw UTF-8 bytes, BPE iteratively counts all adjacent token pairs across a training corpus and merges the single most frequent pair into a brand-new vocabulary symbol until reaching target vocabulary size (typically to ). Because every string decomposes down to UTF-8 bytes in the worst case, Byte-Level BPE achieves zero Out-of-Vocabulary (<UNK>) errors on any language or code snippet.
2. Learned Embedding Lookup Table: An embedding layer stores a trainable parameter matrix . For token index , its initial vector is the row lookup:
Similarity between two embeddings (or full document embeddings inside Vector Databases & HNSW for RAG) is ranked via Cosine Similarity, invariant to vector length:
3. Distributional Hypothesis (Firth, 1957; Mikolov et al., 2013): “You shall know a word by the company it keeps.” Optimizing next-token cross-entropy (or Skip-Gram negative sampling) forces embeddings to factorize Pointwise Mutual Information (Levy & Goldberg, 2014), naturally encoding semantic axes like gender, geography, and tense as linear directions.
See Tokenization & nn.Embedding Lookup in PythonOptional Code
import torch
import torch.nn as nn
# Vocabulary of 50,257 BPE tokens mapped into 768-dim vectors (GPT-2 scale)
vocab_size, d_model = 50_257, 768
embed_table = nn.Embedding(num_embeddings=vocab_size, embedding_dim=d_model)
# Batch of 1 sentence encoded as 6 subword token IDs
token_ids = torch.tensor([[1844, 42910, 489, 6891, 7609, 0]])
vectors = embed_table(token_ids)
print("Embedding tensor shape:", vectors.shape) # torch.Size([1, 6, 768])How did "bank" know to jump straight toward "river" or "money"?
The embedding table gave "bank" a neutral starting coordinate. What exact mathematical spotlight scanned the rest of the sentence, spotted "river" or "cash", and pulled "bank" to the right meaning?
Welcome to the single most famous mechanism in modern AI: Self-Attention Explained.
Quick Intuition Check
Four quick real-world scenarios. No scores, just test your intuition.
Why do modern AI language models slice text using Byte-Pair Encoding (BPE) subword pieces instead of storing every whole English word?
Why did AI language models historically struggle to count how many times the letter 'r' appears in 'strawberry'?
What key advantage do Contextual Embeddings have over classic Static Embeddings like Word2Vec?
Plain token IDs like 6891 and 6892 already tell an AI model that two words have similar meanings, so no coordinate map of meaning is needed.
Common Questions
Why do LLMs use subword tokens (BPE) instead of whole words or single characters?
Why did LLMs famously fail to count the letter 'r' in 'strawberry'?
What is an Embedding in AI and LLMs?
How is an embedding lookup table actually stored inside a Transformer?
What is the difference between Word2Vec and modern LLM embeddings?
Related Visual Guides
Self-Attention & the Attention Mechanism
Understand the exact spotlight powering modern AI: Query, Key, Value dot products, sqrt(d_k) scaling, Multi-Head Attention, and Causal Masks.
Vector Databases & HNSW
Fly inside a 3D multi-layer HNSW skip graph to see how vector databases search millions of embeddings in milliseconds, compare IVF vs. HNSW, and shrink RAM 32x with Product Quantization.
Euclidean Distance & Cosine Similarity
How recommenders, search engines, and AI embeddings measure closeness, and why high dimensions distort everyday geometry.
Written & illustrated by Muhammad Aashir Irshad · Updated December 2026 · Free open-access reference
Cite this guide in a paper, thesis, or course (BibTeX & APA)
@misc{irshad2026tokenizationembeddings,
author = {Irshad, Muhammad Aashir},
title = {Tokenization & Word Embeddings: Interactive Visual Guide},
howpublished = {SeeingML},
year = {2026},
month = {dec},
url = {https://seeingml.com/learn/tokenization-embeddings/},
note = {Interactive visual guide}
} Irshad, M. A. (2026). Tokenization & Word Embeddings: Interactive Visual Guide. SeeingML. https://seeingml.com/learn/tokenization-embeddings/