Forward Deployed EngineeringAI EngineeringTransformer Architectures

Decoder-only, encoder-only, encoder-decoder: what is each good at, and why does generation use decoder-only?

Question 66 of 73 · Updated

On this page

Interview-ready answer#

The key difference is how each architecture reads its input and produces its output.

  • Encoder-only models use bidirectional self-attention to build contextual representations of an input. They are a natural fit for classification, embeddings, retrieval and reranking.
  • Decoder-only models use causal self-attention and predict the next token from the preceding tokens. They are a natural fit for open-ended generation, chat and code completion.
  • Encoder-decoder models encode the source with bidirectional attention and generate the target autoregressively using cross-attention to the encoded source. They are especially useful for input-to-output transformations such as translation and summarization.

Decoder-only models dominate many general-purpose generative LLM applications because a single causal stack can represent the prompt, examples, conversation and generated continuation as one sequence. This aligns naturally with next-token pretraining and incremental generation. However, encoder-decoder models can generate text too; decoder-only is not the only architecture capable of generation.

1. Compare the architectures#

FeatureEncoder-onlyDecoder-onlyEncoder-decoder
Input attentionBidirectionalCausalBidirectional encoder
Output attentionNo separate autoregressive decoderCausalCausal decoder
Cross-attentionNoNo in standard text-only architectureDecoder attends to encoder states
Typical pretrainingMasked language modeling or representation-learning objectivesNext-token predictionConditional generation, often denoising
Common tasksClassification, embeddings, extractive QA, rerankingChat, code generation, completionTranslation, summarization, text-to-text tasks
Representative familiesBERT, RoBERTaGPT, LlamaT5, BART

These are typical architectural conventions and objectives, not universal restrictions on every variant.

Loading diagram…

2. Why the attention mask matters#

Consider three input tokens: A, B and C. In a standard bidirectional encoder, B can attend to A, B and C. In a causal decoder, B can attend only to A and B.

Query tokenEncoder can attend toCausal decoder can attend to
AA, B, CA
BA, B, CA, B
CA, B, CA, B, C

Padding masks and other task-specific restrictions are omitted here.

Bidirectional attention is useful for understanding a complete input because context on either side can resolve ambiguity. Causal attention prevents a prediction from seeing future target tokens. During training, next-token predictions for different positions can still be computed in parallel using the causal mask and shifted targets. At inference, newly generated tokens must normally be produced in sequence.

In an encoder-decoder model, the decoder has causal self-attention over its target prefix and cross-attention to the encoded source. Cross-attention uses decoder queries and encoder-derived keys and values. The whole source can therefore inform each target prediction without exposing future target tokens.

3. Why decoder-only works well for general-purpose generation#

A causal language model learns a factorization of the sequence probability into next-token probabilities conditioned on the preceding sequence. At inference, a prompt establishes the prefix, and each generated token extends it. Instruction following, few-shot examples and conversation history can all be expressed within this same interface.

There are two practical advantages:

  • Unified training and conditioning: one stack handles prompt tokens and response tokens. Large-scale next-token prediction can use ordinary text without requiring a separately defined source-target pair for every example.
  • Incremental serving: with standard causal attention, earlier token representations do not change when new tokens are appended. A KV cache can retain their attention keys and values, avoiding repeated projection and processing of the entire prefix at every decoding step.

The cache still grows with retained sequence length, and each new token must attend to relevant cached states. Encoder-decoder models also support incremental KV caching for decoder self-attention and may cache cross-attention keys and values for the fixed source. KV caching is therefore not exclusive to decoder-only models.

4. How I would choose as an FDE#

For semantic retrieval over documents, start with a suitable embedding encoder; use a bidirectional cross-encoder reranker when higher relevance accuracy justifies its cost. For an interactive assistant that must answer questions and generate code, choose an instruction-tuned generative model, commonly decoder-only. For a bounded source-to-target task such as translation, evaluate a specialized encoder-decoder model alongside decoder-only alternatives.

Architecture alone does not determine quality, latency or cost. Compare models trained for the actual task, using representative inputs and operational constraints. An encoder-only model can be adapted to fill masked tokens, but that is different from standard left-to-right generation. An encoder-decoder model can be a strong generator, so do not claim decoder-only is inherently superior for every text-generation task.

Related questions

Explore other topics:AI AgentsAI System DesignLarge Language Models