From words to attention: how a Transformer actually understands a sentence
Token IDs carry identity, not meaning. Attention is how meaning arrives: every token asks a question, every token advertises what it knows, and every token decides whom to listen to.
Welcome back.
In part one we learned what an LLM is, what tokens are, and how text gets represented. Now we're diving into Transformers and Attention — and the mathematics behind them.
This won't feel like a textbook. Think of it as a conversation where we learn, question, get confused, and eventually make sense of it.
A quick recap
An LLM — Large Language Model — is a neural network trained on a huge amount of text to learn patterns in language.
For an autoregressive LLM like GPT, the core training objective is next-token prediction: given some tokens, predict the most likely next token.
Input: "The sky is"
Output: "blue" (high probability)
Then take the new sequence and predict again.
One token at a time, until a full response exists.And text gets broken into tokens first, each token mapped to an ID. Our pipeline so far:
Text
↓
Tokens
↓
Token IDs
↓
Embeddings / Vectors
↓
Transformer
↓
Predicted next-token probabilitiesBut now comes the interesting question: how does the model take these vectors and figure out how the tokens relate to each other?
Let's find out.
Token IDs are labels, not meaning
Take “I love coding”. The tokenizer might produce [“I”, “ love”, “ coding”], and each token gets a number:
“I” → 40, “ love” → 1827, “ coding” → 9234.
These are Token IDs — think of them as roll numbers for every token in the vocabulary. That's all they are.
Now here's where Token IDs start to fall apart.
Suppose the vocabulary contains cat → 100, dog → 101, car → 102, banana → 103. Is dog closer to cat than banana is, because 101 − 100 = 1 while 103 − 100 = 3? Of course not.
We could completely reshuffle the IDs — cat → 5000, dog → 17, car → 892, banana → 3021 — and the meaning of the tokens wouldn't change at all.
Token IDs tell the model “this is token #100”. They do not tell the model “this token is related to animals”. They contain identity, not meaning.
Embeddings: turning identity into meaning
So how do we turn a meaningless number into something a neural network can work with?
Enter embeddings.
An embedding is a learned vector of numbers that gives a token a numerical representation the network can compute with. When the model sees token ID 42, it retrieves that token's row from an embedding matrix — a giant learned table with one row per token in the vocabulary:
Token ID Embedding
0 [0.12, -0.41, 0.73, …]
1 [0.52, 0.18, -0.29, …]
2 [-0.31, 0.64, 0.11, …]
…
42 [0.21, -0.73, 0.44, 0.18, …]
…
63 [0.19, -0.65, 0.51, 0.22, …]When the model sees 42, it basically says: “Okay, token 42 — let me grab your row.”
And it gets [0.21, −0.73, 0.44, 0.18, …]. That vector is the embedding.
Why numbers? Because neural networks perform mathematical operations — once a token is a vector, the model can compare, add, transform, and measure relationships between vectors.
And who decides what these numbers should be?
Not a human. The model learns them during training: as it processes enormous amounts of text, these parameters get adjusted again and again until they become useful for the model's job.
Nobody can say “the third number means animals” or “the seventh number means happiness” — the information is distributed across many dimensions.
What is a vector?
Don't let the word scare you. A vector is just an ordered list of numbers.
With two numbers, [2, 5], you can represent a point in 2D space; with three, a point in 3D space. Language models use vectors with hundreds or thousands of dimensions — impossible to visualize, but mathematically unremarkable.
When we say cat → [0.21, −0.73, 0.44, …], we mean: represent the token “cat” as this point in a high-dimensional space. The individual dimensions don't have clean human-readable meanings; the combination creates the representation.
A word alone isn't enough
Here's the problem with fixed embeddings.
Consider: “The dog chased the cat” versus “The cat chased the dog”. Same important words. Completely different meaning — because the relationships between the words changed.
Or: “I deposited money in the bank” versus “I sat beside the bank of the river”. Same token, “bank”, different meaning.
So a single, fixed embedding for “bank” can't represent its meaning in every context. The model needs to look at the other tokens.
And that brings us to one of the most important ideas in Transformers.
Context: tokens need to interact
Consider this sentence: “The animal didn't cross the street because it was tired.”
What does “it” refer to? Most likely the animal.
But how did you figure that out? You looked at the other words. The meaning of “it” depends on its relationship with the rest of the sentence.
Imagine every token as a person sitting in a room: The, animal, didn't, cross, the, street, because, it, was, tired.
Each has its own representation, but to understand the sentence they need to interact. “it” needs to ask “who am I referring to?”, “cross” needs to ask “who is crossing what?”, “animal” needs to ask “what's happening to me?”
We need a mechanism that lets token representations communicate.
Attention: who should I pay attention to?
Attention lets a token gather information from other tokens that are relevant to understanding it. When the model processes “it”, it looks at every other token and assigns different levels of importance:
The → low
animal → HIGH
didn't → low
cross → medium
the → low
street → low
because → medium
it → …
tired → mediumThe pattern is what matters: information from “animal” gets more influence when building the new representation of “it”.
And no, there isn't a rule inside the model like “if token == 'it', look for the nearest noun”. The model learns these relationships during training, from enormous amounts of text.
Don't take this literally either — the model isn't consciously deciding what to look at. Attention is a mathematical mechanism that lets each token combine information from other tokens.
But how does the model actually calculate what to pay attention to? That's where three little characters enter: Query, Key, and Value.
Q, K, and V
Imagine you're in a huge library looking for information about Transformers.
You walk around with a question in your head — that's your Query.
Every book carries a description of what it contains — those are Keys. You compare your question against the keys: “does this book contain what I'm looking for?”
If a book looks relevant, you pick it up — and inside is the actual information. That's the Value.
Q → Ask: “What am I looking for?”
K → Match: “Do I contain what you're looking for?”
V → Retrieve: “Here's the information I can give you.”Back to our sentence. The token “it” gets a Query. Every other token has a Key.
The model compares the Query of “it” with the Keys of the others and finds: it → animal = strong match, it → cross = weaker, it → street = weak, it → tired = moderate.
But knowing which token is relevant isn't enough — the model also needs the actual information from it. That's what the Value provides.
Why three different vectors instead of one? Because attention does two related but different jobs: deciding what's relevant (Query vs Key) and gathering information from the relevant token (Value).
Using the same representation for both would be limiting — separating them gives the model flexibility.
And where do Q, K, V come from? Also learned. Starting from a token's embedding X, the Transformer applies three learned transformations:
Q = X·W_Q
K = X·W_K
V = X·W_VTake the input representation X and transform it in three different learned ways. The model learns the matrices W_Q, W_K, W_V during training — nobody tells it to make one dimension about nouns and another about verbs.
Scoring relevance: the dot product
How do we calculate how relevant one token is to another? We compare the Query with each Key using the dot product — a simple mathematical tool that measures how aligned two vectors are.
For one Query, we compare against the Keys of every token we're allowed to attend to:
Q(it) · K(the) → 0.2
Q(it) · K(animal) → 3.8
Q(it) · K(street) → 0.7
Q(it) · K(tired) → 1.2A larger score means the Query and Key are more aligned — so the model now has an idea of which tokens are more relevant.
But these are raw scores. We need to turn them into useful weights.
Softmax: turning scores into attention weights
[0.2, 3.8, 0.7, 1.2]
↓ softmax
[0.02, 0.88, 0.05, 0.05]Softmax transforms raw scores into normalized values: positive, summing to 1. Conceptually: The → 2%, animal → 88%, street → 5%, tired → 5%.
Now the model has weights describing how strongly each token should contribute to the current representation.
What Value actually does
We've figured out which tokens are important. Now we need the information they carry. The model uses the attention weights to create a weighted combination of the Value vectors, producing a new vector that contains information gathered from the relevant context.
Before attention:
it → representation of “it”
After attention:
it → representation of “it”
+ information from the relevant tokensThe representation of “it” is now contextual. It isn't just “I am the token it” — it contains information gathered from the surrounding sentence.
That's the whole point of attention.
The attention equation
We've built all the intuition — now the famous equation, which looks much scarier than it is:
Attention(Q, K, V) = softmax(QKᵀ / √d_k) · VRead it like a recipe.
Step one: QKᵀ compares Queries with Keys — which tokens are relevant to which?
Step two: divide by √d_k to scale the scores, because dot products grow with vector dimension and unscaled scores make softmax too sharp, which makes training unstable.
Step three: softmax turns the scores into normalized weights — how much should each token contribute?
Step four: multiply by V — use those weights to gather the useful information from the Value vectors.
Figure out what's relevant, turn that into weights, then use those weights to gather information. That's attention.
But then… what is a Transformer?
Zoom out. Attention isn't the entire Transformer — it's one of the key mechanisms inside Transformer layers.
A Transformer is a neural-network architecture built from layers that combine self-attention, feed-forward networks, residual connections, and normalization to repeatedly transform token representations:
Text
↓
Tokens
↓
Token IDs
↓
Embeddings
↓
Self-Attention
↓
Feed-Forward Network
↓
More Transformer Layers (many, stacked)
↓
Output
↓
Next-token predictionAnd one important detail: GPT-style models use causal attention.
A token cannot attend to tokens that come after it in the sequence. When predicting the next token after “The cat”, the model can use “The” and “cat” — but it cannot look ahead at the answer.
This prevents the model from cheating by seeing future tokens during training.
The big picture
Look how far we've come.
We started with “The animal didn't cross the street because it was tired” and followed it through the model — and suddenly none of these terms are random anymore. They're connected.
Token IDs tell us which token we're dealing with. Embeddings turn those IDs into learned numerical representations. Context tells us tokens need information from each other.
Self-attention lets each token compute how strongly it should incorporate information from the others. Q, K, and V give attention its mechanism — dot products measure alignment, softmax turns scores into weights, Values carry the information that gets gathered.
And the Transformer's stacked layers repeatedly transform these representations until the model can predict the next token.
There's still so much ahead: why separating Q, K, and V actually helps; what the dot product means geometrically; how causal masking works inside the matrices; and how all of this becomes the giant matrix multiplications GPUs execute so efficiently.
I'd rather take it step by step than throw a whole textbook at you.
So — get some rest, stay curious, and I'll see you in the next one.