Open the diagram from the famous “Attention Is All You Need” paper and you’ll see two tall stacks of blocks sitting side by side. One is the encoder, the other the decoder, and almost every confusion people have about modern AI — why BERT and GPT are different, why “the Transformer” isn’t the same as “ChatGPT” — dissolves the moment you understand the transformer encoder vs decoder distinction. Get this one idea and a Transformer diagram stops being intimidating and starts being readable.
This guide explains both halves in plain terms: what each one does, the single most important difference between them, and why the most famous AI models each kept only one half. No heavy math, one comparison table you’ll return to, and links to go deeper.

The Original Design Had Two Halves
The Transformer was invented in 2017, in the paper “Attention Is All You Need”, for machine translation — turning an English sentence into a German one. That task has a natural two-part shape, and the architecture mirrored it exactly:
- The encoder reads and understands the input sentence, building a rich internal representation of it.
- The decoder takes that representation and generates the output sentence, one word at a time.
Read the source, then write the translation. Encoder understands; decoder generates. That division of labor is the whole reason there are two stacks in the diagram, and everything else follows from it. Both halves are built from the same core ingredient — attention — but they use it differently, and that difference is the key.
The Encoder: Built to Understand
How it reads
The encoder’s job is comprehension. It takes the whole input at once and, through self-attention, lets every word look at every other word — those before it and after it. This is called bidirectional attention, and it’s exactly what you want for understanding: to know what “bank” means in “I sat on the river bank,” you need to see “river,” which comes later. The encoder is allowed to peek in both directions because it isn’t trying to predict anything — it’s trying to represent the input as richly as possible.
What it produces
The encoder’s output is a set of context-rich embeddings — numeric representations that capture the meaning of each word in context. Those representations are gold for any task that’s about analyzing or comparing text: classification, sentiment, and especially search. It’s no accident that semantic search runs on encoder-style models.

