Skip to main content

Why Transformers Won (vs RNNs/LSTMs)

Editorial

Introduction

The Transformer Architecture — The Big Picture, Self-Attention, Intuitively and Multi-Head Attention & Positional Encoding took the transformer apart. Now the question its 2017 paper answered: why did it replace the models that came before it?

Before 2017 the leading models for language were recurrent: they read text one token at a time. The paper says so in its first lines: recurrent networks, LSTMs and gated recurrent networks "have been firmly established as state of the art approaches in sequence modeling and transduction problems such as language modeling and machine translation". Its own model was built "dispensing with recurrence and convolutions entirely".

You'll learn:

  • how a recurrent network reads, and the two things that held it back
  • the two properties that let transformers win: every token at once, and a direct path between any two
  • the price transformers pay, a score for every pair of tokens, counted on real models
  • what that price means for context windows and for what you pay

The Old Champion: RNNs and LSTMs

A recurrent neural network (RNN) reads one token at a time, left to right, and carries a hidden state: a vector that sums up what it has read so far. At each step it takes the next token and the last hidden state and makes a new hidden state.

LSTMs and GRUs add gates to that step: learned switches that decide how much of the old state to keep and how much of the new to let in. Cho and colleagues, describing the GRU's gate that can clear the state: "This effectively allows the hidden state to drop any information that is found to be irrelevant later in the future". LSTMs carried language for years; in 2016 Google's translation system stacked "8 encoder LSTM layers".

All of it rests on recurrence: each step needs the step before it.

Flaw 1: Each Step Waits for the Last

Because each step needs the hidden state of the step before, a recurrent layer over a sentence is a queue: 32 tokens are 32 steps in a row, each waiting for the last. The 2017 paper: "This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths, as memory constraints limit batching across examples."

Graphics chips are built to do many operations at once. A recurrent network can still run many sentences side by side, but within one sentence its steps cannot overlap, and the longer the text, the longer the queue.

Flaw 2: Long-Range Context Fades

Everything a recurrent network knows about the past travels in its hidden state, and the first token reaches the last only through every step between them: 7 hand-offs for 8 tokens. Training sends its signal back along the same chain, and the signal weakens on the way. Hochreiter and Schmidhuber, introducing the LSTM: "Learning to store information over extended time intervals by recurrent backpropagation takes a very long time, mostly because of insufficient, decaying error backflow."

Gates help, and the limit can still be measured. Khandelwal and colleagues tested an LSTM language model by shuffling and dropping the words far back in its input: it "is capable of using about 200 tokens of context on average, but sharply distinguishes nearby context (recent 50 tokens) from the distant history", and it "ignores word order in the long-range context (beyond 50 tokens)".

How Transformers Fixed Both

The transformer drops recurrence, and both flaws go with it.

  • Every token at once. The rows of an attention layer depend only on the layer's input, not on each other, so they are worked out together, in one step, as a few large matrix products that graphics chips run well. The paper's Table 1 gives the sequential operations of a self-attention layer as constant and those of a recurrent layer as growing with the length. Its abstract: transformers proved "more parallelizable and requiring significantly less time to train".
  • A direct path between any two tokens. In one attention layer the last token compares itself with the first directly. Table 1 gives that path as one step, where a recurrent layer's grows with the distance.

A direct path is a chance, not a promise. A head still learns how much weight to give a distant token, and Self-Attention, Intuitively showed real heads giving much of theirs to the start marks and the word before. RoPE, from Multi-Head Attention & Positional Encoding, builds in a "decaying inter-token dependency with increasing relative distances". And Anthropic's documentation says of long contexts that as the token count grows, "accuracy and recall degrade".

Why transformers beat recurrent networks, counted for one layer over the 8 words "The animal didn't cross the street because it". A recurrent layer, the kind inside RNNs and LSTMs, works them one after another: each word goes into a hidden state that is handed to the next, 8 steps in a row, each waiting for the one before, and "The" reaches "it" only through 7 hand-offs. Measured on an LSTM language model by Khandelwal and colleagues in 2018, such a model uses about 200 tokens of context and keeps word order only within the last 50. An attention layer works every word at once: 1 step for all 36 scores, each word compared with itself and every earlier word, the later words masked, so "it" reaches "The" directly. The cost of that is the scores, one for every pair: twice the context gives 4 times the scores. But they are only part of the work. By Kaplan and colleagues' count of a forward pass, the scores' share at n tokens of context is n over 12 times the model's width plus n, so it reaches half only at 12 times the width: 9,216 tokens for GPT-2 117M, 49,152 tokens for LLaMA 6.7B, 147,456 tokens for GPT-3 175B. Doubling a context of 2,048 tokens multiplies LLaMA 6.7B's whole forward pass by 2.08, and GPT-3 175B at its own 2,048 tokens spends 1.4% of its work on the scores.

