Skip to content
Technology, explained from the insideAI · Software · Digital payments
TayoTechTalk.

How AI Works

Attention in AI: How a Model Uses Relationships Between Tokens

By Temitayo Solomon Gbadebo · November 24, 2026
Conceptual illustration of token tiles linked by lines of varying thickness to show weighted information flow.

A language model can receive thousands of tokens, but having those tokens available leaves an important question: how does information from one part of the text influence another?

That is where attention comes in. In a Transformer, attention helps build a token’s working representation using information from other token positions. It is a numerical operation, not a human act of concentration.

Earlier topics in this series covered next-token prediction, context, retrieval, embeddings and learning. Attention connects those ideas to a more specific question: what happens between the input numbers and the next prediction?

Start with a sentence that needs context

Consider this unfinished sentence:

The keys beside the notebook on the desk are

A continuation such as “missing” concerns the keys. “Notebook” and “desk” are nearby nouns, but they are not the subject of “are.” A useful internal representation at that final position needs information from beyond the immediately preceding word.

For this explanation, imagine each word occupies one token position. Actual tokenizers can split words into smaller pieces, and punctuation can have its own positions.

As Hugging Face’s Transformer course explains, attention lets a representation depend on context elsewhere in a sequence. Our sentence illustrates why that matters: closeness alone does not tell us which relationship is useful.

Queries, keys and values

Attention uses three sets of numbers, usually called queries, keys and values. Learned transformations produce them from the representations entering an attention layer.

  • Query: the numbers used by a position to score potential sources of information.
  • Key: the numbers at each available position that are compared with that query.
  • Value: the information that position can contribute to the resulting mixture.

The names can sound like a database lookup. Here, however, the comparison is mathematical and usually blends information from multiple positions. Nobody writes a question such as “find the subject” inside the query.

In standard scaled dot-product attention, query–key comparisons produce scores. The scores are scaled, any disallowed connections are masked, and a function called softmax turns the remaining scores into weights that sum to one. The output is a weighted sum of the value vectors. The PyTorch reference implementation shows that sequence directly.

A small, made-up attention calculation

Imagine one attention head at the final “are” position produced these weights:

  • “keys”: 0.50
  • “notebook”: 0.20
  • “desk”: 0.10
  • All remaining positions combined: 0.20

These are invented teaching numbers, not measurements from a model. They add up to one. For this head, the value associated with “keys” receives half the mixture’s weight.

To see the arithmetic, temporarily reduce each value vector to a single number. Suppose those numbers were 4 for “keys,” 1 for “notebook,” 2 for “desk,” and a weighted average of 0 for the remaining group. The mixture would be:

(0.50 × 4) + (0.20 × 1) + (0.10 × 2) + (0.20 × 0) = 2.4

Real values contain many numbers, and the weighting operates across their components. Our tiny calculation simply makes the blending visible.

Crucially, 0.50 does not mean the model is 50% confident that the keys are missing. It is a mixing weight inside one head at one layer. The number 2.4 is not a word, a factual answer or a probability either.

Nor would changing “keys” to “book” just replace one value in this example. Queries, keys, values and later computations can all change when the input changes. Attention patterns are calculated from the current representations.

Invented weights of 0.50 for keys, 0.20 for notebook, 0.10 for desk and 0.20 for other positions produce a simplified weighted value of 2.4.
Teaching example only: these weights are invented and are not model confidence scores.

Why there are multiple heads and layers

The original Transformer paper, Attention Is All You Need, describes multiple attention heads operating in parallel. Each can form a different mixture, and their outputs are combined through a learned transformation.

This gives the model multiple routes for combining information. It does not guarantee a neat division where one head always handles grammar and another always handles names.

Attention also sits within a larger network. Feed-forward layers transform representations, residual connections carry information forward, and normalization helps regulate the computation. Position information helps the model distinguish token order. Stacking layers allows later computations to use representations already influenced by earlier ones.

The paper’s memorable title should not be read as a complete inventory of the model. Attention is central, but other components do substantial work.

Why a text generator cannot peek ahead

In a typical causal language model, a position can attend to itself and earlier positions, not future ones. This rule matters during training too: supplying a full training example must not let the prediction use the answer it is supposed to predict.

During generation, the final position’s processed representation is used to calculate next-token probabilities. Once another token is generated, the process continues.

Implementations often reuse previously calculated keys and values rather than recomputing them for every step. Hugging Face’s caching documentation explains this key-value cache. It saves repeated computation; it does not turn attention into permanent personal memory.

Useful mechanism, limited explanation

A diagram of bright attention links can be informative, but it is not a full explanation of an answer. The research paper Attention is not Explanation demonstrated that different attention distributions could produce equivalent predictions in the systems studied. A weight alone does not establish a token’s causal importance.

Attention also cannot certify whether a passage is true. Retrieved text can supply useful evidence or misleading claims. Mixing information from that text does not settle which is which.

The practical takeaway: attention helps tokens exchange information through learned, context-dependent numerical relationships. That makes it an important part of how a model uses its input, while leaving accuracy, interpretation and the final prediction dependent on much more than attention alone.

Continue reading