AI
What Is a Transformer Model? How LLMs Work Inside
A transformer model is the neural network behind ChatGPT and modern LLMs. Learn how self-attention works, encoders vs decoders, and why it took over AI.
A transformer model is a type of neural network designed to process sequences, such as words in a sentence, by looking at all the parts at once and working out how they relate to each other. It's the design behind ChatGPT, Claude, Gemini and nearly every large language model, and it now powers image, audio and video AI too. If you want to understand how modern AI works inside, the transformer is the place to start.
This guide explains the transformer architecture in plain language: what came before it, how a transformer processes text step by step, how self-attention works, the difference between encoders and decoders, why transformers took over AI, and their limits.
What Is a Transformer Model?
A transformer is a neural network built around a mechanism called attention. Attention lets the model, while processing one word, look at every other word in the input and decide which ones matter most for understanding it.
The design was introduced in 2017 by researchers at Google in a paper titled "Attention Is All You Need." The title made the point: earlier language models relied on recurrence, reading text one word at a time, and the authors showed that attention alone could do the job better and faster.
Today, "transformer" describes a whole family of models. All the big large language models are transformers, but so are many models for translation, search, speech recognition and image understanding.
Before Transformers: The Problem They Solved
Earlier language models were mostly recurrent neural networks (RNNs) and their improved version, LSTMs. They read text in order, one token at a time, carrying a running summary forward.
That caused two problems:
- Slow training: each step depended on the previous one, so the work couldn't be spread across many GPU cores at once.
- Fading memory: information from early in a long passage tended to get lost by the time the model reached the end.
Transformers fixed both. They process all tokens of a passage in parallel during training, which suits GPUs perfectly, and attention connects any word directly to any other word, however far apart they are.
How a Transformer Model Works, Step by Step
Here's what happens when a transformer reads text:
- Tokenise. The text is split into tokens: words or pieces of words. Our guide to LLM tokens explains how.
- Embed. Each token becomes a list of numbers, an embedding, that represents its meaning.
- Add position. Because the model sees all tokens at once, it needs to know their order, so position information is added to each embedding.
- Pass through transformer blocks. The embeddings go through a stack of identical layers. Each layer has two main parts:
- Attention, where each token gathers information from other tokens.
- A feed-forward network, which processes each token's updated information further.
Shortcut connections and normalisation steps keep training stable across dozens of layers.
- Predict. In a language model, the final layer turns the last token's representation into probabilities for the next token. The model picks one, adds it to the text and repeats.
Large models stack many of these layers and use enormous embeddings, which is where their billions of parameters come from.
Self-Attention Explained Simply
Self-attention is the heart of the transformer. Take the sentence: "The animal didn't cross the street because it was too tired." What does "it" refer to? You know it's the animal. A transformer works this out with attention.
For each token, the model creates three vectors:
- A query: what this token is looking for.
- A key: what this token offers to others.
- A value: the information this token passes on.
To update "it," the model compares its query with every token's key. Tokens with a strong match, here "animal," get a high attention score. The model then blends the values of all tokens, weighted by those scores. The new representation of "it" now carries information about "animal."
Every token does this at the same time, in every layer, so meaning flows across the whole passage.
Multi-head attention
Transformers run several attention operations in parallel, called heads. Each head can learn to track a different kind of relationship: one might link pronouns to nouns, another verbs to their subjects, another nearby words. Their results are combined. This is called multi-head attention.
Encoder, Decoder and Decoder-Only Models
The original transformer had two halves, designed for translation:
- The encoder reads the full input sentence and builds a rich representation of it, with every token able to attend to every other token.
- The decoder generates the output one token at a time, attending to what it has written so far and to the encoder's representation.
Later models used one half or both:
- Encoder-only models, such as Google's BERT, are good at understanding text: classification, search ranking and producing embeddings.
- Decoder-only models, such as the GPT family and most chat assistants, generate text by predicting the next token. Each token can only attend to tokens before it, which is what lets them write text one step at a time.
- Encoder-decoder models, such as T5 and many translation and speech-to-text systems, keep both halves for tasks that transform one sequence into another.
Most of today's large language models are decoder-only transformers.
Why the Transformer Architecture Took Over
- It trains fast on GPUs, because tokens are processed in parallel.
- It handles long-range context, connecting words far apart directly.
- It scales. Researchers found that bigger transformers trained on more data kept getting better, which led to today's large models.
- It's general. The same design works for text, images, audio, code and more, as long as the data can be cut into tokens.
Transformers Beyond Text
- Images: vision transformers split an image into small patches and treat them like tokens.
- Speech: speech recognition models such as OpenAI's Whisper use an encoder-decoder transformer to turn audio into text.
- Multimodal models combine text, image and audio tokens in one transformer, so a single model can read a chart and answer questions about it.
- Science: attention-based networks are used in protein structure prediction and other scientific fields.
Limits of Transformers
- Attention gets expensive with length. In standard attention, every token compares itself with every other token, so the cost grows with the square of the input length. That's one reason context windows have limits and long prompts cost more.
- Memory hungry: large models need a lot of fast memory to run.
- No built-in memory between conversations: a transformer only knows what's in its current input and what it learned in training.
- They can be confidently wrong, because they predict likely text rather than check facts.
Researchers are working on alternatives and improvements, such as more efficient attention methods, state space models and mixture-of-experts designs that use only part of a model for each token, but the transformer remains the dominant architecture.
Transformer Model vs LLM
An LLM is what you build; a transformer is how it's built. Every major LLM today is a transformer model trained on huge amounts of text, usually decoder-only. But not every transformer is an LLM: small transformers power translation, search, image recognition and speech tools that have nothing to do with chat.
FAQ
What is a transformer model in simple terms?
A transformer is a type of neural network that processes a whole sequence, like a sentence, at once and uses attention to work out which parts relate to which. It's the architecture behind ChatGPT and other large language models, and it's also used for images, audio and video.
What does "Attention Is All You Need" mean?
It's the title of the 2017 Google research paper that introduced the transformer. The authors showed that a model built mainly on attention, without the recurrence used in earlier language models, could perform better and train much faster.
What is self-attention?
Self-attention is the mechanism that lets each token in a sequence look at every other token and decide how much each one matters to it. The model uses queries, keys and values to score these relationships and blend information accordingly.
Is ChatGPT a transformer?
Yes. ChatGPT runs on large language models from OpenAI's GPT family. GPT stands for generative pre-trained transformer, and these models are decoder-only transformers that generate text by predicting the next token.
What is the difference between an encoder and a decoder?
An encoder reads the whole input and builds a representation of it, which is useful for understanding tasks like classification and search. A decoder generates output one token at a time, which is used for writing text. Many chat models are decoder-only; translation models often use both.