A transformer is a neural network architecture that uses attention to combine information from different parts of an input. In a language model, attention lets a token’s representation incorporate context from other tokens. The input is processed through a stack of blocks that repeatedly update those representations.
Vaswani and colleagues introduced the Transformer in the 2017 paper Attention Is All You Need for machine translation. Many later language models use variations of that design.
Our neural networks guide starts with a single node and shows how its output can become an input to another layer.
How self-attention works
Self-attention starts with a numerical representation for each token. Learned transformations produce three vectors for that representation. A query is compared with candidate keys to determine their relative weights. The layer then combines the corresponding values using those weights.
Consider “The technician put the sensor in a drawer because it was fragile.” When the model processes “it,” earlier tokens such as “sensor” and “drawer” can both supply context. The mechanism lets the representation for “it” draw on either one. Which token actually receives more weight depends on the model and layer. This sentence illustrates the operation without claiming to show a measured attention pattern.
What happens inside a transformer block
A transformer block applies several attention heads, each with its own learned transformations, to combine different signals. A feed-forward network then transforms each position’s representation. Residual connections and normalization help information pass through the stack. The model also uses position information to distinguish sequences with different word order.
The matrices used to create queries, keys, and values contain learned weights. Our LLM parameters guide explains how those weights relate to the model’s parameter count.
Three transformer architectures
Encoder-only models such as BERT build representations using context on both sides of a token. These representations can support tasks such as text classification.
Decoder-only models in the GPT family generate text by predicting the next token. A causal mask prevents a position from attending to future positions during training.
Encoder-decoder models, including the original Transformer, divide the work between an encoder for the input sequence and a decoder for the output sequence. In translation, the decoder can use both the translated text so far and the encoder’s representation of the source sentence.
How a transformer generates text
A decoder-only language model scores possible next tokens for a prompt. A decoding rule selects one, appends it, and repeats the process. Processing an input sequence can use parallel computation across positions. Generating a continuation autoregressively still depends on the tokens already produced. See the deep learning inference guide for how a trained network processes a request.
Transformers power many language models, but some generative systems use other architectures. Our generative AI vs. LLMs guide explains the distinction.
Why long context can be expensive
Standard full attention can compare many token positions with one another. Doubling the input length can roughly quadruple the number of pairwise comparisons in a full attention pass. Implementations and model architectures change the practical cost. During token-by-token generation, a runtime can cache earlier keys and values for reuse, which saves computation but takes memory.
The transformer is one component in a working AI application. The generative AI ecosystem guide explains the other layers that handle data, serving, and the interface you use.
Frequently asked questions
Is every transformer a large language model?
No. A transformer is a neural network architecture. Transformers can also process image patches or audio representations, and some language transformers are small.
What is the difference between self-attention and cross-attention?
Self-attention combines information within a sequence. In an encoder-decoder transformer, cross-attention lets the decoder use information from the encoder’s representation of a separate input sequence.
Why does a transformer need position information?
The model needs to distinguish different orders of the same tokens. Positions can be represented explicitly or built into the attention calculations.
Does attention show why a model gave an answer?
Attention shows how a particular operation weighted its inputs. Many heads, layers, and other operations contribute to the output, so one attention map cannot explain the entire answer.

