Explainer

Encoder vs decoder models: which architecture should make your decisions?

There are two main kinds of AI language model: ones that read (encoders) and ones that write (decoders, such as the GPT family). For sorting, routing and scoring text, a reader is usually faster and cheaper, because it gives its answer in one step instead of writing it word by word. Laya is a reader.

7 min readLast updated

Swiss-hosted inference. Nothing you send is ever stored.Swiss data residency

In 30 seconds

  • Encoders read the whole text at once and build an understanding of it; decoders write new text one piece at a time.
  • For picking a label, an encoder answers in one pass, so it is usually faster and cheaper per decision.
  • Decoders (LLMs) win when you need reasoning, explanations or writing.
  • Laya is an encoder with a decision head: it cannot write, so it cannot invent text.
  • Encoders read a limited amount of text per question, so put the important part first.

Code and dense tables are folded away. Open any of them on demand.

What is the difference between encoder and decoder models?

Encoder models, such as BERT and ModernBERT, read the whole input in both directions at once and turn it into a representation, which suits classification, retrieval and scoring. Decoder models, such as the GPT family, predict the next token from left to right and generate text. For labelling, an encoder answers in one pass; a decoder must generate its answer.

All modern language models descend from the transformer of Vaswani et al. (2017), which had two halves: an encoder that builds a representation of the input and a decoder that generates output one token at a time. Later work split them apart.

  • Encoder-only models (BERT, RoBERTa, DeBERTa, ModernBERT, mmBERT) keep only the encoder. Every token attends to every other token in both directions. They are pretrained with masked language modelling: hide some tokens, predict them from the context on both sides.
  • Decoder-only models (the GPT family and most current LLMs) keep only the decoder. Each token attends only to tokens before it (causal attention). They are pretrained to predict the next token, which makes them natural text generators.
  • Encoder-decoder models (T5, BART) keep both. The encoder reads the input bidirectionally; the decoder generates an output sequence while attending to it. They are common for translation and summarisation.
Show technical details· 5 rows × 4 columns
Encoder-onlyDecoder-onlyEncoder-decoder
AttentionBidirectionalCausal (left to right)Bidirectional in, causal out
PretrainingMasked token predictionNext-token predictionSpan corruption / denoising
Natural outputA vector per tokenA sequence of tokensA sequence of tokens
Typical size for production use100M to 1B parameters1B to hundreds of billions200M to 11B+
Typical useClassification, retrieval, NER, rerankingChat, reasoning, generation, agentsTranslation, summarisation

Why bidirectional attention suits classification

A classifier needs to understand the whole input before committing to an answer. In an encoder, the representation of every token already includes information from the entire sequence, left and right. A classification head can read one or more of those vectors and produce a distribution over labels in a single forward pass.

A decoder can classify too, but it has to do it by generating. The usual pattern is to prompt for a label and read the first generated token (or several). Because attention is causal, the model's representation of the input can only use context that came before each position, and the label is produced after the model has read the input. Decoders compensate with scale and with instruction tuning, which is why a large LLM can be a good zero-shot classifier. But the mechanics are indirect: the answer is a string that has to be parsed, and the "probability" is a token probability that depends on how the label is spelled.

Cost and latency: one pass versus a loop

The practical difference is in how work scales.

Encoder: one forward pass over the input. Cost grows with input length and model size. There is no output length. Laya's model card reports 39.5 ms for one question on a T4 GPU with a 421M-parameter encoder, and 158.6 ms for ten questions batched.

Decoder: one forward pass over the input (the prefill), then one additional forward pass per generated token. Cost grows with input length, model size and output length. A label of three tokens costs at least three decode steps; a label with a short justification costs dozens. Hosted LLM latency also includes queueing and network overhead that a small self-hosted encoder avoids.

Show technical details· text sample
text
encoder latency  ≈ prefill(input)
decoder latency  ≈ prefill(input) + n_output_tokens × decode_step

For a single ticket this may not matter. For an agent that makes several decisions per turn, or a pipeline classifying millions of emails, it dominates. See latency budgets for agents and the cost of using an LLM as a classifier.

Where decoders win

An honest comparison has to say where encoders lose.

  • World knowledge and reasoning. A multi-billion-parameter LLM knows far more than a 400M-parameter encoder and can reason through a multi-step policy. If the decision genuinely requires reasoning (reading a contract and applying a clause), a small encoder will not substitute for it.
  • Very large label spaces. Laya scores options at marker tokens that share a fixed budget, and its accuracy falls off with dozens of options: 0.425 on 77-label Banking77 against 0.870 published for Jev. An LLM that can read a long label list, or a fine-tuned classifier with a fixed head, does better here.
  • Zero-shot on unfamiliar schemas. The model card reports that Laya's base checkpoints are near chance on the multi-field typed-decisions benchmark zero-shot (0.362 vs 0.461 majority class). Instruction-tuned LLMs are generally stronger zero-shot on complex, unfamiliar tasks.
  • Explanations. If you need a written rationale, you need a generator.