The Decoder: Built to Generate
How it writes
The decoder’s job is generation, and that changes the rules. It produces text one token at a time, and — this is the crucial constraint — each position is only allowed to look at the words that came before it, never after. This is called masked (or causal) attention: the future is hidden. The reason is simple and strict: the decoder is being trained to predict the next word, so it must not be allowed to peek at the answer. If it could see the word it’s supposed to produce, it would learn nothing.
The two attention steps
In the original translation model, the decoder actually runs attention twice per layer: once on the text it has generated so far (masked self-attention), and once looking back at the encoder’s output (called cross-attention or encoder-decoder attention) so the translation stays faithful to the source. That second step is how the two halves talk to each other.
Encoder vs Decoder: The Full Comparison
Here’s the whole distinction in one place:
| Encoder | Decoder | |
|---|---|---|
| Core job | Understand the input | Generate the output |
| Attention direction | Bidirectional (sees all words) | Masked / causal (sees only past words) |
| Can it “peek ahead”? | Yes — that’s the point | No — it’s predicting the future |
| Natural output | Context-rich representations | New text, token by token |
| Best at | Analysis, classification, search | Writing, chat, completion |
| Famous example | BERT | GPT (and ChatGPT, Claude) |
The single row that matters most is attention direction. Bidirectional (encoder) means “understand by seeing everything.” Masked (decoder) means “generate by seeing only what came before.” That one difference determines what each half is good for — and it’s why the AI world split into two families.
Why the Famous Models Each Kept One Half
Here’s the twist that makes this worth understanding: the biggest models today aren’t the full encoder-decoder Transformer at all. Researchers realized you often only need one stack, and the two most influential AI families each kept a different one.
BERT: encoder-only
Google’s BERT (2018) kept just the encoder — the clue is right in its name, which stands for Bidirectional Encoder Representations from Transformers (original paper). It’s a pure understanding machine — bidirectional, great at representing text, useless at open-ended generation (that’s not its job). BERT and its descendants power search, classification, and the embeddings behind semantic search and retrieval. When you need to understand text, you reach for an encoder.
GPT: decoder-only
OpenAI’s GPT family kept just the decoder. It’s a pure generation machine — masked attention, predicting the next token over and over. This is the architecture behind ChatGPT, Claude, and essentially every chatbot, and it’s why large language models are described as “decoder-only.” When you need to generate text, you reach for a decoder.
Encoder-decoder: still alive
The original both-halves design didn’t disappear — it lives on in models built for transforming one text into another, like Google’s T5 and most dedicated translation systems. Input in, transformed output out: the task the Transformer was born for.
This is the payoff of the whole distinction: “the Transformer” is a family, not a single model. The next time someone uses those terms interchangeably, you’ll know that BERT (encoder) and GPT (decoder) are as different as a reader and a writer — same alphabet, opposite jobs.
Which One Should You Use?
If you ever reach for a pre-built model, this distinction stops being trivia and becomes a practical choice. The task tells you which half you want.
Reach for an encoder when the job is understanding
Any task where the output is a judgment about text — not new text — wants an encoder. Classifying an email as spam or not, detecting sentiment, grouping similar documents, and above all search: you embed your documents with an encoder and compare them by meaning. If the answer you need is a label, a score, or a “which of these is most similar,” an encoder-style model (BERT and its many descendants) is the right tool.
Reach for a decoder when the job is generating
Any task where the output is new text wants a decoder. Writing, summarizing, answering questions conversationally, completing code, drafting — all generation, all decoder-only territory. This is why the general-purpose assistants (ChatGPT, Claude, Gemini) are decoder-only: chatting is open-ended generation, and a decoder is the machine built for it.
Reach for encoder-decoder when the job is transforming
When you have a specific input and want a specific transformed output — translate this, rewrite this in a different style, convert this to a summary of a fixed shape — the original both-halves design (like T5) still fits best. Read the source with the encoder, write the result with the decoder.
In practice, a lot of modern work leans decoder-only, because a big enough decoder can imitate the other two jobs reasonably well by generating a classification or a translation as text. But knowing the native tool for each job helps you understand why a given model behaves the way it does — and why, for example, you don’t hand a chatbot a pure-understanding task and expect a clean label.
❓ Frequently Asked Questions
What is the difference between a Transformer encoder and decoder? The encoder’s job is to understand input — it uses bidirectional attention, so every word can see every other word. The decoder’s job is to generate output — it uses masked attention, so each position can only see the words before it (because it’s predicting the next word and mustn’t peek at the answer). Encoder understands; decoder generates.
Is GPT an encoder or a decoder? GPT is decoder-only. It kept just the decoder half of the original Transformer because its job is generating text one token at a time. ChatGPT and Claude are decoder-only models too. BERT, by contrast, is encoder-only, built for understanding rather than generating.
What does “masked attention” mean? It means the decoder is only allowed to attend to (look at) tokens that came before the current position — the future is “masked” out. This is essential because the decoder is trained to predict the next word; if it could see that word, it couldn’t learn to predict it. The encoder has no such mask and can see the whole input.
Why did BERT and GPT each use only half of the Transformer? Because you often only need one. BERT’s goal is understanding text, which needs only the bidirectional encoder. GPT’s goal is generating text, which needs only the masked decoder. Keeping a single stack made each model simpler and better-suited to its task. The full encoder-decoder design is still used for translation-style “text in, transformed text out” tasks.
What is cross-attention? In the original encoder-decoder Transformer, cross-attention (also called encoder-decoder attention) is the step where the decoder looks back at the encoder’s output while generating — so the output stays faithful to the input. It’s how the two halves communicate. Decoder-only models like GPT don’t have it, since they have no separate encoder.
📚 Further Reading
- How Transformer Attention Works, Step by Step — the mechanism both halves are built from, in detail.
- What Is “Attention” in AI? — the plain-English foundation.
- How Large Language Models Work — why the chatbots you use are decoder-only.
- How Semantic Search Works — where the encoder side shines.
📚 Recommended Resources
Disclosure: some links below are Amazon affiliate links. If you buy through them, I earn a small commission — at no extra cost to you — which helps keep this site running.
Build a Large Language Model (From Scratch)
Sebastian Raschka · Book · ~$50 on Amazon
The clearest way to internalize the encoder/decoder distinction is to build a decoder-only model yourself. Raschka walks you through a GPT-style Transformer in plain PyTorch, step by step — masked attention included.
Natural Language Processing with Transformers (Revised Edition)
Lewis Tunstall, Leandro von Werra & Thomas Wolf · Book · ~$60 on Amazon
Written by the Hugging Face team, this is the standard practical guide to working with both encoder models (like BERT) and decoder models (like GPT) — loading, fine-tuning, and shipping them.
Deep Learning with Python
François Chollet · Book · ~$50 on Amazon
The most approachable on-ramp to the neural networks underneath — including the attention mechanisms that both the encoder and decoder are built from. From the creator of Keras.