What matters in AI.

Subscribe

Learn / AI basics

Definition · AI basics

Transformer

A transformer is a neural network design that reads a sequence of tokens. Attention lets each token get information from all the other tokens. Eight researchers made the design public in 2017, in the paper "Attention Is All You Need". Most large language models, such as GPT and Llama, use it.

Last reviewed

Key points

  • A transformer uses attention, not recurrence. Each token looks at all other tokens in one step, thus training can use many GPUs in parallel.
  • The 2017 paper was about translation. Its abstract gives 28.4 BLEU on English to German and 41.8 on English to French (WMT 2014).
  • There are three families. Encoder only models (BERT) read text. Decoder only models (GPT, Llama) write text. Encoder and decoder models change one text into a different text.
  • Attention work increases with the square of the input length. Thus each model has a context window limit, and long inputs cost more.
  • A large language model puts instructions and data in one sequence of tokens. The UK NCSC says that LLMs do not enforce a security boundary between the two.

How a transformer works

A transformer changes text into tokens, then each token into a vector (an embedding). A stack of layers then changes the vectors again and again. Vaswani and colleagues used 6 layers in the encoder and 6 in the decoder.

Each layer has two parts:

  1. Self-attention. Each token makes a query. Each other token gives a key and a value. The query and the key set how much of each value goes into the result.
  2. Feed-forward network. A small network changes each token vector independently.

The paper uses 8 attention heads in each layer. Each head can follow a different relation between tokens. Attention does not know the word sequence, thus the model adds position data to each token.

A decoder only transformer: tokens go in, pass through a stack of attention layers, and the model gives the next token.
A decoder only transformer: tokens go in, pass through a stack of attention layers, and the model gives the next token.

Why the transformer is important for security

  1. Instructions and data share one input. The system prompt, the user text and a web page become one sequence of tokens. The UK NCSC (8 December 2025) says LLMs “do not enforce a security boundary between instructions and data inside a prompt”. In an LLM, “there is only ever ‘next token’”. This lets prompt injection work.
  2. Long inputs cost more. The 2017 paper gives self-attention a cost for each layer of O(n²·d), for n tokens. Thus each context window has a limit. Very long inputs can also help a denial of AI service.
  3. A model does not read a long input equally. Liu and colleagues (2023) found that results are good when the important text is at the start or the end. Results “significantly degrade” when it is in the middle.

Encoder, decoder, or the two together

The 2017 design had an encoder and a decoder, for translation. Many subsequent models keep only one part.

Family Use Example
Encoder only Reads and classifies text BERT (2018)
Decoder only Writes text, one token at a time GPT (OpenAI, 2018), Llama 3 (Meta, 2024)
Encoder and decoder Changes one text into a different text the 2017 translation model

A decoder applies a mask. The mask makes sure that the prediction for a position uses only the earlier positions. Thus a chat model writes its answer one token at a time.

In practice: from translation to all AI

The transformer replaced recurrent networks, which read one word after the other. Attention looks at all tokens in one step. Thus training can use many GPUs in parallel.

At this time, the design has many uses, not only translation:

  • Text. The largest Llama 3 model from Meta (2024) is a dense transformer with 405B parameters.
  • Images. The Vision Transformer (2020) applies it to small patches of an image.

Questions and answers

What does attention mean in a transformer?

Attention is the step where each token gives a weight to each other token. Then it adds their information by those weights. The 2017 paper uses queries, keys and values for this step. It uses 8 attention heads in parallel in each layer.

Is ChatGPT a transformer?

Yes. GPT means Generative Pre-trained Transformer. Hugging Face lists GPT (June 2018) as the first pretrained Transformer model. The GPT family uses the decoder only shape, which is good for text generation.

Why do transformers have a context limit?

Self-attention compares each token with each other token. The 2017 paper gives its cost for each layer as O(n²·d), for n tokens. Two times the input gives about four times the attention work. The largest Llama 3 model from Meta accepts a maximum of 128K tokens.

Sources

  1. Attention Is All You NeedarXiv (Vaswani et al.), 12 Jun 2017
  2. Attention Is All You Need (HTML, v7)arXiv (Vaswani et al.), 12 Jun 2017
  3. How do Transformers work? (LLM Course, chapter 1)Hugging Face
  4. BERT: Pre-training of Deep Bidirectional Transformers for Language UnderstandingarXiv (Devlin et al.), 11 Oct 2018
  5. The Llama 3 Herd of ModelsarXiv (Meta), 31 Jul 2024
  6. An Image is Worth 16x16 Words: Transformers for Image Recognition at ScalearXiv (Dosovitskiy et al.), 22 Oct 2020
  7. Lost in the Middle: How Language Models Use Long ContextsarXiv (Liu et al.), 6 Jul 2023
  8. Prompt injection is not SQL injection (it may be worse)UK National Cyber Security Centre, 8 Dec 2025

In the news