Transformer Architecture
Encoder/decoder stacks, residual streams, feed-forward layers, and why transformers dominate LLMs.
In plain words
Words look at each other to work out what matters, think about it on their own, and repeat that many times before guessing the next word.
Think of it like…
A newsroom with a repeating editing round: in each round every reporter reads the others' drafts (attention), then works alone on their own piece (FFN), and every round's changes are written on top of the previous draft rather than replacing it (residual stream).
Where you’ve seen it
A team compares serving a 7B and a 70B decoder-only model. Both have the same block design; the 70B has more layers and wider matrices, so each token costs roughly ten times the compute and the weights need about 140 GB in 16-bit precision instead of 14 GB. That single architectural fact drives their GPU choice, batch size and price per request.
Words you’ll hear
- Residual stream
- The running vector for each token; every sublayer adds an update to it.
- Feed-forward network (FFN)
- A per-token two-layer MLP in each block; holds most of the parameters.
- Decoder-only
- Causal-masked stack that predicts the next token; used by GPT-style chat models.
- Layer norm
- Normalises activations so deep stacks train stably.
Visual diagram
Theory & deep dive
Inside a block
Input tokens become embedding vectors plus some encoding of position (rotary embeddings are common today). In each block, self-attention computes, for every token, a weighted blend of the other tokens' value vectors, which is how information moves between positions. The FFN then applies the same small network to every token independently, typically expanding the vector to about four times its width and back, with a non-linearity between; this is where much of the model's stored knowledge and most of its parameters sit.
Each sublayer's output is added to its input (the residual connection) and normalised. That addition gives gradients a direct path through dozens of layers, which is what makes deep transformers trainable, and it means later layers refine an existing representation. Modern variants use gated FFNs (SwiGLU), RMSNorm and grouped-query attention to cut cost, but the skeleton is the same.
In a typical block with hidden size d, attention projections hold about 4d² parameters and a standard FFN about 8d². Roughly two thirds of each block's weights are in the FFN, which is why sparse mixture-of-experts models replace the FFN with many expert FFNs.
Encoder, decoder and what chat models use
The original 2017 transformer had an encoder (bidirectional, reads the whole input) and a decoder (generates the output while attending to the encoder). Encoder-only models such as BERT suit classification and embeddings; encoder-decoder models such as T5 suit translation and summarisation; decoder-only models now dominate general-purpose LLMs because one next-token objective scales cleanly and handles any task as text continuation.
At inference a decoder-only model works in two phases: a prefill over the whole prompt, which is compute-heavy and parallel, then decode, which generates one token at a time and is limited by memory bandwidth because each step re-reads all the weights. That split is why time-to-first-token and tokens-per-second are different metrics and are tuned differently.
Semantic search embeddings: encoder. Summarising documents with a fixed pipeline: encoder-decoder or decoder. Open-ended chat, code and tool use: decoder-only.
How it works, step by step
Step 1 of 41. Embed tokens and positions
Look up each token id's vector and add or apply positional information so order matters.
- d
- 4,096 – 12,288 typical
When to use it & trade-offs
What it is
A transformer is a stack of identical blocks. Each block lets every token attend to the other tokens (self-attention), then passes each token through a feed-forward network (FFN), with residual connections and normalisation around both. Token vectors flow up the stack, and the final layer turns the last vector into probabilities over the vocabulary. Most chat LLMs are decoder-only: the same stack, with a causal mask so each token sees only earlier tokens.
Why it matters: Everything in LLM engineering maps back to this structure: context length is the attention window, parameter count lives mostly in the FFN and attention weights, serving cost depends on layer count and width, and fine-tuning methods such as LoRA target specific matrices inside blocks. Knowing the pieces lets you reason about speed, memory and capability instead of treating the model as a black box.
When to reach for it
- Reasoning about cost from layers, width and parameter count
- Choosing encoder vs decoder models for a task
- Understanding where LoRA adapters and quantisation apply
When to avoid or be careful
- Treating all transformers as the same for latency; prefill and decode behave differently
- Assuming bigger always means better for your task and budget
- Ignoring that parameter count, not just context length, sets memory
Transformer families
Strengths: One objective, scales well, simple to serve.
Watch out: Attends only left context; large KV cache at long lengths.
Strengths: Bidirectional context; small and fast.
Watch out: Doesn't generate text.
Strengths: Separates reading from writing.
Watch out: Less common for general chat now.
Interview signals & numbers to know
When this topic comes up
- “Explain the transformer architecture”
- “Why is a decoder-only model used for chat?”
- “What's the residual stream?”
- Prefill vs decode latency
- Where do parameters live?
Benchmarks & rules of thumb
- 2017
- "Attention Is All You Need"
- ≈ 2 bytes
- per parameter in 16-bit weights
- ≈ 4×
- FFN hidden width relative to model width
Common mistakes & follow-ups
Common interview mistakes
- Saying attention is where the knowledge is stored, when most parameters are in the FFN layers.
- Forgetting the causal mask, and describing a decoder as seeing the whole sequence.
- Quoting one tokens-per-second number without separating prefill from decode.
Likely interviewer follow-ups
- What does the residual connection buy you?
- Why do chat models use decoder-only transformers?
- What's the difference between prefill and decode?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Transformer Architecture with generative AI interview Q&A and hands-on practice.