LLM Foundations
How large language models actually work, from tokens to next-token sampling.
01Tokenize
GPT-2 splits text with byte-pair encoding into pieces from a 50,257-token vocabulary. Common words are one token; rare words break into parts. Click a token to follow it below.
02Embed
Each token id looks up a 768-number vector; a position signal is added so the model knows word order. Showing 12 of 768 dimensions for ␣is.
03Self-attention
Tokens look at the words that matter for meaning, like "is" looking at "capital" and "France".
04Transformer block × 12
Attention moves information between tokens; the MLP transforms each token on its own. GPT-2 small stacks 12 of these blocks, then turns the last token's vector into one score (logit) per vocabulary entry.
05Predict & sample
pi = exp(zi/T) / Σ exp(zj/T). Low T sharpens toward the top choice; high T flattens. Top-k and top-p then drop the tail and renormalise. Top 10 of 50,257 candidates shown.
The capital of France is ▍
Token ids come from the real GPT-2 tokenizer. Attention heads and next-token scores are simplified, illustrative models of GPT-2 small, so the page works instantly with no model download. Inspired by Transformer Explainer ↗, which runs the full model in your browser.