← All animations

🔤 Attention & Transformers

Large language models read a sentence all at once and, for every word, decide which other words matter. That weighing act is called attention, and stacking it into layers gives you a Transformer — the architecture behind today's models (GPT, Claude and the rest). Scroll through to see how a model figures out what "it" refers to, how query/key/value math turns words into weights, and why attention swept past the older recurrent networks.

1. Self-attention — which words does a word look at?

Here's a classic ambiguous sentence. Click any word and the curves show how strongly it attends to every other word — thicker, brighter links mean "more relevant." Click it and watch where the model looks to resolve the pronoun. Flip the last word with the toggle: with "tired," it points to the animal; with "wide," it points to the street. Same word, different attention.

Click a word to see its attention

Weights here are illustrative — a small softmax over toy query·key scores — but they behave like the real thing: every word's weights are non-negative and add up to 1 (100%).

2. Query · Key · Value — how a weight is actually computed

Each word emits three vectors. Its Query ("what am I looking for?") is compared against every word's Key ("what do I offer?") with a dot product. Those raw scores go through a softmax to become weights that sum to 1, and the word pulls a weighted mix of every word's Value ("what I actually carry"). Watch one token do it below.

Head 1 of 3

Real models run several heads at once (multi-head attention) — each learns a different pattern, like grammar in one head and subject-tracking in another. Stack many such attention layers and you have a Transformer. Order is supplied separately by a positional encoding added to each word's embedding, since attention itself is order-blind.

3. Why attention beat the old recurrent networks

Before Transformers, models read text one word at a time (RNNs/LSTMs), passing a hidden state along like a relay baton. That made them slow and forgetful over long passages. The 2017 paper "Attention Is All You Need" threw out recurrence entirely:

The full pipeline a model runs on your prompt:

tokens→ embeddings→ + position→ attention layers→ predict next token

Text is split into tokens, each becomes an embedding vector, position is added, the stack of attention layers blends context across the whole sequence, and the model outputs a probability for the next token — then repeats, one token at a time, to write its reply.