Learn AI /Transformers and Attention /Encoder and decoder
Encoder and decoder
- Deeper
- Transformers and Attention
- Technical readers
Also called: encoder, decoder, encoder-decoder model, seq2seq.
Back to the milestone you came from
In five seconds
What it is
In an encoder-decoder model, the encoder represents input, while the decoder uses those representations to generate output, often one token at a time.
In plain words
Think of it as a two-part system. One part reads and organises the input. The other part uses that organised view to produce the output step by step.
An everyday example
At a scholarship office, one staff member reviews each incoming application and marks the important details: missing documents, language used, deadlines and special instructions. A second staff member uses those notes to draft a standard response letter, adding one sentence at a time depending on what has already been written. If the notes say the passport copy is missing, the letter asks for it; if everything is complete, the letter confirms receipt. The first person organises the incoming material, and the second turns that organised view into outgoing text.
In the analogy, two people make explicit notes that a human can read. In a model, the intermediate representations are vectors, not neat written summaries. Also, the encoder and decoder are usually trained together from data rather than following office rules written in advance. Finally, some real systems have only a decoder, so the two-person office setup does not fit every language model.
Remember thisThe encoder represents the whole source in context. The decoder consults those representations and its own previous output to generate the target, usually one token at a time.
Understand it
Understand it
Why it matters
If you procure, evaluate or use AI tools, this term helps you tell different system designs apart. An encoder-decoder model is often a good fit when the task transforms one input into a different output, such as translation or summarisation. A decoder-only model may behave differently because it treats the prompt and response as one running sequence. This affects quality, latency, cost, context handling and failure modes. Knowing the distinction helps you ask better questions about what data the system uses, how outputs are produced, what kind of errors to expect and whether human review is needed.
How encoder and decoder work together
After viewing this, a reader should be able to explain that the encoder reads the source into internal representations and the decoder uses those representations plus previous output to generate the target step by step.
01
1. Source enters
The model receives a source sequence such as a sentence, audio tokens or other ordered input pieces.
Draw a horizontal row of 5 to 8 rounded token boxes on the left, labelled with example source tokens. Connect them with a single arrow into the encoder block.
02
2. Encoder builds context
The encoder compares parts of the source with one another and turns them into context-rich vectors.
Show a large middle block labelled Encoder. Inside it, stack 2 or 3 simplified layers. Above the block, place a caption: 'reads whole source'. Output a row of coloured vector bars or token boxes labelled 'encoded representations'.
03
3. Decoder starts output
The decoder receives a start token or first prompt token and the encoder’s representations.
Place a decoder block to the right. Feed the encoded representations into it with arrows from the encoder output row. Also show a small start token entering the decoder from below.
04
4. Decoder generates step by step
At each step, the decoder uses the source representations and the output produced so far to predict the next token.
Draw 3 output token boxes emerging one by one on the far right. Add a curved feedback arrow from the generated output tokens back into the decoder, labelled 'previous output so far'.
A left-to-right flow diagram with three main blocks: input tokens, encoder, and decoder leading to output tokens. Arrows show the whole input entering the encoder once, encoded representations passing to the decoder, and a feedback arrow from generated output back into the decoder for the next step.
Hovering or tapping each block highlights its incoming and outgoing arrows; stepping through reveals the feedback loop only after the first output token appears.
Describe this without the diagram
The diagram is a left-to-right process. First, a row of source tokens enters an encoder. The encoder reads the full source and produces encoded representations, shown as a transformed row. Those representations are sent into a decoder. The decoder also receives the output generated so far, starting with a start token. On each step, the decoder uses both the encoded source and the previous output to produce the next output token. The key point is that the encoder prepares the source, while the decoder generates the target step by step using that prepared source plus its own earlier output.
With reduced motion: A static flow chart shows source tokens feeding into an encoder, encoded representations feeding into a decoder, and a fixed curved arrow from partial output back to the decoder to indicate repeated next-token generation.
These words name related parts, not one single thing. An encoder reads the input and builds internal representations: numerical patterns that highlight what may matter in each part of the input. A decoder produces the output, usually step by step, while looking at what it has generated so far and, in many systems, also looking back at the encoder’s representations.
An encoder-decoder model contains both parts. It is common in tasks where the output depends on the whole input but is not just a continuation of it, such as translation, summarisation, speech recognition and some information extraction. The older label seq2seq means “sequence to sequence”: one sequence goes in, another sequence comes out. Not every seq2seq system uses transformers, but many modern ones do.
What goes in depends on the task: words, subword pieces, audio frames or other tokens. Inside the model, each token is turned into a vector, meaning a list of numbers. The encoder uses attention to compare tokens with one another and build context-rich vectors. The decoder then uses those vectors plus the already generated output tokens to predict the next token.
Decoder-only models differ structurally, but both can be autoregressive. A decoder-only model usually treats the input as part of one long prompt and then continues it. An encoder-decoder model separates reading the source from generating the target. That structure often helps when the source and target play different roles, but the boundary is not absolute because real systems vary.
How “Checking and rewriting application forms” maps onto the real thing
| In the story | In the AI system |
|---|---|
| Reviewer marking important details in each application | Encoder building context-rich representations of the input |
| Notes passed to the letter drafter | Encoder outputs made available to the decoder |
| Drafter writing the response sentence by sentence | Decoder generating output token by token |
| Using both the notes and the partial letter | Decoder attending to the encoded input and previous output |
A worked example
A desk officer uses an approved translation tool to prepare a first-pass English rendering of one sentence from a Spanish-language non-paper before informals.
Starting from: Source text: "La reunión empieza mañana"
- The source is split into tokens and converted into vectors carrying information about each token and its position.
- Using self-attention, the encoder processes all source positions and produces a contextualised hidden state for each one.
- The decoder begins with a start marker. Masked self-attention lets it use only the English tokens already generated, not future ones.
- Cross-attention lets the decoder consult the encoder's hidden states while predicting "The", then "meeting", "starts" and "tomorrow" in sequence.
- The decoder stops when it predicts an end marker.
Result: Draft output: "The meeting starts tomorrow." The desk officer checks the rendering against the source before using it in the file.
In a United Nations workplace
In an inter-agency secretariat, a statistician in the data unit is coding open-ended answers from a needs assessment before the monitoring report goes for clearance. An approved model proposes standard category labels for review.
The encoder builds contextualised representations of each response. The decoder attends to those representations and generates the proposed category label one token at a time, using both the encoded response and the label tokens already produced.
Before you try thisNeeds-assessment responses may contain personal or protection-sensitive details. They must not be entered into any tool not approved for that material. Remove unnecessary identifiers, apply the required access controls, and retain human review. De-identification alone does not make an unapproved tool acceptable.
Where you may hear it
- A vendor demo of an approved translation tool in a procurement meeting
- An architecture review by an information and communications technology governance board
- An inter-agency working group reviewing automated coding of needs-assessment responses
- A technical briefing before a summarisation pilot is cleared
Avoid confusion
Avoid confusion
Often confused with
Autoregressive model
A model that assigns each token a probability based on earlier tokens in the same sequence, and generates by predicting and adding one new token at a time.
Transformer
A neural network design that processes many parts of a sequence in parallel by weighting which parts matter most to each other.
Self-attention
A mechanism that lets each token reweight information from available tokens in the same sequence, including itself, before the next layer processes it.
Common misconceptions
Common misconception: Encoder-decoder simply means any transformer.
Correction: Not all transformers use both parts. Some systems have only an encoder, often for classification or retrieval, and some have only a decoder, often for next-token generation. Encoder-decoder names a specific arrangement of components, not the whole transformer family.
Common misconception: Seq2seq and encoder-decoder are always exactly the same thing.
Correction: They are closely related, but not identical in every context. Seq2seq is a broader task or model family label: one sequence mapped to another. Many seq2seq systems use an encoder and a decoder, but older or alternative designs may implement sequence-to-sequence behaviour differently.
Common misconception: The encoder creates a final summary and the decoder only reads that one summary vector.
Correction: That description fits some older architectures better than many modern transformer systems. In a transformer encoder-decoder, the decoder usually has access to a set of encoded representations across the whole source, not just one compressed vector. That helps preserve detail from different input positions.
Common misconception: Because the decoder writes the output, the encoder is just a pre-processing step and not very important.
Correction: The quality of the output depends heavily on what the encoder passes forward. If the encoded representations miss relationships in the source, the decoder has less useful information to work with. In many tasks, a strong encoder is central to good performance.
Quick check
Which statement best distinguishes an encoder-decoder model from a decoder-only autoregressive model?
The distinction is structural. An encoder-decoder model encodes the source separately, and its decoder usually cross-attends to those source states. It can still generate token by token. Its decoder also uses self-attention over earlier target tokens, and the architecture does not require separate source and target vocabularies. A decoder-only model represents the prompt and generated tokens in one running context.
Go deeper
Go deeper
The simple version
Think of it as a two-part system. One part reads and organises the input. The other part uses that organised view to produce the output step by step.
The technical version
In a transformer encoder-decoder architecture, source tokens are embedded and processed by encoder layers, typically using self-attention to produce contextualised hidden states for all source positions. The decoder generates the target autoregressively: at each step it uses masked self-attention over previously generated target tokens and cross-attention over the encoder’s hidden states to predict the next target token. “Encoder” and “decoder” can also refer to the individual halves by themselves. “Encoder-decoder model” names the full architecture containing both halves. “Seq2seq” is the broader idea of mapping an input sequence to an output sequence; many seq2seq systems are encoder-decoder models, but the terms are not perfectly interchangeable across all literature.
Reviewed external resources
EncyclopaediaIncremental encoder (opens in a new tab)Wikipedia
This link gives a reference-style overview of incremental encoder ideas and suits readers who want terminology and related concepts in a concise format.
Intermediatelink checked 2026-07-31
ArticleAttention Is All You Need (opens in a new tab)Vaswani et al., 2017
This paper introduces the transformer architecture and suits readers who want the original technical framing of encoder and decoder layers.
Deeperlink checked 2026-07-31
ArticleThe Illustrated Transformer (opens in a new tab)Jay Alammar
This article gives an intuitive visual walkthrough of transformer components and suits readers who learn best from diagrams and plain-language explanations.
Intermediatelink checked 2026-07-31
VideoVideo explanations: Encoder and decoder (opens in a new tab)YouTube search
This is a search results page for videos on the topic, so it suits readers who want to compare several teaching styles rather than rely on one source.
search results, not one pagelink checked 2026-07-31
DiscussionCommunity discussion: Encoder and decoder (opens in a new tab)Reddit search
This is a search results page for community discussion, so it suits readers who want to see common questions, confusions and informal explanations from many users.
search results, not one pagelink checked 2026-07-31
Where this came from
Drafted with a language model through an offline pipeline, checked against this site's schema, and every external link above fetched to see whether it answers. Treat it as a starting point and check anything that matters against the primary sources. How this site is built and what that means.
The one thing to remember
The encoder represents the whole source in context. The decoder consults those representations and its own previous output to generate the target, usually one token at a time.