# Masked vs Causal Language Modelling

> How masked and causal language models learn, which one reads both sides of a word, and which to pick for each job, explained simply for your AI interview.

**In short:** A masked model hides some words and predicts them from the words on both sides, which suits understanding text. A causal model predicts the next word from the words on its left only, which is how it writes text.

AI and LLM interview question Q063, Transformer architecture. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/masked-vs-causal-language-modelling

## Say it in 30 seconds

A masked model is shown a sentence with some words hidden, and it guesses them from the words on both sides. BERT is the classic example, and it suits reading jobs like sorting claims or finding clauses. A causal model sees only the words to the left and predicts the next word. That is how GPT-style models write. When I pick one, I ask whether the job is to understand text, which means masked, or to write it, which means causal.

## Say it in 2 minutes

Both kinds of model learn by guessing words from real text. The difference is what they may look at when they guess. A masked model hides some words and predicts each hidden one from the words on both sides of the gap. BERT, introduced in 2018, works this way.

A causal model reads only the words before a point and predicts the next word. That is the model used to write text, one token at a time. During training, the writing part of the model is stopped from looking at the positions after the one it is predicting, so it can't see the answer it is about to give.

In an interview I'd state the trade-off plainly. Masked models suit reading jobs, like sorting insurance claims or finding a clause in a contract. Causal models suit writing jobs, like drafting a customer reply. Neither is better in general, so a team should test each job on its own.

I'd also name the honest limit. Masked training uses a [MASK] token that does not appear during fine-tuning (training again on a specific task), so the model sees a mismatch when it is later fine-tuned. The BERT paper admits this, and says it tries to soften the mismatch during training. In an interview, I'd also say how I'd check the effect: with a test set of real past claims, each one labelled by a person.

## What is the difference, in plain words?

A language model learns by guessing words from real text. Masked and causal are two ways to set up that guessing game. The difference is what the model is allowed to look at when it guesses.

Picture a claims adjuster who gets a damaged-claim form with one word blacked out. She reads the lines above and below the gap, then fills it in. A second adjuster reads only the lines above a point and writes what comes next. The first is masked. The second is causal.

In AI, BERT is the classic masked model, introduced in 2018. GPT-style models are causal, and causal language models are frequently used for text generation.

- **Token:** A small piece of text the model reads, often a word or part of one.
- **Masked language model:** A model that hides some tokens in a text and predicts them from the rest.
- **Causal language model:** A model that predicts the next token from the tokens before it.
- **Bidirectional:** Reads in both directions, so a word sees the words on its left and its right.
- **Transformer:** The neural network design behind most modern language models. It uses attention to weigh each word against the others.

## What does each model get to see?

The chart shows what each model sees, and what it must produce, for one short claims note.

*Diagram, What each model sees while it learns: The masked model reads the note with one word hidden and fills the gap from both sides. The causal model reads only the words written so far and predicts the next one.*
1. The masked model gets the note with one word hidden.
2. It reads both sides of the gap and fills it in.
3. The causal model gets only the words written so far.
4. It predicts the next word from its left side alone.

The sources put the two ways in their own words:

