The machinery inside almost every modern LLM. The key fact that makes it learnable: modern LLMs are mostly the same transformer block stacked dozens of times. GPT, Claude, Gemini, and LLaMA broadly share this design — what separates them is training data, scale, configuration, and post-training, not a fundamentally different architecture. Understand one transformer block and you understand most of the field.

This page is the architecture layer beneath Introduce to LLM. That page answers “what is an LLM and why should a PM care”; this one answers “what is actually happening inside the box.”

The Pipeline

Text goes in, integers come out, and after a stack of identical blocks the model emits one token. Then it does the whole thing again for the next token.

1. Tokenization

Models don’t read text, they read integer IDs. A tokenizer maps your prompt to a sequence of integers from a fixed vocabulary. Tokens are usually subword pieces, not whole words. See Tokenization for the full treatment (subword tradeoffs, BPE vs SentencePiece, the “strawberry” problem, why it drives context cost).

2. Embeddings

Each token ID is just a row index into the embedding matrix — one row per vocabulary entry, each row a long vector (the hidden size, e.g. 4,096 numbers in a 7B-class model). The lookup replaces the integer with its learned vector, the model’s representation of what that token “means.”

The striking property: semantically similar tokens land near each other in this space, with no hard-coding. king sits near queen, Paris near France, and arithmetic like king − man + woman ≈ queen actually works. The geometry carries real semantic structure that emerged purely from learning to predict text. But an embedding alone says nothing about where the token sits in the sequence — dog gets the same vector whether it’s first or fifth.

3. Positional Encoding

Plain attention has no built-in sense of word order, but order changes meaning, so position must be injected into the math.

  • Original (2017): add a sinusoidal pattern (unique per position) to each embedding. Worked, but the embedding then had to carry both meaning and position, and learned absolute positions didn’t generalize past trained lengths.
  • Modern (RoPE, Rotary Position Embeddings, 2021): instead of adding, rotate the Query and Key vectors by an angle that grows with position. At attention time what matters is the difference in rotation between two tokens, which directly encodes how far apart they are. Used in LLaMA, Mistral, Gemma, Qwen. Encodes relative position, generalizes to longer contexts, adds no parameters.

Why "put important context first" works

Even with good positional encoding, models have a documented “lost in the middle” problem (Liu et al. 2023): they use the start and end of long prompts more reliably than the middle. This is the mechanical reason behind prompt-engineering advice to front-load key context or repeat it at the end. It connects directly to Context Window Management Strategies and Context Engineering — the model does not read every part of your context equally well.

4. Attention

The mechanism that names the architecture. Each token produces three learned vectors — Query, Key, Value — and uses them to pull information from the other tokens it’s allowed to see. This is the conceptual heart of the whole thing; see Attention Mechanism for Q/K/V, causal masking, induction heads, the quadratic cost, the KV cache, and GQA.

5. Multi-head Attention

One attention pass gives one view of token relationships. Language has many at once (subject–verb, pronoun–referent, long-range references, local phrasing), so transformers run attention many times in parallel as heads, each in its own smaller learned subspace, then concatenate and mix the results. Heads specialize on their own during training. Covered with attention in Attention Mechanism.

6. Feed-Forward Network (FFN)

After attention mixes information between tokens, the FFN processes each token on its own with no cross-token mixing. It expands the vector to a larger size, applies a non-linearity, then compresses it back.

  • The non-linearity (ReLU → GELU → SwiGLU over the years) is essential: without it, stacked linear layers collapse into a single linear layer no matter how many you add.
  • Most of a dense model’s parameters live here, not in attention. And this is where much of the model’s stored factual/semantic structure sits — specific FFN neurons fire on specific concepts (one on Eiffel-Tower text, one on programming languages, one on past-tense verbs). Methods like ROME can edit a single fact (“Eiffel Tower is in Paris” → “Rome”) with a targeted low-rank edit to one FFN weight matrix, no retraining.
  • Mixture of Experts (MoE): replace the single FFN with many parallel “experts” plus a tiny router that sends each token through only a few. Mixtral 8x7B has 8 experts, activates 2 per token: 46.7B total parameters but only ~12.9B used per token. This is how the largest models grow parameter count without growing inference cost proportionally.

7. Residual Stream + Layer Normalization

Each sub-block’s output is added to the token’s vector rather than replacing it (a residual connection, borrowed from ResNet, 2015). Across 30–100 layers these contributions accumulate into the residual stream — the central object in interpretability research, since every head and FFN reads from it and writes back to it, and the original embeddings keep a direct additive path all the way to the final layers.

Layer normalization keeps that running sum stable (otherwise the numbers explode or collapse and training fails). Modern choices: pre-norm (normalize before each sub-block, easier to train deep) and RMSNorm (drops the mean-shift step, keeps only rescaling — cheaper, nearly as good). Without these two tricks, very deep transformers are simply not trainable.

8. Next-Token Prediction

After the final layer, take the last token’s vector and convert it to one logit (raw score) per vocabulary entry. Softmax turns the logits into a probability distribution. Decoding settings shape the output:

  • Temperature — sharpness/randomness (low = conservative, high = varied)
  • Top-k / Top-p — limit sampling to the most plausible tokens

Pick a token, append it, run the loop again (reusing the KV cache), until an end-of-sequence token or length limit. A whole paragraph is just this loop, one token at a time.

The base model is only ever trained to predict the next token

Factual accuracy, reasoning, conversation, coding, and safety are not direct training objectives. The base model learns one thing — predict the next token over massive text. Everything chat-like comes from post-training — see Post-Training and RLHF for what turns this raw predictor into an assistant. This is the single most clarifying fact about why LLMs behave as they do, and it underpins the failure modes in Introduce to LLM.

Speculative decoding is the main speed trick: a small draft model proposes several tokens ahead, the big model verifies them in parallel, accepting them when they match its own distribution. Same output, much faster.

What Actually Differs Between Models

At this level GPT, Claude, Gemini, and LLaMA sit in the same design space. The differences are:

  1. Trained weights — different data, different scale
  2. Configuration — layer count, vocab size, head count, parameter count, MoE vs dense
  3. Post-training — instruction tuning, RLHF, safety controls

The 2023–2025 “modern transformer” stack converged independently across serious teams on a common recipe: pre-norm, RMSNorm, RoPE, SwiGLU, Grouped-Query Attention, and MoE in the largest models. None invented at once; accumulated over ~5 years on top of the 2017 design. (The same convergence-under-independent-pressure pattern shows up in agent harness design — see Harness Engineering.)

Where It’s Going

The convergence on one architecture across language, vision, audio, and multimodal is historically unusual — every domain used to have its own network type. It may not last: Mamba and other state-space models are credible for very long sequences, and MoE already stretched what “the architecture” means. But the core problems — tokens, embeddings, position, attention, FFN, residual stream + norm, next-token prediction — are the durable parts any sequence model has to solve somehow.