Attention gives a sequence decoder access to a weighted combination of source representations at each output step. This can reduce the bottleneck created by requiring a recurrent encoder to compress an entire input into one fixed-size summary.
Before attention existed, sequence-to-sequence models handled tasks like machine translation with a pair of recurrent networks (Sutskever et al., 2014). An encoder RNN reads the source sequence one token at a time, updating a hidden state at each step, and a decoder RNN generates the target sequence conditioned on nothing but the encoder’s final hidden state , where is the source length. That final vector is the entire channel between source and target. A three-word sentence and a three-hundred-word document are both compressed into a vector of the same fixed dimension , and the decoder must reconstruct everything it needs — syntax, referents, long-range agreement, the works — from that single summary.
This is an information bottleneck in the literal sense: a fixed-capacity vector standing between an arbitrarily long input and the output it must explain. It has two related failure modes. First, capacity: a vector of dimension can only carry so many bits before nearby inputs start to collide in representation space, and there is no mechanism to grow that vector as the source sentence grows. Second, recency bias inherited from the recurrence itself: even with gated cells like LSTMs (Hochreiter and Schmidhuber, 1997) that mitigate vanishing gradients across time steps, the hidden state at step is computed by repeatedly overwriting and blending information from step through step , so tokens near the end of the source sequence tend to dominate the final state more than tokens near the beginning — regardless of which tokens actually matter for producing a given output word. Empirically, translation quality for these models degrades sharply as source sentences grow longer than those seen during training, exactly as the bottleneck argument predicts: there is nowhere for the additional information to go.
Bahdanau et al. (2015) diagnosed the bottleneck precisely as “encoding a variable-length source sentence into a fixed-length vector” and replaced it with a mechanism that lets the decoder revisit the entire source sequence at every output step, rather than working from one pre-compressed summary. Instead of a single context vector, they computed a distinct context vector for each decoder step , formed as a weighted combination of all encoder hidden states: The weights are not fixed; they are produced by a small learned feedforward network — an alignment model — that scores how well decoder state matches encoder state , followed by a softmax over so that the weights for a fixed form a probability distribution summing to one. Crucially, the alignment model is trained jointly with the rest of the network by ordinary backpropagation: nothing about which source words matter for which target words is hand-specified, it is learned end to end from translation data alone.
This single idea — score every source position against the current decoding state, normalize the scores into a distribution with softmax, and use that distribution to take a weighted average of the source representations — is the direct ancestor of query/key/value attention. The decoder’s state plays the role that a query will play; the encoder’s hidden states play the role that keys and values will play; the alignment score plays the role of the compatibility function between a query and a key. What Vaswani et al. (2017) did was strip away the recurrence entirely, keep only the alignment-and-weighted-average mechanism, and generalize it in two directions that matter for the rest of this book. First, they made query, key, and value each an explicit learned linear projection of the input rather than an ad hoc scoring network, giving the mechanism a clean, reusable matrix form. Second, they let a sequence attend to itself — every position generating a query against every position’s keys and values, including its own — which is what makes attention a general-purpose sequence-processing layer rather than a translation-specific add-on. The companion book develops the mechanism in detail.
Continue with the book
Explore the full treatment, worked examples, and exercises in LLMs Derived.
Companion article for LLMs Derived, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
