Foundations
How LLMs Work
A large language model predicts the next token, over and over. This module follows one request through a typical transformer: tokenization, embeddings, attention layers, and sampling. The numbers in the interactive diagrams are small invented examples; providers do not publish the internal values of their models.
What Is a Transformer?0:55 · intermediate How Does Attention Work?0:57 · intermediate What Are Embeddings?0:55 · beginner What Is the KV Cache?0:48 · advanced
The inference pipelineBeginner
Your messages are assembled into one sequence, tokenized into IDs, turned into vectors, transformed by dozens of layers, and converted into a probability for every possible next token. One token is chosen, appended, and the process repeats until a stop condition.
prompt → messages → tokens → IDs → embeddings (+ position)
→ N × [attention → feed-forward] → logits → probabilities
→ sample one token → append → repeatEmbeddingsBeginner
Each token ID selects a row in an embedding table: a vector of a few thousand numbers. Training arranges these vectors so tokens used in similar ways end up near each other.
Self-attentionIntermediate
Attention lets every position gather information from earlier positions. Each token produces a query, a key, and a value vector. A token’s query is compared with every key; the softmax of those scores weights the values it mixes in.
Attention(Q, K, V) = softmax( Q·Kᵀ / √d_k + mask ) · V
Multi-head attention and causal maskingIntermediate
Several attention heads run in parallel, each learning different relationships. In a decoder model a causal mask sets future positions to −∞ before the softmax, so a token can only attend to itself and earlier tokens.
Positional information and RoPEAdvanced
Attention by itself ignores order, so position is added. Rotary Position Embeddings (RoPE) rotate each query and key by an angle that depends on its position; the dot product then depends on the distance between tokens. Many open models use RoPE.
Feed-forward layers, residuals, normalizationAdvanced
After attention, each position passes through a feed-forward network (two linear layers with a non-linearity). Residual connections add each sub-layer’s input to its output, and layer normalization keeps values in a stable range so very deep stacks can train.
Mixture of ExpertsAdvanced
Some models replace the feed-forward block with several “experts” and a router that sends each token to a few of them. Only part of the model runs per token, which lowers compute for a given parameter count. Whether a proprietary model uses MoE is often not disclosed.
Sampling: temperature and top-pIntermediate
The final layer produces a logit for every token in the vocabulary. Softmax turns logits into probabilities. Temperature divides the logits first: below 1 sharpens the distribution, above 1 flattens it, and 0 means always take the top token. Top-p keeps the smallest set of tokens whose probabilities add up to p and samples among them.
p_i = exp(z_i / T) / Σ_j exp(z_j / T)
Prefill, decode, and the KV cacheAdvanced
Generation has two phases. Prefill processes the whole prompt in parallel and stores every layer’s keys and values in the KV cache. Decode then produces one token at a time, reusing the cache so earlier tokens are not recomputed. Prefill time drives time to first token; decode speed drives tokens per second. The KV cache grows with context length, which is one reason long contexts cost more.
Latency, streaming, and quantizationAdvanced
Streaming sends tokens as they are decoded, so users see text before the answer is complete. Quantization stores weights with fewer bits (for example 8 or 4) to save memory and speed up inference, sometimes with a small quality cost.
Key takeaways
- A model generates one token at a time.
- Attention mixes information from earlier tokens.
- Temperature and top-p shape which token is picked.
- The KV cache makes decoding fast and long contexts memory-hungry.