See It: Chain or Grid

Run both layers side by side. Pick how many tokens, press Run both, and count: the recurrent layer, above, lights one hidden state after another, while the attention layer's grid, below, lights all at once. Tap the last token to see how each layer gets back to the first. Then double the tokens and watch the two bars: the chain's steps double, the grid's scores grow about four times.

A recurrent layer and an attention layer over the same tokens: count the steps, the hand-offs and the scores.

The Tradeoff: A Score for Every Pair

The direct path has a price: every token is compared with every token it may see. Under a decoder's mask, 8 tokens make 36 scores and 32 make 528. The count grows with the square of the length: twice the tokens, about four times the scores.

The scores are not the whole bill, though. Kaplan and colleagues count a transformer's forward pass per token as the work of its weights plus the attention scores, and note that while the model's width is more than a twelfth of the context, "the context-dependent computational cost per token is a relatively small fraction of the total compute". Worked out on real models from their papers: the scores reach half the work only at 12 times the model's width, 9,216 tokens for the smallest GPT-2, 49,152 for LLaMA 6.7B and 147,456 for GPT-3 175B. GPT-3 at its own context of 2,048 tokens spends 1.4% of a forward pass on them. Doubling a context of 2,048 tokens multiplies LLaMA 6.7B's whole forward pass by 2.08, not 4; doubling 32,768 tokens, by 2.8; past that, the square takes over.

A recurrent layer's work grows in step with the length, as Table 1 also says, but it cannot use the chip's parallel hardware within a sentence. The field took the trade. Keeping a linear cost without the queue is an active line of work: Mamba, a state space model, reports "linear scaling in sequence length".

Why This Matters for You

  • A long context costs inside the model. Past a length set by the model's width, the scores take most of the work, and attention keeps every earlier token's key and value, so its memory grows with every token.
  • You pay by the token. Anthropic's pricing page, read on 15 November 2026, lists its models from Claude 4.6 on, except Claude Haiku 5.5, with the full 1M-token window at standard pricing; on Claude Haiku 5.5 a prompt over 100,000 tokens costs more per token. A prompt twice as long costs about twice as much to send.
  • Longer is not better by itself. Recall drops as a context grows. Sending the passages a question needs, which retrieval does, keeps both the bill and the context small.

Check Yourself

Six questions on the two layers and their price.

Check yourself: why transformers won

1 / 6

Multiple choice

A 1,000-token document goes into a recurrent layer and into an attention layer. Which can work all its tokens at once, and why?

Mental-Model Corrections

  • Transformers did not win everywhere at once. They won on training speed and on direct paths between tokens. A recurrent layer still grows only in step with the length, and new recurrent designs are an active line of work.
  • Attention's square is real, and smaller than it sounds. The scores grow with the square of the length, but for most contexts most of a forward pass is the weights; the square takes over past about 12 times the model's width.
  • A direct path is not recall. It makes every token reachable in one step; the weight a head gives is learned, and recall drops in long contexts.
  • Long prompts cost by the token, at the providers' published rates, not by the square.

Key Takeaways

  • Recurrent networks read one token at a time through a hidden state: a sentence is a queue of steps, and the first token reaches the last only through every step between them.
  • The training signal fades along that chain; a measured LSTM used about 200 tokens of context, and word order only within the last 50.
  • Transformers work every token at once and join any two tokens in one step: faster to train, direct to reach.
  • The price is a score for every pair: twice the tokens, about four times the scores. They dominate a forward pass only past about 12 times the model's width.
  • Providers bill by the token, and long contexts lose recall: send what the question needs.

Next: Hands-On: Build a Tiny GPT (Transformers Demystified in Code). Every piece of these lessons, in one small program.