Each token produces a query, a key and a value. Comparing one token’s query against all the keys produces relevance scores, which become weights; the output is the weighted sum of the values. Nothing encodes these relationships by rule — they are learned because getting them right improves prediction.
Attention’s cost grows with the square of the sequence length, which is why long context is expensive and why so much engineering targets this one operation.
