Each token looks back at the others.

Lesson 3 of 6About 9 minutes6 steps

A word means different things in different company. Attention is how a transformer lets each token pull in what it needs from the tokens before it, and it’s the idea the whole architecture is named after.

  • Explain queries, keys and values
  • Work through one attention step by hand
  • Say why attention gets expensive on long text

You need: Lesson 2, Embeddings.

  1. 1Tokens
  2. 2Embeddings
  3. 3Attention
  4. 4Picking the next token
  5. 5Context and the KV cache
  6. 6Hallucination
0 of 6 steps
  1. Words take meaning from neighbours

    Predict first

    In “I sat by the river bank”, which word tells the model what “bank” means?

    Just show me

    “river”: it’s a river bank, not a bank that keeps money.

    A token’s embedding alone can’t tell “bank” the river from “bank” the money. Attention lets each token mix in information from other tokens, weighted by how relevant they are, so “bank” ends up carrying a bit of “river”.

    Quick check

    What does attention produce for each token?

    An updated list of numbers that blends in what matters from the context.

  2. Query, key and value

    Predict first

    Work it through below. Which earlier token will “bank” weigh most?

    Just show me

    “river”: 37% of the mix.

    Each token makes three lists from its embedding: a query (what it’s looking for), a key (what it offers to be found by) and a value (what it passes on). “bank”’s query is compared with every key by a dot product; the scores are divided by √d (here d = 4) and turned into shares by softmax; the shares mix the values. The numbers below are small and made up so you can follow them; a real model uses hundreds per head.

    The query of bank: [1, 0.2, 1.2, 0.1] (d = 4)

    TokenKeyq · k÷ √4Share
    I0.1 0 0.2 0.10.350.1711.7%
    sat0 0.3 0.1 00.180.0910.8%
    by0.2 0.1 0 0.10.230.1211.0%
    the0.1 0.1 0.1 0.10.250.1311.2%
    river0.9 0.2 1.4 0.32.651.3237.0%
    bank0.4 0.5 0.6 0.21.240.6218.3%

    Mixed with these shares, the values give bank’s new vector: [0.433, 0.22, 0.221], the largest part from river.

    Quick check

    What decides how much “river” contributes to “bank”?

    The query-key score sets the share; the value is what gets passed on.

  3. Softmax: scores into shares

    Predict first

    After softmax, what do the shares for “bank” add up to?

    Just show me

    1: softmax turns any scores into shares of one whole.

    Softmax raises e to each score and divides by the total, so big scores get most of the share and every share stays positive. Dividing by √d first keeps scores from growing with the size of the vectors; without it, softmax would give nearly everything to one token and training would stall.

    Softmax, symbol by symbol

    Quick check

    Why divide the scores by √d?

    Dot products grow with dimension; scaling keeps the softmax in a range where it can still learn.

  4. No peeking ahead

    Predict first

    While a GPT-style model works on “bank”, can it look at the words after it?

    Just show me

    No: a causal mask hides every later token, because when it writes, those words don’t exist yet.

    Models that write (decoders, like GPT and Qwen) use a causal mask: a token attends to itself and earlier tokens only. Models that read a whole text at once (encoders, like BERT) attend in both directions, which suits classifying or embedding text but not writing it word by word.

    Quick check

    You want to embed whole sentences for search. Which kind fits?

    Embedding models are usually encoders: they can use the whole sentence at once.

  5. Many heads, many layers

    Predict first

    How many times does attention run for each token in Qwen 3 (8B)?

    Just show me

    36 layers, each with 32 attention heads, from its config file: 1,152 attention patterns per token.

    A head is one query-key-value set. Several heads in a layer can each follow something different (researchers have found heads that track the previous token, or a verb’s object), and stacking layers lets later ones build on earlier ones. Qwen 3 (8B) shares each key and value between groups of query heads: 32 query heads but 8 key-value heads, which the next lessons come back to.

    Quick check

    Why have many heads instead of one big one?

    Different heads learn different patterns; the layer combines them.

  6. Why long text gets expensive

    Predict first

    A text doubles from 1,000 to 2,000 tokens. How do the attention scores per head grow?

    Just show me

    Four times: every token is scored against every earlier one, so 1,000 tokens make about 5,00,500 scores and 2,000 about 20,01,000.

    The scores grow with the square of the length. At 32,768 tokens that’s about 537 million scores per head per layer. Much of the engineering in modern models, from the KV cache to faster attention kernels, is about this cost.

    Quick check

    Which grows fastest as a prompt gets longer?

    Weights and vocabulary are fixed; attention work grows with length squared.

In the interview: attention.

Ask Prism about this lesson

Sources

  1. Vaswani and others, “Attention Is All You Need”, NeurIPS 2017: the transformer and scaled dot-product attention.
  2. Qwen, Qwen 3 (8B), Apache 2.0: its config file and model card for the layers and heads.

Back: lesson 2, Embeddings