How language models are built

An atlas of 44 documented architectures. Follow a token through a model, see what it remembers, and work out why two designs can give very different answers to the same engineering problem.

Start with one prediction

Suppose a model reads “The cat sat on the”. To suggest what comes next, it first breaks the text into tokens, which may be whole words, word fragments or punctuation. An embedding turns each token into a list of numbers. Each layer updates those numbers. The output head then gives each possible next token a score.

As you read, keep two questions in mind. Which earlier tokens can influence this one? And what must the model keep in memory for the next prediction? Start with self-attention for the baseline. Then compare a sliding window, a handful of selected keys and a fixed-size state.

Each chapter has a figure and a question to try before opening the reasoning. The model sheets give the architecture, reported results and sources. The animations use illustrative tokens and routing choices. They show how information can move through a model.

How to read the pictures

Full causal attention: reads the current and earlier tokens Sliding window: reads only recent tokens Latent or compressed attention Sparse attention: reads a selection Fixed state: linear attention, SSMs KV cache per token (blue bars for window layers) MoE feed-forward block or selected expert Dense feed-forward

Inspired by Sebastian Raschka's LLM Architecture Gallery and his articles, which are cited as secondary sources. All figures here are drawn from the data by this site; none of his images are reused.

What the mechanisms are for

The 2017 Transformer has four limits that matter at today’s scale. Each of the twelve mechanisms in this atlas works on one or two of them.

Attention cost

Every token is compared with every earlier one, so doubling the prompt quadruples the attention work. Some designs limit how far a query looks; others fold the past into a state of fixed size.

Chapters: self-attention, the sliding window, state-space models and linear hybrids and sparse attention.

Cache size

With full attention, each new token reads the keys and values for earlier positions. As the prompt grows, moving that data from memory can limit decoding speed. Sharing or compressing it across heads, positions and layers makes it smaller.

Chapters: shared keys and values, latent attention and compressed memory and the encoder-decoder.

Weights versus work

In a dense layer every weight is used on every token, so a larger model is a slower one. Routing, reusing layers and drafting several tokens at once separate what a model stores from what it computes.

Chapters: mixture of experts, multi-token prediction and looped depth.

Order and signal flow

Attention by itself ignores word order, and a standard stack adds every block’s update to one shared vector with a fixed weight. Position signals supply the order; newer residual designs change how that vector is carried through depth.

Chapters: position signals and the residual stream.

Compare the twelve mechanisms in one table
MechanismWhat changesWork per decoded tokenMemory as the context growsTrade-off
1. Self-attention (MHA)Every head queries all past positionsO(n)O(n · H)Can read any earlier position. Prefill work grows quadratically.
2. RoPE, partial RoPE, NoPEEncodes token order by rotation, or leaves it to other layersO(1) extraUnchangedRelative angles generalise well; unseen distances still need rescaling
3. GQA and MQASeveral query heads share one K/V headO(n), less to readO(n · G), with G below HSmaller cache at close to MHA quality; a single KV head (MQA) costs some quality
4. Latent attention (MLA)Caches one shared low-rank latent and a small rotary keyO(n) in latent spaceO(n · d_c)Smaller cache than GQA; QK-Norm cannot be applied
5. Sliding windowReads only the most recent w tokensO(w)Capped at O(w)One layer reads w positions. Earlier information can travel farther across successive layers.
6. Mixture of expertsA router runs k of E feed-forward expertsk expertsExpert weights are fixed as context growsEach token uses k experts. All E sets of weights must be available, and routing can be uneven.
7. State-space and linear hybridsFolds history into a state of fixed sizeO(1) on linear layersO(1) on linear layersLinear-time prefill; a fixed state cannot copy arbitrary distant text
8. Multi-token predictionExtra modules draft the tokens after the next oneDrafting work plus one trunk pass per candidate blockExtra module weights are fixed as context growsFaster when drafts match; wasted work when they do not
9. Sparse attentionAn indexer selects the top k past tokens for each queryO(n) index, O(k) attentionStill O(n) storedLess attention work and fewer reads, not less storage
10. mHC and attention residualsSeveral parallel streams, or attention across depthSmall mixing overheadWider stream to carryMore flexible flow across depth; unconstrained mixing destabilises training
11. Looped depthRuns the same L layers P timesP × L layer evaluationsOne set of weights, up to P cachesSaves weight memory, not computation
12. Compressed memory, encoder-decoderOlder tokens compressed in blocks, or encoded once for the decoderO(n / r) plus a local windowSmaller by block and layer sharingLarge prefill and cache savings; compressed blocks lose per-token detail

The mechanisms

Twelve chapters, one for each mechanism. Each explains its mechanism in plain words, with a figure, a short piece of code and a question to try.

Compare two models

Stack height shows the number of drawn blocks on a shared scale. Each coloured row is one block. The table explains grouped layers and highlights differences.

Sources

papers, technical reports and official documents. Model sheets link to the reports and configuration files used for their architecture and measurements.