IT Vizag
← How LLMs Really Work
Loading video…

How LLMs Really Work · Lesson 3 of 15 · 3 min read · 6 min video

Attention: how models decide what matters

The idea behind every modern LLM: each word looks at every other word.

📄 Download slides (PDF)

Read this sentence: "The fisherman took his boat out from RK Beach because it was calm." What does "it" mean? The sea, not the boat or the fisherman. You worked that out by looking at the other words. That, in one idea, is attention, the mechanism behind every modern LLM.

The problem attention solves

Older models read text one word at a time, carrying a summary forward. By the end of a long paragraph, early details faded. Attention lets every word look directly at every other word, however far apart, and decide which ones matter for understanding it.

Query, key, value

For each word, the model makes three vectors from its embedding:

  • Query (Q): "What am I looking for?"
  • Key (K): "What do I contain?"
  • Value (V): "What information do I pass on if chosen?"

Think of a library. Your query is the question you bring. Every book has a key on its spine. You compare your question with every spine, pick the best matches, and read their values: the contents.

The four steps

For the word "it":

  1. Score: compare its query with every word's key (a dot product). "calm" and "sea"-like words score high, "fisherman" lower
  2. Scale: divide by the square root of the vector size, so scores don't explode
  3. Softmax: turn scores into weights that add up to 1, for example 0.6 calm, 0.25 RK Beach, 0.1 boat, 0.05 the rest
  4. Blend: add up every word's value vector using those weights

The result is a new vector for "it" that now contains information about what it refers to.

import numpy as np

def attention(Q, K, V):
    scores = Q @ K.T / np.sqrt(K.shape[-1])          # 1. score + 2. scale
    weights = np.exp(scores) / np.exp(scores).sum(-1, keepdims=True)  # 3. softmax
    return weights @ V, weights                       # 4. blend

rng = np.random.default_rng(0)
x = rng.normal(size=(5, 8))      # 5 words, 8-number embeddings
out, w = attention(x, x, x)
print(w.round(2))                # each row: how much a word looks at the others

Many heads

One attention pattern can't capture everything. So models run multi-head attention: many attention calculations side by side, each with its own Q, K and V. In trained models, some heads tend to track grammar, some track what pronouns refer to, some track nearby words. Their outputs are joined back together.

Looking only backwards

When generating text, a model must not peek at words it hasn't written yet. So LLMs use masked (causal) attention: each word can attend only to itself and earlier words. That's why they write strictly left to right.

Why it changed everything

  • Every word sees the whole context in one step, so long-range links survive
  • All words are processed in parallel, which suits GPUs and made training on huge data practical
  • The same mechanism works for text, images, audio and code
Tip: When a model "forgets" something you said early in a long chat, it's often because that part fell outside the context window, or attention spread too thin across a very long input.

In one line

Attention lets every word score every other word, turn the scores into weights, and blend in the most relevant information, so meaning comes from context.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend