# Sparse attention

> Canonical: https://skillalmanac.com/glossary/sparse-attention | Last verified: 2026-11-25 | Index status: index-eligible

Sparse attention uses fewer direct token-pair connections than dense attention, aiming to reduce computation and memory. Patterns can be fixed or data-dependent; some tokens may still have global attention. Costs and quality depend on the design and workload.

Also called: sparse attention transformer, sparse attention mechanism, sparse transformer, sparse self-attention, sparse attention LLM.

## In plain language

Reading a long report, you do not compare every sentence with every other sentence. You look at nearby sentences and a few key ones, such as the title and summary. Sparse attention gives a language model the same shortcut. Unmasked dense attention compares all token pairs within a layer; causal masks exclude future positions. Sparse patterns omit direct connections, but their selection may be fixed or data-dependent, and information can still travel indirectly across layers. A vendor may not disclose a hosted model’s pattern. Compute and memory effects depend on the chosen design and workload.

## Technical definition

The Sparse Transformers paper (arXiv 1904.10509) notes that standard Transformers need time and memory that grow quadratically with sequence length, and introduces sparse factorizations of the attention matrix that reduce this to O(n sqrt(n)). The Longformer paper (arXiv 2004.05150) combines local windowed attention with task-motivated global attention, so the cost scales linearly with sequence length, and describes it as a drop-in replacement for standard self-attention. The Hugging Face documentation for Longformer describes global_attention_mask, where 0 means local attention and 1 means global attention, for tokens that attend to all other tokens and that all other tokens attend to. This differs from attention_mask, where 0 masks padding and 1 marks unmasked tokens. Reformer uses locality-sensitive hashing, an example of content-dependent selection rather than a universally fixed pattern.

## How it shows up when you use agents

Sparse attention is an internal model choice, not something a coding-product name establishes. Longer retained inputs can require more work, but latency and charges depend on the implementation, cache, workload and provider or plan; architecture alone does not set the bill. The [context window](/glossary/context-window) is finite. Research on sparse patterns exists because the standard [attention mechanism](/glossary/attention-mechanism) in a [Transformer](/glossary/transformer) has time and memory costs that grow quadratically with sequence length, as the Sparse Transformers paper states.

Where it can appear: in open model code you run yourself, such as the Longformer classes in Hugging Face Transformers, where global_attention_mask marks local versus global attention, separately from the padding attention_mask. For a hosted model, the vendor may not say which pattern it uses, so check its documentation instead of assuming.

What goes wrong when it is misunderstood: people read "sparse" as "the model ignores most of your text" and trim prompts blindly, or they assume a larger window means the model uses every token equally. A window size and an attention pattern are separate facts.

Next action: treat context as a budget. Decide what a task needs, put that in first, and use a packing step like the one in the catalogue. Test on your own long inputs instead of relying on a design name.

## Common confusions

- **Sparse attention versus a longer context window.** A window is how much input a model accepts. Sparse attention is one way to make longer inputs cheaper to compute. A model can have one without the other.
- **Sparse attention versus Flash Attention.** Both concern attention cost, but they are different ideas. See the [Flash Attention](/glossary/flash-attention) entry; this entry makes no claim about how they compare.
- **Local versus global attention.** In Longformer, local attention is a sliding window and global attention is used for chosen tokens that attend to all others and are attended by all others.
- **Sparse attention versus mixture of experts.** Sparse attention changes which tokens attend to which. Mixture of experts is a different design that activates parts of a network. Do not use the word "sparse" alone to tell them apart.
- **Myth.** Sparse means worse. The Longformer abstract reports results on long-document tasks, and this entry makes no general quality claim in either direction.

## Worked examples in this catalogue

### [What Is an LLM? The Facts Agent Users Need](https://skillalmanac.com/learn/what-is-an-llm-for-agent-users)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The current guide contrasts the original Transformer decoder's autoregressive generation with BERT's masked pretraining, and distinguishes runtime context from learned parameters.

Why it exercises the concept: Sparse attention is one design aimed at making long inputs cheaper inside the model. The guide covers the limit you meet from outside the model. It does not say which attention pattern any model uses.

### [context-window-packer](https://skillalmanac.com/skills/skill-almanac/context-window-packer)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill chooses which files fit one prompt's token budget and records what was kept, shrunk or dropped, and why.

Why it exercises the concept: A contrast case. Sparse attention changes which token pairs a model compares inside one input. The packer decides what goes into the input at all, which is a choice you control whatever the architecture.

### [llm-cost-estimator](https://skillalmanac.com/skills/skill-almanac/llm-cost-estimator)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill turns a usage profile and your own price table into a monthly LLM cost range.

Why it exercises the concept: Input length affects cost, and attention design is one reason long inputs are costly to serve. The skill uses the prices you supply and does not model attention. Prices are a service policy, not a property of sparse attention.

## Related terms

- [Attention mechanism](https://skillalmanac.com/glossary/attention-mechanism.md)
- [Transformer](https://skillalmanac.com/glossary/transformer.md)
- [Context window](https://skillalmanac.com/glossary/context-window.md)
- [Multi-head attention](https://skillalmanac.com/glossary/multi-head-attention.md)
- [KV cache](https://skillalmanac.com/glossary/kv-cache.md)
- [FlashAttention](https://skillalmanac.com/glossary/flash-attention.md)

## Sources

- [Generating Long Sequences with Sparse Transformers](https://arxiv.org/abs/1904.10509), arXiv (accessed 2026-11-24)
- [Longformer: The Long-Document Transformer](https://arxiv.org/abs/2004.05150), arXiv (accessed 2026-11-24)
- [Longformer](https://huggingface.co/docs/transformers/model_doc/longformer), Hugging Face (Transformers documentation) (accessed 2026-11-24)
- [Reformer: The Efficient Transformer](https://arxiv.org/abs/2001.04451v2), Kitaev, Kaiser and Levskaya (arXiv) (accessed 2026-11-25)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
