Attention Mechanism

Attention is the mechanism inside a transformer that lets each token weigh how much every other token matters before being processed, so the model can track what 'it' refers to or which date belongs to which clause.

LLMs & languageself-attentionmulti-head attention
§ 01DefinitionAttention Mechanism

No jargon, no math

In plain words, what is it?

Attention gives each token three vectors. The query says what this token is looking for, the key says what it can offer, and the value is the actual content on offer. The model multiplies queries against keys to get a score for every pair: when 'penalty' meets the liquidated-damages clause, the score spikes. A softmax turns the scores into weights that sum to one, and each token rebuilds itself as a weighted sum of all the values. Think of reading a contract with a highlighter: every word decides for itself which other words to shade in, and the shading drives what the word means in context. Multi-head attention runs this several times in parallel with different views: one head catches grammar, another catches cross-references. The failure mode is distance: in a very long document the few tokens that matter can be outshouted by the many that merely surround them, which is why models miss the one buried number.

§ 02How it worksQuery meets key, values get blended
  1. 01

    Query. Each token asks, in vector form, what it is looking for.

  2. 02

    Key. Every token broadcasts what information it can offer.

  3. 03

    Score. Query times key produces a relevance score for every pair of tokens.

  4. 04

    Weigh. A softmax turns the scores into weights that sum to one.

  5. 05

    Blend. Each token becomes a weighted mix of all the values, so context is baked in.

§ 04

Want this running in your stack?

We design and ship AI systems with permissions, logs and human approval. It starts with a 30-minute scoping call.