Bible Network Crypto DeFi Onchain RWA AI Agent Stablecoin Chain SAFU CryptoTax DeFAI AGI Claude Me Claude Skill Claude Design Claude Cowork
Independent Media
Not affiliated with any project
Exploring the Frontier of AI Intelligence
claude-me.com
LATEST
The Attention Mechanism: Why Transformers Actually Understand What You Say  ·  Four System Prompt Design Patterns That Make Claude Predictable and Repeatable  ·  Running Out of Context? Five Tricks to Keep Long Claude Conversations on Track  ·  Weekly Reports Without the Pain: Building a Repeatable System with Claude  ·  Claude Batch API in Practice: How to Cut Costs in Half on Bulk Tasks  ·  MCP vs Direct Claude API: What Is the Difference and When to Use Which
fundamentals

The Attention Mechanism: Why Transformers Actually Understand What You Say

30-Second Version · For the impatient
The Attention mechanism lets every word simultaneously 'see' the entire sentence, then decide which words to focus on — this is the core of how LLMs understand language.

Full Explanation +
01 · Why did this happen?

What is the fundamental difference between Transformers and RNNs?

The fundamental difference is in processing order:

  • RNN: Must process word by word in sequence. At each step, it only sees the current word and a "memory" vector carried from the previous step. Training cannot be parallelized. Information from early in a sequence tends to fade over long distances.
  • Transformer: Processes the entire sequence simultaneously. Any word can directly attend to any other word regardless of distance. Training is highly parallelizable, enabling massive-scale training.

This fundamental difference allowed Transformers to outperform RNNs on nearly every NLP task and ultimately gave rise to modern LLMs like GPT and Claude.

02 · What is the mechanism?

How are Attention scores calculated?