The two are complementary. A common production pattern is a fast encoder for the high-volume, well-defined decisions, with escalation to an LLM or a human when the encoder's calibrated confidence is low. See act and escalate routing.

How Laya uses an encoder

Laya is an encoder-only decision model. The English checkpoint uses ModernBERT-large (395M parameters, fully fine-tuned) and the multilingual one uses mmBERT-base. On top sits a decision head trained from scratch: two transformer layers, an option-marker scorer and an act/escalate head, for 421M parameters in total on the English checkpoint.

The input is built so that the question, the options and the state are all in one sequence:

Show technical details· text sample
text
[CLS] <type> question: <instructions> [SEP] [MASK] opt0 [MASK] opt1 ... [SEP] <state> [SEP]

Because attention is bidirectional, each option's [MASK] marker can attend to the whole state and to the other options, and the state tokens can attend to the options. The scorer reads one logit per marker. This is what lets the label set change per request without retraining, something a classic fine-tuned BERT with a fixed classification head cannot do. The details are in option-marker scoring.

Every question in a request is batched into the same forward pass, and nothing is generated. That is what "non-autoregressive" means here; see non-autoregressive models.

Choosing between them

If your decision is...Prefer
High volume, fixed or slowly changing options, under ~20 labelsEncoder (Laya, or a fine-tuned BERT)
Latency-critical, inside an agent loopEncoder
Needs calibrated probabilities to threshold onEncoder trained with proper scoring rules, then calibrated
Needs multi-step reasoning or a written rationaleDecoder LLM
Dozens to hundreds of labels in one questionFine-tuned classifier, a hierarchical encoder setup, or an LLM
Rare, high-stakes, low volumeDecoder LLM or a human, possibly behind an encoder pre-filter

Calling an encoder decision model through Laya Studio

Laya Studio runs the Laya encoders as a hosted API. A request looks like any other /v1/systemone call; the difference from an LLM call is that the response has no generated text and output_tokens is always zero.

Show technical details· bash sample
bash
curl -s https://api.laya.studio/v1/systemone \
  -H "Authorization: Bearer $LAYA_STUDIO_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "state": {"request": "Write a SQL query that returns the top 10 customers by revenue last quarter."},
    "questions": {
      "domain": {
        "type": "choice",
        "instructions": "What domain does request belong to?",
        "criteria": {
          "code": "software engineering, programming, debugging",
          "math_or_logic": "mathematics, logic puzzles, proofs",
          "writing": "creative writing, essays, emails",
          "factual_lookup": "facts, definitions, trivia",
          "data_analysis": "statistics, SQL, data manipulation, metrics",
          "chitchat": "casual conversation, greetings"
        }
      },
      "needs_tools": {
        "type": "noul",
        "instructions": "Does answering request require external tools, search or private data?"
      }
    }
  }'

The answers come back as typed fields (choice, probabilities, confidence, noul) with a routing block naming the checkpoint that answered. Two questions read the state twice, billed per input token. Create a key or read the API docs.

Frequently asked questions

Is Laya an LLM?
No. Laya is an encoder-only transformer with a decision head, about 421M parameters for the English checkpoint. It reads text but never generates it, so it cannot write answers, explanations or code.
Can a decoder-only LLM be used as a classifier?
Yes, by prompting it to output a label and parsing the result, or by reading token probabilities for each label. It is often strong zero-shot, but costs more per decision, adds latency per output token, and its label probabilities depend on tokenisation and phrasing.
Why not just fine-tune BERT with a classification head?
That works well when the label set is fixed and you have labelled data. A fixed head cannot accept new labels at request time, and one model per task multiplies deployments. Laya scores options supplied in the request, so one model serves many schemas.
Are encoders always faster than decoders?
For the same input and a comparable parameter count, an encoder that produces a label in one pass does less work than a decoder that must also generate tokens. In practice, encoders used for classification are also much smaller than LLMs, which widens the gap.
Do encoder models have context limits?
Yes. Laya's English checkpoint uses 512 tokens per question, of which 192 are reserved for the instructions and options. The multilingual checkpoint uses 1,024. Long documents are truncated, so put the decisive text first or pre-extract it.
Is GPT an encoder or a decoder model?
GPT models are decoder-only transformers: each token attends only to earlier tokens, and they are pretrained to predict the next token, which makes them text generators. BERT-style models, including ModernBERT and mmBERT, are encoder-only.
Which is better for text classification, an encoder or an LLM?
For a fixed, well-defined label set at volume, an encoder is usually faster and cheaper per decision and returns probabilities directly. An LLM is often stronger zero-shot on unusual tasks and can explain itself. Many teams use an encoder for the confident majority and escalate the rest to an LLM.

Sources

Last updated . Laya Studio is an independent hosted service for the open-source Laya model (Apache-2.0, © Convai Innovations) and is not affiliated with Convai Innovations or TypeSafe.