Lesson 10.4 · 4 of 7 · 6 min read · Reviewed 24 Nov 2026
Encoder-only, decoder-only and encoder-decoder
By the end, you can
- Tell encoder-only, decoder-only and encoder-decoder transformers apart by their attention masks and training goals.
- Match each family to the jobs it suits, from embeddings to chat.
The original transformerTransformer: The neural network architecture behind modern LLMs, built from stacked attention and feed-forward layers. had two halves: an encoder that read a sentence in one language and a decoder that wrote it in another. Later families kept one half, or both, and each choice suits different jobs. The difference comes down to two things: which tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. may attend to which (the mask), and what the model was trained to predict.
The three families
| Encoder-only | Decoder-only | Encoder-decoder | |
|---|---|---|---|
| Attention | Two-way: every token sees every token | Causal: each token sees only earlier ones | Encoder two-way; decoder causal plus cross-attention to the encoder |
| Trained to | Fill in masked words | Predict the next token | Write the target text from the input text |
| Output | A vector per token, or one per text | Text, one token at a time | Text, one token at a time |
| Examples | BERT, RoBERTa, many embedding models | GPT-style models, Llama, Mistral, most chat LLMs | Original transformer, T5, BART |
| Good at | Classification, embeddings, search, tagging | Open-ended generation, chat, code, tool use | Translation, summarisation, other input-to-output jobs |
A worked example: one sentence, three masks
Take “the bank was closed” and look at what bank can see.
- Encoder: the, bank, was, closed. Seeing closed helps decide it is a building, not a river bank. That is why encoders make strong embeddingsEmbedding: A list of numbers (a vector) that represents the meaning of a piece of text, so similar meanings land close together.: every token’s vector reflects the whole sentence.
- Decoder: only the and bank. It must make sense of bank before seeing the rest, because at generation time the rest doesn’t exist yet.
- Encoder-decoder: the encoder reads the full sentence both ways. The decoder, writing say a French translation, attends causally to its own output so far and, through cross-attention, to every encoder vector.
As grids, where a filled cell means “row token may attend to column token”:
Encoder (two-way) Decoder (causal)
t b w c t b w c
t x x x x t x . . .
b x x x x b x x . .
w x x x x w x x x .
c x x x x c x x x x
Why decoder-only won for chat
A decoder-only model needs only raw text to train: every token is a prediction target, so nothing is wasted. The same model can do translation, summarisation and classification by putting the task in the prompt. That generality, and how well it improves with scale, is why most general-purpose LLMsLarge language model (LLM): A neural network trained on large amounts of text to predict the next token, which lets it generate and transform language. today are decoder-only. Vendors don’t always publish full architecture details, so treat this as the common pattern rather than a fact about every product.
When the other families still win
- Embeddings for search (chapter 4): encoder-based models are small, fast and see the whole text at once.
- RerankersReranking: Re-scoring an initial set of retrieved results with a more accurate model to put the best ones first.: a cross-encoder reads the query and a passage together and outputs one relevance score.
- High-volume classification (lesson 9.1): a fine-tunedFine-tuning: Continuing to train a model on your own examples to change its behaviour or specialise it. encoder is cheap and easy to evaluate.
- Fixed input-to-output tasks such as translation, where encoder-decoder models remain strong for their size.
A typical RAGRetrieval-augmented generation (RAG): Retrieving relevant documents at request time and adding them to the prompt so the answer is grounded in them. system uses an encoder to embed, maybe a cross-encoder to rerank, and a decoder to write the answer.
Common mistakes
- Using a chat model to make embeddings when a purpose-built embedding modelEmbedding model: A model trained to turn text (or images) into embeddings for search and comparison, rather than to generate text. is cheaper and usually better at it.
- Expecting an encoder to write text. BERT-style models fill gaps; they don’t generate long answers.
- Assuming encoder-decoder is obsolete. It is less common for chat, but still a good fit for some jobs.
Key points
- Encoder-only models (such as BERT) let every token see every other token, and are trained to fill in hidden words; they are good at understanding, not writing.
- Decoder-only models (such as GPT-style and Llama-family models) use a causal mask and next-token prediction; most chat LLMs are built this way.
- Encoder-decoder models (such as T5) read the input with an encoder and write the output with a decoder that attends to it through cross-attention.
- Embedding models and rerankers are often encoder-based, so a RAG system can use all the families at once.
Compare an encoder and a decoder locally
Run an encoder-only and a decoder-only model on your own machine and see the difference in what each produces.
You will have: A short notes file with a fill-mask result from BERT, a continuation from GPT-2, and one sentence on which suits search and which suits chat.
Steps
- Create a virtual environment and install the libraries: python -m venv .venv, activate it, then pip install transformers torch.
- Run a fill-mask pipeline with bert-base-uncased on "The bank was [MASK] on Sunday." and note the top five words.
- Run a text-generation pipeline with gpt2 on "The bank was" with max_new_tokens=15 and note the continuation.
- Print each model's config and find the setting that shows whether it is a decoder (is_decoder, or the architecture name).
- Try asking BERT to continue text and GPT-2 to fill a blank, and write down what goes wrong in each case.
Done when
- You have BERT's top five words for the masked position.
- You have a GPT-2 continuation of at least 10 tokens.
- Your notes say which model you would use to embed documents and which to write replies, with a reason.
Docs: Transformers documentation (Hugging Face) · Pipelines (Hugging Face Transformers) · Start locally (PyTorch)