# What is attention in AI?

> Attention is the mechanism in a transformer that lets each token weigh every other token in the input when the model computes its output.

Canonical: https://promptlyfound.com/glossary/attention
Updated: 2026-12-01

## Short answer

Attention is **the mechanism in a transformer that lets each token weigh every other token in the input when the model computes its output**. For each token, the model scores how well every other token matches it and mixes in their information by those scores. The 2017 paper Attention Is All You Need built the transformer from attention alone. Its cost grows with the square of the input length.

## Key findings

- Attention Is All You Need defines an attention function as a mapping from a query and a set of key-value pairs to an output. The output is a weighted sum of the values, and each weight comes from how well the query matches a key. [1]
- The paper gives the cost of a self-attention layer as O(n²·d) for n tokens, so the cost grows with the square of the input length. The layer connects any two positions in a constant number of steps, where a recurrent layer needs n steps. [1]
- A 2014 paper on machine translation let a model search the source sentence for the parts relevant to each word it predicts. Attention Is All You Need calls this method additive attention. [2]
- In a 2023 study, language models used relevant information best when it was at the start or end of a long input. Performance dropped when the information was in the middle. [3]

Attention decides how much each token of a prompt affects every other token. In AI search, it is how a model relates the question to the retrieved pages in its context window. It is the core of the transformer, the architecture behind current large language models.

## How does attention work?

Attention computes a new representation for each token as a weighted mix of the tokens around it. [Attention Is All You Need](https://arxiv.org/abs/1706.03762) describes an attention function as a mapping from a query and a set of key-value pairs to an output. The output is a weighted sum of the values. Each weight comes from how well the query matches the corresponding key.[1]

The paper's version is called scaled dot-product attention. The model multiplies the query by every key, divides the results by the square root of the key size, and applies a softmax function to turn the scores into weights.[1]

The paper shows an example from its own model. When the model processes the word "making", several attention heads give weight to the distant words "more difficult", which complete the phrase "making ... more difficult".[1]

In self-attention, the queries, keys and values all come from the same text. In a model that generates text, a mask lets each token attend only to the tokens before it.[1] [What is a transformer in AI?](https://promptlyfound.com/glossary/transformer) covers multi-head attention and the rest of the architecture.

## Where did the attention mechanism come from?

Attention came from machine translation. [Neural Machine Translation by Jointly Learning to Align and Translate](https://arxiv.org/abs/1409.0473), submitted in September 2014, said that a fixed-length vector for the whole source sentence limited translation quality. It let the model search for the parts of the source sentence that are relevant to each word it predicts.[2] Attention Is All You Need calls this method additive attention.[1]

Those earlier models used attention on top of recurrent networks, which read a text one token at a time. The 2017 paper removed the recurrence and built the transformer from attention alone. [What did the Attention Is All You Need paper introduce?](https://promptlyfound.com/llms/attention-is-all-you-need) covers the paper and the models that followed it.

## What does attention cost?

Self-attention compares every token with every other token, so its cost grows with the square of the input length. The paper gives the complexity of a self-attention layer as O(n²·d), where n is the number of tokens and d is the size of each token's vector. In exchange, the layer connects any two positions in a constant number of steps. A recurrent layer needs n steps.[1]

A prompt of 100,000 tokens has four times as many token pairs as a prompt of 50,000 tokens. The paper suggests restricting attention to a neighborhood of nearby tokens to improve performance on very long sequences.[1]

## How does attention affect AI search answers?

An AI search answer comes from a model that attends across the question and every retrieved passage at once. Attention lets any passage affect any word of the answer. It does not make the model use every passage equally well. In [Lost in the Middle](https://arxiv.org/abs/2307.03172), a 2023 study, models used relevant information best when it was at the start or end of a long input. Performance dropped when the information was in the middle, even for models built for long contexts.[3] [What is a context window?](https://promptlyfound.com/llms/context-windows) covers how much of a page reaches the model.

## Sources

1. Attention Is All You Need. arXiv, 2017-06-12. https://arxiv.org/abs/1706.03762
2. Neural Machine Translation by Jointly Learning to Align and Translate. arXiv, 2014-09-01. https://arxiv.org/abs/1409.0473
3. Lost in the Middle: How Language Models Use Long Contexts. arXiv, 2023-07-06. https://arxiv.org/abs/2307.03172

---

This is a plain-text copy of https://promptlyfound.com/glossary/attention from Promptly Found. The web page is the canonical version and includes the charts.
