How does an LLM use the words around a word?

“River bank” and “savings bank” share a word. The surrounding words change what it means.

How does an LLM use that context?

This comic follows a query, two keys and two value vectors through a small attention calculation. You can check the dot products and softmax yourself. The toy leaves out scaling, which the real mechanism needs.

Download this comic as a PDF

Comic page 1 of 6: How does an LLM use the words around a word?
Read page 1 as text

How does an LLM use the words around a word?

River bank. Savings bank. How does the same token acquire different context?

WHAT WE WILL FIGURE OUT

01

From token ID to context

02

What queries, keys and values do

03

Where the vectors come from

OUR EXAMPLE

Toy query at “it” in: The cat drank the milk because it was hungry.

Follow the steps. Ask the obvious questions. Check the result.

Part 5 of 18 · Challenge: page 5 · Suggested answers: page 6

Comic page 2 of 6: From token ID to context
Read page 2 as text

From token ID to context

1

ONE TOKEN, TWO CONTEXTS

river bank river context

savings bank financial context

same token initial embedding

different context different later state

river bank

savings bank

Is bank always the same thing?

The initial token embedding is only a starting representation. Later layers combine it with available context.

river bank / savings bank

2

EMBED THE ID

FOLLOW THE CHANGE

token ID

vector

layers

token ID

→

vector

→

layers

An embedding is a learned vector of numbers associated with a token. It is not a dictionary definition in plain English.

Token ID → learned vector → layer stack

3

LOOK BACK

river

bank

future masked

river

bank

future: masked

A causal decoder position attends to itself and earlier positions. It cannot read future tokens that have not arrived.

river can inform the later bank position

4

BUILD A RICHER STATE

FOLLOW THE CHANGE

visible states

attention blend

visible states

→

attention blend

Attention mixes information from visible positions. Other parts of each layer transform the resulting representation.

Attention connects positions; layers refine state

Comic page 3 of 6: What queries, keys and values do
Read page 3 as text

What queries, keys and values do

5

QUERY

QUERY FROM THE POSITION “it”

q [2, 1]

Earlier cat / milk keys are visible

query from it

[2, 1]

Whose query is this?

Take “it” in our cat-and-milk sentence. Its current hidden state produces the query, which scores available keys.

Toy q = [2, 1]. Later words stay masked.

6

KEY

TWO CANDIDATE KEYS

cat key [2, 0]

milk key [0, 1]

Different keys, same comparison rule

cat key [2,0]

milk key [0,1]

A key helps determine how a position is matched. Its value vector is the information delivered by the weighted blend. They have different jobs.

Toy keys: cat [2,0]; milk [0,1]

7

SCORE AND NORMALIZE

cat: 2 × 2 + 1 × 0 = 4 milk: 2 × 0 + 1 × 1 = 1

softmax of [4, 1]

cat weight 95.3%

milk weight 4.7%

scores 4, 1

→

95.3%, 4.7%

In this unscaled toy, dot products give scores 4 and 1. Softmax turns them into about 95.3% and 4.7%.

Real attention also scales scores and applies masks

8

VALUE

0.953 × [1, 0] + 0.047 × [0, 1]

WEIGHTED BLEND

[0.953, 0.047]

V1 [1,0]

V2 [0,1]

The attention weights blend the value vectors. It is a weighted combination, not necessarily one exact lookup.

Values [1,0], [0,1] → [0.953,0.047]

Comic page 4 of 6: Where the vectors come from
Read page 4 as text

Where the vectors come from

9

LEARNED PROJECTIONS

hWq

hWk

hWv

hWq

hWk

hWv

Who wrote those vectors?

Each layer makes queries, keys and values by applying learned projection matrices to its current hidden state.

q=hWq; k=hWk; v=hWv

10

THE SAME FILTERS

same matrices

changing states

same matrices

changing states

The projection matrices are reused across positions. Context changes the hidden states that flow through later layers.

Same weights, context-dependent representations

11

MANY HEADS AND LAYERS

attention

feed-forward

attention

feed-forward

Multiple attention heads can capture different relationships. A layer also contains other transformations, such as a feed-forward network.

One toy head does not explain a real decision

12

NOW THE CACHE MAKES SENSE

Earlier computed keys + values

New token reuses earlier KV

old K,V

new token

Can earlier keys and values be kept?

With causal generation, appending a token leaves earlier states unchanged. Their computed keys and values can be reused.

Next set: what the context can hold

Comic page 5 of 6: Your turn. Can you explain what happens?
Read page 5 as text

Your turn. Can you explain what happens?

Use the same example. Change one detail. What follows?

KEEP THIS EXAMPLE IN MIND

Toy query at “it” in: The cat drank the milk because it was hungry.

Try first. Suggested answers are on the next page.

1

In a causal decoder, can bank look at a word that appears after it?

What makes you say that?

2

Which vector is matched: a query to keys, or a query to values?

What makes you say that?

3

In the toy, why is the output mostly the first value?

What makes you say that?

Write your answers in the comments.

Comic page 6 of 6: Suggested answers
Read page 6 as text

Suggested answers

How does an LLM use the words around a word?

01

In a causal decoder, can bank look at a word that appears after it?

No. That later position is masked for this prediction.

02

Which vector is matched: a query to keys, or a query to values?

The query is compared with keys; the resulting weights blend values.

03

In the toy, why is the output mostly the first value?

Its matching score is larger, so softmax assigns it about 95.3% of the weight.

Try the challenge on page 5. Suggested answers follow on page 6.