> "In order to train a deep bidirectional representation, we simply mask some percentage of the input tokens at random, and then predict those masked tokens." (BERT paper (Devlin et al., 2018))
>
> "We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions." (Transformer paper (Vaswani et al., 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 docs)

## How does a masked model learn?

BERT hides a share of the tokens at random, then predicts only the hidden ones. Its experiments hid 15% of the tokens in each sequence. Because the model sees words on both sides of each gap, it learns rich context for every word.

Hiding is what makes the two-sided view safe. The BERT paper warns that bidirectional conditioning would allow each word to indirectly "see itself", and the model could trivially predict the target word.

Take a claims note that says "Claimant reports water damage since Tuesday." Hide the word water. The model reads Claimant reports on the left and damage since Tuesday on the right. Those clues point to water, and the guess is scored against the real word.

## How does a causal model learn?

A causal model is trained to predict the next token from the tokens before it. It can only look left. Hugging Face's docs put it plainly: causal modelling predicts the next token, and the model can only attend (pay attention) to tokens on the left.

The Transformer paper handles this with a mask inside the decoder, the part of the model that writes output. Later positions are blocked, so training can't leak the answer.

1. **Read the text so far:** The model gets the first words of a note, such as the start of a claims note.
2. **Block everything after the point:** Words to the right of the current position are hidden, so the model can't copy them.
3. **Predict the next word:** The guess is scored against the real next word, and the model is nudged to do better.
4. **Repeat for every position:** One sentence gives many practice questions, one for each word.

Here is the same idea for one claims note, in plain Python. It makes one practice question per word, then the masked version with one word hidden.

```python
# Two ways to make practice questions from one claims note. Standard library only.
note = ["Claimant", "reports", "water", "damage", "since", "Tuesday"]

print("Causal: predict the next word from the words on its left")
for i in range(1, len(note)):
    print("  " + " ".join(note[:i]), "->", note[i])

print("Masked: hide one word, predict it from both sides")
hidden = 2  # picked by hand, so every run prints the same thing
masked = ["[MASK]" if i == hidden else w for i, w in enumerate(note)]
print("  " + " ".join(masked), "->", note[hidden])
```

Output:

```text
Causal: predict the next word from the words on its left
  Claimant -> reports
  Claimant reports -> water
  Claimant reports water -> damage
  Claimant reports water damage -> since
  Claimant reports water damage since -> Tuesday
Masked: hide one word, predict it from both sides
  Claimant reports [MASK] damage since Tuesday -> water
```

## Which model fits which job?

The job decides the choice. Masked models suit jobs where the answer depends on the whole text at once, like sorting a claim or finding a clause. Hugging Face calls masked modelling "great for tasks that require a good contextual understanding of an entire sequence." Causal models suit jobs that write new text, one token at a time.

*Diagram, Pick the model by the job: Reading jobs go to the masked model and writing jobs go to the causal model. A person checks each drafted reply before it goes out.*
1. The claim text goes to the masked model to be sorted.
2. A contract search goes to the masked model too, to find the clause.
3. A customer reply goes to the causal model, which writes it.
4. The masked model's answer is a label for the claim.
5. The causal model's answer is a draft that a person checks first.

> **Tip:** In an interview, say masked for reading and classifying, and causal for writing and drafting. Then add that you would test the choice on your own data, not trust the model's name.

Large causal models can also read and classify when you prompt them, so treat this split as a starting point and test it on your own data.

### How would you choose for a claims team?

Say the team wants two things: sort incoming claims by cause, and draft replies to customers. I would test a masked model on the sorting, since the label depends on the whole claim. I would test a causal model on the drafts, since each reply is written word by word.

Each choice gets its own test set, built from real past claims, and a cost check, because a larger model costs more on every call.

> **Tip:** A masked model fills gaps. It is not built to write a long reply from start to finish, so do not use it for drafting.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] What each model sees: both sides for masked, the left side only for causal.
- [ ] BERT is masked, and GPT-style models are causal.
- [ ] Masked suits reading jobs. Causal suits writing jobs.
- [ ] Why hiding matters: a model that sees the answer copies it.
- [ ] The trade-off: masked training uses a [MASK] token that fine-tuning text never has.
- [ ] Test each job on your own data, not on the model's name.

**Common traps**

- Trap: "Masked models can write long replies too." Better: They fill gaps. Writing a reply from start to finish is the causal model's job.
- Trap: "BERT and GPT only differ in size." Better: They differ in what they see during training. Size is a separate choice.
- Trap: "Causal models read the whole text before they answer." Better: A causal model reads only the words to the left. That is what lets it write one word at a time.

## Can you spot the cheat?

**Spot the cheat:** A team trains a model to predict each word. Which setup lets the model cheat?

A. It predicts each word from the words before it only.
B. It can see the very word it must predict, so the answer sits in its input.
C. It hides some words and predicts them from the rest.
D. It splits the training text into short sentences first.

**Answer:** B. If the answer is in the input, the model learns to copy rather than guess. Causal and masked training both block that path, in different ways.

**Quick check:** A bank's support team wants an assistant that drafts replies to customers about disputed card charges. Which model type fits best?

A. A masked model, because it reads both sides of a word
B. A causal model, because it writes the next word from the words before it
C. A masked model, because it hides words during training
D. A classifier that only labels the customer's message

**Answer:** B. Drafting writes new text one word at a time, which is what a causal model does. A masked model fills gaps in text that already exists.

## Can you answer these from memory?

**Flash cards**

- **What does a masked model predict?** Hidden words, using the words on both sides of the gap.
- **What does a causal model predict?** The next token, using only the tokens before it.
- **Which kind is BERT?** Masked. It hides tokens and predicts them from both sides.
- **Why must the decoder block later words?** Otherwise training leaks the answer, and the model learns to copy it.
- **Which kind fits claims sorting?** Masked, because the label depends on the whole claim at once.
- **Which kind fits drafting replies?** Causal, because it writes new text one token at a time.

## Likely follow-up questions

- Q049 Encoder-only vs decoder-only vs encoder-decoder? https://designervenkat.online/interview/ai/encoder-only-vs-decoder-only-vs-encoder-decoder
- Q050 What is causal (masked) attention? https://designervenkat.online/interview/ai/what-is-causal-masked-attention
- Q062 How does BERT differ from GPT? https://designervenkat.online/interview/ai/how-does-bert-differ-from-gpt
- Q065 Prefill vs decode phases? https://designervenkat.online/interview/ai/prefill-vs-decode-phases
- Q068 How does the final hidden state become the next token? https://designervenkat.online/interview/ai/how-does-the-final-hidden-state-become-the-next-token

## Learn it in depth

- [Transformers](https://designervenkat.online/learn/machine-learning/transformers)
