esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

What Is Causal (Masked) Attention?

How causal attention hides later words from a model, why GPT-style models need it, and how to test it, explained simply for your AI interview.

In short

Causal (masked) attention lets each word look only at itself and the words before it, never at later ones. That hides the future during training and while the model writes, which is how GPT-style models predict the next word.

10 min readIntermediateCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Causal attention, also called masked attention, lets each word look only at itself and the words before it. The model scores every pair of words, then sets the scores for later words to minus infinity. After softmax those words get zero weight, so the future stays hidden. That's how a model like GPT learns to predict the next word without peeking ahead, and why it writes one token at a time.

What is causal (masked) attention, in plain words?

A language model reads text as tokens, small pieces of words. Attention is how each token decides how much to use every other token when it builds its meaning. Causal attention adds one rule: a token can look only at itself and the tokens before it.

Picture a claims adjuster writing a case note line by line. She can point back to what is already in the file, but she must not quote a page that has not been written yet. Causal attention gives the model that same rule.

Real case: an insurer's assistant drafts a reply to a policyholder, one word at a time. When it writes 'approved', it may use 'Your claim is' before that word, but not the sentence that comes after, because that text does not exist yet.

Key words

Token
A small piece of text, often part of a word, that the model reads and writes.
Attention
The step where each token weighs how much to use every other token.
Mask
A rule that blocks some pairs of tokens, here every pair where the other token comes later.
Softmax
A step that turns scores into weights that add up to 1.
Causal model
A model that predicts the next token from the tokens before it, like GPT-2.

How does the mask stop a model looking ahead?

Attention starts with a score for every pair of tokens, so a four-token reply makes a four by four table. The causal mask covers the upper-right half of that table, the pairs where the other token comes later.

How a causal mask hides the future

Every word vs every word

a score for each pair of words

Causal mask

later words get minus infinity

Softmax

turns each row into weights

Next word

picked from the words so far

  1. 1Each pair's score moves on to the mask.
  2. 2Masked scores go on to softmax, where later words count as 0.
  3. 3The visible weights pick the next word.
Each word's scores go through the mask, then softmax, which turns the visible scores into weights that add up to 1. Masked words get weight 0, so the next word can only draw on words that already exist.
  1. 1

    Score every pair

    Each token gets a score against every token, including itself.

  2. 2

    Hide the later ones

    Scores for later tokens are set to minus infinity, so they can't count.

  3. 3

    Turn scores into weights

    Softmax makes each row's visible weights add up to 1.

Here is a made-up four-token reply from a bank's drafting assistant, with the mask applied. Read each row: 'today' spreads its weight across all four words, because it comes last. 'Refund' is 1.00, because it can see only itself.

Python
# A made-up score table for four tokens of a bank's drafted reply.
import math

tokens = ["Refund", "is", "approved", "today"]
scores = [
    [1.2, 0.3, 0.1, 0.4],
    [0.5, 1.0, 0.2, 0.3],
    [0.1, 0.6, 1.4, 0.2],
    [0.3, 0.2, 0.9, 1.1],
]

def causal_softmax(row, i):
    # Future positions get -infinity before the softmax, so their weight becomes 0.
    masked = [s if j <= i else float("-inf") for j, s in enumerate(row)]
    exps = [math.exp(s) for s in masked]
    total = sum(exps)
    return [e / total for e in exps]

print("query    " + "".join(f"{t:>10}" for t in tokens))
for i, row in enumerate(scores):
    weights = causal_softmax(row, i)
    print(f"{tokens[i]:<9}" + "".join(f"{w:>10.2f}" for w in weights))

Output

query        Refund        is  approved     today
Refund         1.00      0.00      0.00      0.00
is             0.38      0.62      0.00      0.00
approved       0.16      0.26      0.58      0.00
today          0.17      0.15      0.31      0.37

Why do GPT-style models use it?

GPT is a family of models that write text one word at a time. They come from the 2017 Transformer design, which has two halves: the decoder, which writes, and the encoder, which reads a whole text at once.

Training needs the model to predict every position in a sentence. With the mask, one pass over the sentence gives a prediction at each position, and none of them can read the word it is meant to predict. Writing works the same way: one word at a time, from the words so far.

  • “We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions.”

    The Transformer paper, 2017

  • “Causal language modeling predicts the next token in a sequence of tokens, and the model can only attend to tokens on the left.”

    Hugging Face, on causal language modelling

  • “If set to true, the attention masking is a lower triangular matrix when the mask is a square matrix.”

    PyTorch, the is_causal option

  • Causal (GPT-style)

    Reads left to right

    Each word sees only the words before it. Used for writing the next word.

  • Bidirectional

    Reads the whole text

    Each word sees words on both sides. Used by models such as BERT.

What are the trade-offs?

  • Training covers every position

    One pass trains every position at once, instead of one word after another.

  • Each word sees one side only

    A causal model cannot use words after a word. A bidirectional model such as BERT can.

  • The mask must be right

    A wrong mask can let answers leak into training, so test scores can look better than the model really is.

Tip

Asked what can go wrong? Say that a leaky mask makes the model look strong in tests. Then say how you'd catch it: with the peek test below.

How would you test that a model cannot peek ahead?

Say a claims team's contract summariser looks too good in staging. You suspect the mask is leaking. Here is a simple check you can describe in an interview.

  1. 1

    Change a later word

    Take a finished prompt and swap its last word for a different one.

  2. 2

    Compare earlier scores

    Scores for earlier positions must stay the same. If they change, the mask leaks.

  3. 3

    Check the serving code

    A misconfigured serving stack might skip the mask, so test the real stack too.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: each word sees only itself and the words before it.
  • The mechanism: scores for later words set to minus infinity before softmax.
  • The why: next-word training, and writing one word at a time.
  • Where it's used: GPT-2 and similar text-writing models.
  • The trade-off: a one-sided view against a bidirectional reader.
  • How you'd test it: change a later word and check earlier scores stay the same.

Common traps

  • Trap"The model just skips the future words."

    BetterIt still computes them. The mask sets their weight to zero before softmax.

  • Trap"The mask filters the final text."

    BetterIt acts on the attention scores, before softmax, which is where the weights come from.

  • Trap"Causal attention is only for chatbots."

    BetterIt applies to any model that writes one token at a time, chat or otherwise.

Can you spot the leak?

Spot the leak

Which line hides the later words from each word?

Quick check

A bank's drafting assistant writes replies one word at a time. Which mask does it need?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q043What is self-attention?11 min
  2. Q044What are queries, keys and values?11 min
  3. Q049Encoder-only vs decoder-only vs encoder-decoder?11 min
  4. Q051What is cross-attention?10 min
  5. Q055What is the KV cache, and why does it speed up generation?12 min

Learn it in depth

Last updated on 26 November 2026

Building a chatbot that writes its own replies?

I design and build AI products and agents, including the interface and the system behind them. Book a short call and tell me what you need.