Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 2. What an LLM is
  4. 2.3

Lesson 2.3 · 3 of 6 · 8 min read · Reviewed 24 Nov 2026

The transformer and attention, in pictures

By the end, you can

  • Explain what attention does in one sentence.
  • Say why transformers process the prompt faster than they write output.

Modern language models, Claude included, are built on the transformerTransformer: The neural network architecture behind modern LLMs, built from stacked attention and feed-forward layers. architecture. You do not need the maths to design systems with them, but a mental picture helps explain their strengths and limits.

Step 1: tokens become vectors

Each tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. id is mapped to an embeddingEmbedding: A list of numbers (a vector) that represents the meaning of a piece of text, so similar meanings land close together.: a long list of numbers that places it in a high-dimensional space. Position information is added so the model knows the order.

Step 2: attention

In every layer, each token asks a question of all the others: which of you matter for understanding me? That is attentionAttention: The mechanism that lets each token weigh every other token in the context when building its representation.. In The cat sat on the, the final the attends strongly to sat and on, which is what makes a surface such as mat likely next.

Attention happens for all tokens at once, which is why transformers train and read input so efficiently on modern hardware.

Step 3: many layers

A model stacks dozens of these layers. Early layers pick up grammar and local patterns. Later layers represent more abstract things: topic, intent, the structure of an argument, the state of a piece of code.

Why this matters to an architect

  • Everything in the context can influence everything else. Instructions, examples and documents all compete for attention, so clear structure (lesson 11.4) helps.
  • Cost grows with context. Longer inputs mean more work per request.
  • Placement matters. Very long contexts can bury key facts; lesson 59.3 covers where to put them.

Key points

  • Transformers process every token in the context in parallel.
  • Attention lets each token weigh the relevance of every other token.
  • Stacked layers build up meaning from surface patterns to abstract ideas.
Hands-on labRuns in the Playground

Compute attention weights by hand

Score words with dot products, turn the scores into weights with softmax, and see which word "it" attends to.

You will have: A script that prints the most attended word for "it" in two versions of a sentence.

Steps

  1. Write softmax: subtract the maximum, exponentiate, divide by the sum.
  2. Score each word by the dot product of the query for "it" and the word's key vector.
  3. Turn the scores into weights and find the largest.
  4. Compare the "tired" and "wide" endings.

Done when

  • With "tired", it attends most to animal.
  • With "wide", it attends most to street.
  • The weights for each sentence add up to 1.

Open in the Playground No API key needed.

Starter code (Python)
import math

KEYS = {
    "The": [0.1, 0.0, 0.0], "animal": [0.9, 0.1, 0.2], "didn't": [0.1, 0.1, 0.0],
    "cross": [0.2, 0.3, 0.1], "the": [0.1, 0.0, 0.0], "street": [0.1, 0.9, 0.3], "because": [0.0, 0.1, 0.1],
}
QUERY_FOR_IT = {"tired": [2.0, 0.2, 0.1], "wide": [0.2, 2.0, 0.3]}

def softmax(xs):
    # TODO: subtract the max, exponentiate, divide by the sum
    return [1 / len(xs)] * len(xs)

def attention(query):
    words = list(KEYS)
    # TODO: score each word with the dot product of query and its key vector
    scores = [0.0 for w in words]
    return dict(zip(words, softmax(scores)))

for ending, q in QUERY_FOR_IT.items():
    weights = attention(q)
    top = max(weights, key=weights.get)
    print(f"...because it was {ending}: it -> {top} ({weights[top]:.0%})")
Show a solution
import math

# Tiny made-up vectors. Real models learn hundreds or thousands of dimensions per token.
KEYS = {
    "The": [0.1, 0.0, 0.0], "animal": [0.9, 0.1, 0.2], "didn't": [0.1, 0.1, 0.0],
    "cross": [0.2, 0.3, 0.1], "the": [0.1, 0.0, 0.0], "street": [0.1, 0.9, 0.3], "because": [0.0, 0.1, 0.1],
}
# The query for "it" depends on the word that follows: tired things are animals, wide things are streets.
QUERY_FOR_IT = {"tired": [2.0, 0.2, 0.1], "wide": [0.2, 2.0, 0.3]}

def softmax(xs):
    m = max(xs)
    e = [math.exp(x - m) for x in xs]
    return [x / sum(e) for x in e]

def attention(query):
    words = list(KEYS)
    scores = [sum(q * k for q, k in zip(query, KEYS[w])) for w in words]  # dot product
    return dict(zip(words, softmax(scores)))

for ending, q in QUERY_FOR_IT.items():
    weights = attention(q)
    top = max(weights, key=weights.get)
    print(f"...because it was {ending}: it -> {top} ({weights[top]:.0%})")
Quick checkIn "The trophy didn't fit in the suitcase because it was too big", what lets the model link "it" to "trophy"?
How sure are you?
Pick an answer and how sure you are