Simplified calculation flow:

  1. For each word, compute Query (Q), Key (K), and Value (V) vectors — derived by multiplying the word's embedding by three separate learned weight matrices
  2. Take the dot product of the target word's Q with every word's K to get raw scores
  3. Divide by √d_k (square root of Key vector dimension) to prevent values from becoming too large and causing gradient issues
  4. Apply Softmax to convert raw scores into a probability distribution between 0 and 1 (all words' scores sum to 1)
  5. Use this probability distribution as weights to compute a weighted sum of all V vectors — this becomes the target word's new representation

This process runs for every word in the sequence and is fully parallelizable.

03 · How does it affect me?

What is the relationship between Claude's context window limit and the Attention mechanism?

The relationship is direct. Self-Attention requires a computation for every pair of words in the sequence, meaning computational cost grows roughly quadratically (O(n²)) with sequence length. The longer the context window, the higher the computational cost, the slower the response, and the greater the memory requirements.

This is why, even though context windows could theoretically be unlimited, they have practical upper limits in deployed models — it's a balance between capability and computational feasibility. It also explains why in very long contexts, content from far away has diminishing influence on model output: Attention is spread across too many words, and the influence of any single word gets diluted.

04 · What should I do?

Why does the paper say 'Attention Is All You Need'? Doesn't Transformer use other components too?

Yes — the paper's core claim is that Self-Attention alone is sufficient to handle long-range dependencies in language, eliminating the need for RNN's step-by-step memory propagation.

But Transformers aren't "only Attention." They also include: Positional Encoding (because Attention itself has no sense of word order — position information must be injected separately), Feed-Forward Neural Network layers (FFN, for non-linear transformation after Attention), Residual Connections, and Layer Normalization.

The "All You Need" in the title means: you no longer need the recurrent structure of RNNs — other components are still necessary. The paper title is a bit of marketing hyperbole, but the core claim is correct.

Full Content +

When you ask Claude "She picked up the book and put it in her bag — what does 'it' refer to?", Claude correctly answers "the book" rather than "the bag." This seemingly simple capability rests on the most important innovation in modern AI: the Attention mechanism.

Understanding Attention isn't just technical curiosity — it helps you understand why Claude sometimes excels and sometimes makes mistakes in long texts, and how to write prompts that make it easier for Claude to "notice" the information that matters.

Before Attention

Before the Attention mechanism, language models relied primarily on RNNs (Recurrent Neural Networks). An RNN reads text like a person reading word by word: starting from the first word, processing one word at a time, and compressing "what has been read so far" into a fixed-size vector passed to the next step.

The problem is obvious: in long sentences, information from early in the sequence gets progressively overwritten by later information and nearly vanishes by the end. This is the long-range dependency problem — if a word near the beginning of a sentence needs to influence the interpretation of a word near the end, the signal must survive many compression steps, and it weakens with each one.

In 2017, Google Brain's paper "Attention Is All You Need" proposed a fundamental change: instead of reading sequentially, let every word simultaneously "look at" every other word, and then decide which ones to pay more attention to.

How Self-Attention Works

Consider the sentence: "The banker walked to the riverbank to deposit some money." The word "bank" is ambiguous in English — it could refer to a financial institution or a riverbank. How does the Attention mechanism resolve this?

Self-Attention generates three vectors for every word in the sentence:
- Query: "What kind of information am I looking for?"
- Key: "What kind of information can I provide?"
- Value: "If you select me, what is my actual content?"

When "bank" computes its Query vector, it calculates dot products against the Key vectors of every other word — a similarity measurement. "Deposit money" has a Key vector that matches "bank"'s Query strongly, so "bank" assigns it a high Attention score. "Riverbank" matches less well in this context.

The new representation of "bank" becomes a weighted average of all words' Value vectors — words with high Attention scores contribute more, those with low scores contribute less. This is how Attention allows "bank" to be understood as a financial institution in this particular context.

Multi-Head Attention

A single set of Query/Key/Value vectors can only capture one type of relationship at a time. Multi-Head Attention runs multiple independent Attention computations simultaneously — often 12, 32, or more "heads" — where each head learns to focus on a different type of relationship.

Within the same sentence:
- One head might specialize in syntactic dependencies (subject → verb)
- Another might learn semantic associations (animal words → action words)
- Yet another might track coreference (pronoun → the noun it refers to)

All heads' outputs are concatenated and passed through a linear layer to produce a final representation. This design allows Transformers to understand text along multiple dimensions simultaneously — far richer than a single RNN's linear readthrough.

Why Attention Makes LLMs Powerful

The Attention mechanism unlocked several critical capabilities:

Parallel computation: Unlike RNNs that must process sequentially, Self-Attention computes relationships between all word pairs in a sentence simultaneously, dramatically accelerating training and making large-scale corpus training feasible.

Arbitrary-distance dependencies: No matter how far apart two words are in a sentence, Self-Attention can establish a direct connection between them — no step-by-step signal propagation, no attenuation over distance.

Partial interpretability: Attention scores can be visualized. Researchers can see which context words the model "most cared about" when generating a particular output — more transparent than the black box of RNNs.

Scalability: The Transformer architecture naturally supports expansion to enormous parameter counts. GPT, Claude, Gemini, and other large language models are all Transformer descendants, trained on massive text corpora with hundreds of billions or trillions of parameters.

What This Means for Your Claude Use

Understanding Attention directly improves how you use Claude:

Put critical information at the beginning or end of your prompt: Research shows that in long contexts, models pay relatively less attention to content in the middle — the "lost in the middle" effect. Your most important instructions and constraints should appear at the top of your prompt or be explicitly restated at the end.

Disambiguate your context: Attention works by using context to resolve ambiguity. If your context is itself ambiguous, the model can only guess. Specify your scenario explicitly ("in a software development context," "for a non-technical audience") to give Attention cleaner signals to work with.

Attention dilution in very long contexts: The number of Attention relationships grows quadratically with context length. In very long contexts, early content has diminishing influence on generated output. This is a structural constraint to keep in mind when working with long documents.

Diagram
Self-Attention 計算流程示意圖左側展示 RNN 的逐步傳遞,右側展示 Attention 的全局並行計算對比 RNN vs SELF-ATTENTION RNN — Sequential w1 w2 w3 w4 Signal weakens over distance O(n) — sequential, slow Self-Attention — Parallel w1 w2 w3 w4 Every word attends to every word O(n²) — parallel, powerful Attention Score Example w1 → w1: 0.05 w1 → w2: 0.72 ◀ high w1 → w3: 0.15 w1 → w4: 0.08 Claude Me · claude-me.com
Feel free to share. Please credit the source.
Ask a Question
Please enter at least 10 characters
Related Articles
How an LLM Actually Generates Text: A Real Explanation for Non-Engineers
fundamentals · Jun 17
How Does AI Actually Work? An Explanation for People Who Don't Know Tech
fundamentals · Jun 08
Emergent Capabilities: Why Scaling AI Models Suddenly Unlocks Abilities That Weren't There Before
fundamentals · Jun 05
How Claude Actually "Thinks": Transformer and Attention Explained in Plain Terms
fundamentals · Jun 03
More Related Topics