Causal Attention

An attention pattern that lets each token read earlier tokens and itself, but not later tokens, so the model can predict text one step at a time.

Causal attention is the decoder-style attention pattern that blocks future tokens. Each position can use the tokens that already exist in the prefix and its own position, but it cannot peek at tokens that have not been generated yet.

At a glance

Released

June 2017

Authors

Ashish Vaswani, Noam Shazeer, Niki Parmar, et al.

Optimizes

  • Autoregressive Generation
  • Next Token Prediction

What It Is

Causal attention is the masked version of self-attention used for next-token prediction. For a query at position t, the mask keeps keys from positions 0 through t visible and blocks positions after t.

Why It Exists

The mask preserves the training and inference rule that the model must predict the next token from only the prefix it has already seen. Without that restriction, a decoder could cheat by reading the answer from later positions.

How It Works

Queries, keys, and values are computed as in ordinary self-attention, but the score matrix is multiplied by a causal mask before softmax. That mask keeps the diagonal and everything to the left, while zeroing scores for future positions. The result is an attention pattern where each token can read its earlier context and itself, but not tokens that come later in the sequence.
Causal attention stops at the current token, while bidirectional attention can also read later positions.

Math Or Compute Schema

The formulas below contrast causal attention with bidirectional attention. The projections stay the same, but causal attention applies a triangular mask that blocks future positions, which is what makes token-by-token autoregressive generation possible.
Causal attention
Attention(Qi,Ki,Vi)=softmax ⁣(Mcausal⊙QiKi⊤dk)Vi\text{Attention}(Q_i, K_i, V_i) = \mathrm{softmax}\!\left(M_{\mathrm{causal}} \odot \frac{Q_i K_i^{\top}}{\sqrt{d_k}}\right) V_i
QQ
Query vectors for head i.
KK
Key vectors for head i.
VV
Value vectors for head i.
HH
Number of query heads.
dkd_k
Key dimension per head.
ii
Query head index.
Bidirectional attention
Attention(Qi,Ki,Vi)=softmax ⁣(Mbi⊙QiKi⊤dk)Vi\text{Attention}(Q_i, K_i, V_i) = \mathrm{softmax}\!\left(M_{\mathrm{bi}} \odot \frac{Q_i K_i^{\top}}{\sqrt{d_k}}\right) V_i
QQ
Query vectors for head i.
KK
Key vectors for head i.
VV
Value vectors for head i.
HH
Number of query heads.
nn
Visible sequence length.
dkd_k
Key dimension per head.
ii
Query head index.
MbiM_{\mathrm{bi}}
Bidirectional mask that keeps both left and right context available inside the visible sequence.

Compared To Nearby Modules

Compared with bidirectional attention, causal attention gives up right-context access so the model can produce text one token at a time. Compared with the broader attention page, this page focuses on the no-looking-ahead mask rather than the full query-key-value mechanism.
Comparison dimensionCausal AttentionBidirectional AttentionAttention Overview
Visible context per queryEarlier tokens plus the current token onlyBoth earlier and later tokens in the same visible sequenceDepends on the architecture and mask pattern chosen for the attention block
Best-fit task styleDecoder-style next-token prediction and autoregressive generationEncoder-style understanding and masked language model objectivesGeneral attention lookup across many Transformer designs
Generation behaviorProduces output one token at a time without looking aheadNot suitable for plain next-token decoding because future tokens stay visibleVaries with the chosen mask, sequence source, and objective

Example Architectures

Causal attention appears in decoder-only transformer stacks and on the decoder side of encoder-decoder systems when the output must be generated step by step.

Limitations And Tradeoffs

Because each token cannot read future positions, causal attention gives up some contextual information that encoder-style full-context attention can use. Long outputs also require repeated decode steps, so latency depends on token-by-token generation rather than one full-sequence pass.

Why It Still Matters

Most chat and text-generation systems still rely on autoregressive decoding. Understanding causal attention explains why decoders need a key-value cache, why generation unfolds left to right, and why future tokens stay hidden during both training and inference.

Tags

References

  1. Vaswani, Ashish, et al. "Attention Is All You Need." arXiv, 2017, https://arxiv.org/abs/1706.03762.