Encoder vs decoder models (BERT vs GPT)

In one sentence

Encoders (BERT) read text in both directions and output vectors for classification, embeddings and rerankers. Decoders (GPT) predict the next token and generate. Decoder-only won the frontier; small encoders still win cheap, high-volume understanding.

Analogy: An editor who reads the whole page before marking it (encoder) vs. a storyteller who can only look back at what was already said while choosing the next word (decoder).

Same Transformer blocks, different attention masks and outputs: understand, generate, or transform.

How it works

  • Encoders train by masking ~15% of tokens and predicting them, using context on both sides.
  • At scale every task becomes generation: a decoder classifies by answering and extracts by writing JSON.
  • Encoder-decoder models (T5, Whisper) fit input → output transforms: translation, summarization, speech-to-text.
  • Decoder-only won on one simple objective, clean scaling, in-context learning and one model for every task.
  • A ~100–400M encoder fine-tuned on a few thousand labels often matches a prompted LLM on narrow tasks, in milliseconds.

Check yourself

Answer to mark it learned; your answers aren't scored.

You must tag support tickets with topics that product managers redefine every week, and you have no labeled examples. A teammate proposes fine-tuning ModernBERT because it's fast and cheap. Your call?

Go deeper

Full explanation

The original 2017 Transformer architecture had two halves; modern models keep one or both.

  • Encoder-only (BERT, RoBERTa, ModernBERT): every token attends to every other, left and right (bidirectional). Trained by masking ~15% of tokens and predicting them. Output: one vector per token, pooled into one for the text. Built for understanding: classification, NER, Embeddings, cross-encoder Reranking (cross-encoders). It can't generate fluently.
  • Decoder-only (GPT, Llama, Qwen, Gemini): each token sees only earlier tokens (causal mask). Trained to predict the next token. Built for generation, and at scale every task becomes generation: classify by answering, extract by writing JSON.
  • Encoder-decoder (T5, BART, Whisper): an encoder reads the full input; a decoder generates while cross-attending to it. Natural for input → output transforms: translation, summarization, speech-to-text.

Why decoder-only won: one simple objective on raw text where every token is a training signal, clean scaling, in-context learning, and one model for every task via prompting. Serving one architecture also concentrated the optimizations (KV cache, batching, speculative decoding).

Where small encoders still win: high-volume classification, embeddings and rerankers. A ~100–400M encoder fine-tuned on a few thousand labels often matches a prompted LLM on a narrow task, in milliseconds and at a fraction of the cost.

Code The three families on one task each
pythonThe three families on one task each
from transformers import pipeline
from sentence_transformers import SentenceTransformer

text = "The refund took three weeks and nobody answered my emails."

# Encoder-only: one forward pass -> class scores. ~66M params, fine on CPU.
clf = pipeline("text-classification",
               model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
print(clf(text))  # [{'label': 'NEGATIVE', 'score': 0.99...}]

# Encoder-only: sentence embeddings for search and clustering (22M params, 384 dims)
emb = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
vecs = emb.encode(["reset my password", "I forgot my login"])

# Encoder-decoder: read the whole input, then generate the output
t5 = pipeline("translation_en_to_fr", model="google-t5/t5-small")
print(t5(text)[0]["translation_text"])

# Decoder-only: any task by prompting, but generated token by token
gen = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct")
msgs = [{"role": "user", "content": f"Sentiment (positive/negative), one word: {text}"}]
print(gen(msgs, max_new_tokens=3)[0]["generated_text"][-1]["content"])
Real example

A marketplace screens ~20M new listings a day against 12 policy labels. Prompting an LLM at ~400 tokens per listing means ~8B tokens a day, plus a second or more per item. Instead, the team fine-tunes ModernBERT-base (149M params) on ~30k labeled listings: one forward pass, a few ms per item on one GPU, near-zero marginal cost. Low-confidence items (~3%) go to an LLM, which also writes the explanation human reviewers read.

Trade-offs +4 · −4

+ You gain

  • Encoders: orders of magnitude smaller than frontier LLMs, millisecond latency, cheap to self-host and fine-tune.
  • Encoders: one pass returns a score per class, with no output parsing.
  • Decoders: one model does every task through prompting, zero-shot, with no labeled data.
  • Encoder-decoders: efficient for long input → short output transforms like translation and ASR.

− You pay

  • Encoders can't generate; each new task needs labeled data and a fine-tune.
  • Classic BERT models stop at 512 tokens; longer text is truncated (ModernBERT reaches 8,192).
  • Decoders pay per generated token in latency and cost, and their output needs validation.
  • Encoder-decoders get less investment now: few open checkpoints near the frontier.

Use it when

Encoder: high-volume, narrow, stable tasks with labels (classification, NER, PII detection, embeddings, rerankers). Decoder: open-ended generation, reasoning, few-label or fast-changing tasks. Encoder-decoder: translation, ASR, fixed transforms.

Avoid it when

Don't fine-tune an encoder for a task that changes weekly or has no labels: prompt an LLM. Don't call a frontier LLM millions of times a day for a label a 150M model learns.

Numbers to know 4
  • BERT-base 110M / BERT-large 340M params; pretraining masks 15% of tokens.
  • DistilBERT: 40% smaller, 60% faster, keeps ~97% of BERT's language understanding.
  • ModernBERT (Dec 2024): 8,192-token context, 149M (base) / 395M (large) params.
  • all-MiniLM-L6-v2: 22M params, 384-dim embeddings, still a common baseline.
Common pitfalls 4
  • Sending every classification to an LLM at scale, then fighting cost and latency.
  • Using raw BERT [CLS] vectors as embeddings: without contrastive training they're poor for similarity search.
  • Forgetting the 512-token limit of classic BERT: long text is silently truncated.
  • Assuming "embedding model" means encoder: many top embedding models today are fine-tuned decoder LLMs.
Interview questions 2

Answer out loud first, then reveal.

Why did decoder-only models win?Show answer

Mostly scaling economics. Next-token prediction turns every token of raw text into a training signal, while BERT's masked objective learns from about 15% of positions, so decoders get more out of the same data. Then one causal model does every task through prompting and in-context learning, so the industry trains, serves and optimizes a single stack: KV cache, batching, speculative decoding. Encoder-decoders still do well on input-to-output transforms, but the investment went to decoders.

Where would you put a BERT-style model in an LLM architecture?Show answer

Anywhere I need a cheap, fast, high-volume judgment over a stable label set: intent routing in front of the LLM, PII or toxicity detection in guardrails, embeddings for retrieval, and cross-encoder reranking. A fine-tuned ~150M encoder answers in milliseconds for a tiny fraction of an LLM call. The trade-off is labeled data and retraining when labels change, so I often bootstrap labels with an LLM and then distill into the encoder.