Transformer Explained: The Architecture Behind Every LLM

⚡ TL;DR: A transformer stacks two things repeatedly: attention, which lets tokens share information, and a feed-forward layer, which processes each token individually. Everything else — tokenisation, embeddings, positional encoding — feeds that stack.

You now have every component. This post assembles them. If you have read attention and embeddings, the transformer will look like something you already half know.

The architecture, top to bottom

Transformer architecture diagram from input text through tokenisation, embeddings, repeated self-attention and feed-forward blocks, to next-token probabilities
The dashed box is one transformer block. GPT-3 stacks 96 of them; smaller models use 12 to 24.

Text enters, is tokenised, becomes embeddings, passes through N identical blocks, and emerges as a probability for every possible next token.

Positional encoding: why order needs adding back

Attention has a property that is easy to miss: it is order-blind. It computes a weighted average, and averages do not care about sequence. Shuffle the input and attention produces the same result.

✅ Why that matters: “the dog bit the man” and “the man bit the dog” contain identical tokens. Without positional information a transformer cannot distinguish them. So a position signal is added to each embedding before the first block — that is positional encoding, and it is the only place order enters the model.

Inside one transformer block

Two sub-layers, and they do genuinely different jobs.

Sub-layerWhat it doesTokens interact?
Self-attentionEach token gathers information from the othersYes — this is the only place they do
Feed-forwardProcesses each token’s vector independentlyNo — same small network applied to each

A useful way to hold it: attention moves information sideways between tokens; the feed-forward layer thinks about each token on its own. Alternating the two, dozens of times, is the whole design.

The feed-forward part is an ordinary two-layer neural network — the exact structure covered earlier on this site, with a non-linear activation in the middle. It also holds most of the model’s parameters.

The two details that make deep stacks trainable

Residual connections. Each sub-layer’s output is added to its input rather than replacing it. This gives gradients a direct path back through the network, which is what allows 96 layers to train at all — without it you hit vanishing gradients long before that depth.

Layer normalisation. Each token’s vector is renormalised to a consistent scale between sub-layers, keeping values in a stable range. Same motivation as feature scaling, applied continuously inside the network.

Neither is glamorous, and both are load-bearing. Remove them and deep transformers do not train.

Encoder, decoder, or both

Encoder-only
Sees the whole input at once. Good for classification, search, embeddings. BERT-style.
Decoder-only
Causally masked, generates left to right. Every modern chat model. GPT-style.
Encoder–decoder
Reads one sequence, writes another. Translation, summarisation. The original 2017 design.

Why transformers replaced everything else

The predecessor architectures — RNNs and LSTMs — processed tokens strictly one at a time, because each step needed the previous step’s output. That made them impossible to parallelise across a sequence, and it made long-range dependencies fade.

🧪 The insight that unlocked scale

A transformer processes every token in the sequence simultaneously. There is no step-by-step dependency during training, so the work maps perfectly onto GPUs.

That is the real breakthrough. Attention is elegant, but parallelism is what made it possible to train on trillions of tokens — and everything since has followed from being able to scale.

Note the trade: transformers gained parallelism and paid for it with quadratic cost in sequence length. On the hardware available, that was overwhelmingly worth it.

🔑 Key Takeaways

  • A transformer block is attention plus a feed-forward layer, stacked N times.
  • Attention moves information between tokens; the feed-forward layer processes each alone.
  • Attention is order-blind, so positional encoding is added to the embeddings.
  • Residual connections and layer normalisation are what make deep stacks trainable.
  • Parallel processing of the whole sequence — not attention itself — is what enabled the scale.

Further reading

Jay Alammar’s Illustrated Transformer remains the clearest visual walkthrough, and Andrej Karpathy’s nanoGPT is a complete, readable implementation in a few hundred lines of Python.

Where to go next

Scroll to Top