State Space Models and Mamba. How Linear-Time Models Challenge Attention
A transformer never forgets a token, and it pays for that on every step. A state space model keeps a memory of fixed size and updates it as it reads, so its cost per token stays flat no matter how long the input runs. That single design choice explains both why models like Mamba are fast and where they fall short, and it is why the most useful long-context designs today mix the two.
What Attention Costs You
Earlier parts of this series showed that attention lets every token look directly at every other token. That direct access is the source of a transformer's precision. It is also the source of two bills that grow with the length of the input.
The first bill is compute. Each new token is compared against all the tokens before it, so the work for a full sequence grows with the square of its length. The second bill is memory. To avoid redoing that work, a transformer stores a set of intermediate values for every past token in every layer. Engineers call this store the key-value cache. It grows with each token the model reads or writes, and at long context lengths it, and not the model weights, becomes the thing that fills the accelerator.
For a short chat turn neither bill matters. For a contract archive, a codebase, a day of sensor readings, or an agent that runs for hours, both do. This is the problem state space models were built to solve.
A Fixed-Size Memory, Updated as It Reads
A state space model, or SSM, borrows an idea from control engineering. Instead of keeping every past token around, it keeps one summary of everything seen so far, called the state. Each new token updates the state, and the output is read from the state.
y_t = C * h_t
Here x is the incoming token, h is the state, and y is the output. A decides how much of the old state to keep, B decides how much of the new token to write in, and C decides what to read out. The state has the same size at token ten and at token ten million. Compute per token is constant, so total compute grows in a straight line with length, and there is no cache that swells as the input gets longer.
The first version of this idea to work at scale was S4, which found a stable and efficient way to compute these updates over very long inputs [2]. Its headline result was on Path-X, a benchmark task with inputs 16,384 steps long. S4 reached 88 percent accuracy on a task where all earlier models scored no better than the 50 percent you get from guessing [2]. It proved that a compressed running memory could carry information across distances that attention models of the time could not afford to span.
Selection, the Idea That Made Mamba Work on Language
Early SSMs were strong on audio and other continuous signals and weak on text. The reason is easy to state. In the equations above, A, B, and C were the same for every token. The model wrote a filler word into its memory with the same weight as a name or a number. Language needs the opposite behavior, where the model keeps what matters and lets the rest pass.
Mamba fixed this by making the update depend on the token being read [1]. The authors call this a selective state space. In plain terms, the model learns a gate for each token that says how strongly to write it into memory and how much old memory to clear. A token that matters is written firmly. A token that does not is close to ignored. This is the same instinct behind attention, deciding what is relevant, applied at write time instead of at read time.
Making the update depend on the input broke the mathematical shortcut that had made earlier SSMs fast, so Mamba paired the idea with an implementation designed around how accelerators move data. The result reported in the paper is inference throughput five times higher than transformers of similar size, with cost that scales linearly in sequence length [1].
The Fixed State Is Also the Limit
A memory that never grows cannot hold an unbounded amount of detail. That is the honest tradeoff, and two results make it precise.
The first concerns exact recall. One study showed that a transformer with only two layers can copy strings whose length is exponential in its size, while any model with a fixed-size state is limited by how much that state can hold [5]. The same study tested pretrained models and found that transformers outperformed Mamba models of similar size at copying and at pulling specific facts back out of the context [5]. If your task is to quote clause 14.3 word for word from a long agreement, direct lookup beats a summary.
The second concerns reasoning over a changing situation. It is tempting to assume that a model with a running state must be naturally good at tracking state, such as who holds which item after a long series of swaps. A formal analysis found otherwise. SSMs sit in the same class of computational power as transformers, which rules out some simple tracking problems for both, and Mamba-style models struggled with those problems in experiments [6]. The same paper shows that a small change to the design lets these models learn the tracking problems, so the limit belongs to the designs in use today and not to the idea itself [6]. A running memory is an efficiency gain. It is not a new kind of reasoning.
Attention looks things up. A state space model remembers a summary. Most real workloads need both, in different proportions.
Mamba-2 and the Bridge Back to Attention
For a while SSMs and transformers looked like rival families. The follow-up work on Mamba showed they are closer relatives than that. Its authors proved that a broad class of state space models and a class of attention variants are two ways of writing the same underlying computation, a connection they call structured state space duality [3].
That finding has a practical payoff. Years of engineering that made attention fast and easy to train could now be carried over to SSMs. The resulting design, Mamba-2, has a core layer the authors report as two to eight times faster than the original while staying competitive with transformers on language modeling [3]. It also made the two layer types easy to place side by side in one network, which is where the field went next.
Hybrid Stacks, the Practical Answer
If attention gives precise recall at a growing cost and SSMs give cheap long-range memory with lossy detail, the obvious move is to use a little of the first and a lot of the second. Jamba is the clearest published example [4]. Its block design uses one attention layer for every seven Mamba layers, and it adds the sparse expert layers covered in Part 7 to raise capacity without raising per-token compute.
Because the key-value cache exists only in attention layers, cutting the number of attention layers cuts the cache by roughly the same share. The paper states that trading attention layers for Mamba layers reduces the total size of that cache, and it reports strong results at context lengths up to 256K tokens [4]. The authors also describe the ratio as a dial. More attention buys recall and costs memory. More SSM layers buy throughput and long reach and cost some precision [4].
This connects directly to Part 8, which covered how transformers stretch their context. Hybrids attack the same problem from the architecture side instead of the serving side.
Four Questions Before You Choose
You will rarely train one of these models yourself. You will choose between hosted and open-weight models built on them, and the architecture should inform that choice. These four questions sort most workloads.
- Does the task need exact text back? Quoting, citing, extracting identifiers, and code editing all depend on verbatim recall. That favors attention-heavy models. A pure SSM is the wrong tool here.
- Is the input long and mostly background? Logs, transcripts, telemetry, and genomic or audio streams reward a model that can read cheaply and keep the gist. That favors SSM-heavy or hybrid models.
- Is memory on the accelerator your binding limit? If you run on a single device or at the edge, a growing cache caps your context long before compute does. A hybrid lets you hold more context in the same memory.
- Does the work run as a long session? Agents that act for hours accumulate context continuously. Flat cost per token matters more the longer the session runs, provided you pair it with a retrieval step for the facts that must be exact.
A red flag in any vendor conversation is a long-context claim with no recall test behind it. Ask for accuracy on finding a specific fact placed early, in the middle, and late in an input of the length you plan to use. A model can accept a very long input and still fail to return what is in it.
Implications for Enterprise Deployment
Three things follow for teams planning deployments.
First, stop reading context window size as a single capability number. Two models that both accept the same input length can differ sharply in what they retain from it and in what it costs to serve. Test on your own documents, at your own lengths, with questions that require exact answers.
Second, expect the cost curve for long inputs to bend. Workloads that were too expensive to run over full histories, such as reviewing a whole case file or a full maintenance record in one pass, become practical when cost per token no longer climbs with length. That changes which use cases belong in a pilot.
Third, keep retrieval in the design. A fixed-size state is a summary, and summaries lose detail. The durable pattern is a model with cheap long-range memory for the gist, plus a retrieval step that fetches the exact passage when precision matters. The architecture lowers the cost of reading. It does not remove the need to look things up.
- Part 1: How Do LLMs Work?
- Part 2: What Is a Transformer?
- Part 3: LLM Tokenization Explained
- Part 4: LLM Pretraining Explained
- Part 5: Fine-Tuning vs RLHF
- Part 6: LLM Scaling Laws
- Part 7: Mixture of Experts
- Part 8: Long Context and Memory
- Part 9: Multimodal LLMs
- Part 10: LLM Agents and Tool Use
- Part 11: Recursive Self-Improvement
- Part 12: Running LLMs Locally
- Part 13: State Space Models and Mamba
References
- [1] Gu, Dao. "Mamba: Linear-Time Sequence Modeling with Selective State Spaces." arXiv 2023. arXiv:2312.00752
- [2] Gu, Goel, Ré. "Efficiently Modeling Long Sequences with Structured State Spaces." ICLR 2022. arXiv:2111.00396
- [3] Dao, Gu. "Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality." ICML 2024. arXiv:2405.21060
- [4] Lieber et al. "Jamba: A Hybrid Transformer-Mamba Language Model." arXiv 2024. arXiv:2403.19887
- [5] Jelassi, Brandfonbrener, Kakade, Malach. "Repeat After Me: Transformers are Better than State Space Models at Copying." ICML 2024. arXiv:2402.01032
- [6] Merrill, Petty, Sabharwal. "The Illusion of State in State-Space Models." ICML 2024. arXiv:2404.08819