Tokens and Transformers
The machinery that turns text into GPT-style intelligence.

“A lot of the issues that may look like just issues with the neural network architecture or the large language model itself are actually issues with the tokenization…”Talk / tutorial presentation, 2019 · 4:23
The lesson
GPT-style models work on sequences of tokens, not raw text. Tokenization is the step that turns text into discrete units the model can process, and Karpathy treats it as a central part of the system rather than a minor preprocessing detail. He emphasizes that many behaviors people blame on the model are actually caused by how text gets chopped up into tokens, especially for spelling, string manipulation, whitespace, and non-English text12.
A tokenizer is therefore a tradeoff between simplicity and efficiency. A simple character-level scheme is easy to understand, and Karpathy uses it for teaching, but modern systems need more compression, which is why byte pair encoding becomes important in practice. This matters not only during training but also at inference time, because token count affects context usage and cost, so the way information is encoded can materially change how well and cheaply a model works3.
Once text has become tokens, GPT models learn to predict the next token in a sequence. Karpathy presents this as a probabilistic sequence model: training repeatedly adjusts parameters so the correct next token gets higher probability, and generation then samples or selects one token at a time. The key architectural engine doing this work is the Transformer, which Karpathy describes as the neural network carrying the main computation under the hood4.
For GPT specifically, the model is a decoder-only Transformer with causal self-attention. Decoder-only means it omits the encoder and cross-attention parts used in the original full Transformer, keeping self-attention and feed-forward computation in repeated blocks. Its autoregressive behavior comes from the triangular mask: each position can use the past but not the future, which is why the model can continue a sequence token by token without peeking ahead56.
Inside each block, attention and the MLP play different roles. Karpathy frames attention as the part where tokens communicate and combine information across the sequence, while the MLP transforms each position's representation more independently; stacked together, these blocks repeatedly refine the residual stream. A practical way to think about GPT, then, is as a fixed mathematical function with many learned weights that maps a token history to a distribution over the next token, not as a persistent self or hidden mind behind the text78.
5 principles from Andrej Karpathy
Treat tokenization as part of the model
Do not treat tokenization as a boring wrapper around the real system. When a model struggles with spelling, formatting, or certain languages, inspect the token boundaries first because many failures originate there rather than in the Transformer itself.
“A lot of the issues that may look like just issues with the neural network architecture or the large language model itself are actually issues with the tokenization…”
Optimize for token efficiency, not just readability
The same information can consume different numbers of tokens depending on how you format it. If you are designing prompts or structured inputs, compare encodings and prefer ones that preserve meaning while using fewer tokens.
“More efficient in tokens. Um, so for example, these are the same in JSON and in YAML. The JSON is 116 and the YAML is 99, so quite a bit of an improvement. And so in the token economy, where we are paying per token in many ways, and you are paying in the context length, and you're paying in, um, dollar amount for the cost of processing all this kind of structured data, when you have to, um, so prefer to use the YAML over JSON. And in general, kind of like, the tokenization density is something that you have to, um, sort of care about and worry about at all times and try to find efficient encoding schemes, and spend a lot of time in the tokenizer, and measure the different token efficiencies of different formats and settings and so on.”
Think of GPT as next-token prediction over a sequence
A GPT-style model does not ingest an idea all at once and emit a full answer from nowhere. It repeatedly predicts one token after another, so both training and prompting should be understood as shaping the token sequence the model sees.
Use causal attention to explain what GPT can and cannot know
In a decoder-only Transformer, each position can attend only to earlier positions because of the triangular mask. This explains why GPT can extend text coherently from prior context but cannot directly use future tokens when computing the current one.
“What we implemented here is a decoder-only Transformer. So there's no component here — this part is called the encoder — and there's no cross-attention block here. Our block only has a self-attention and the feed-forward, so it is missing this third in-between piece here. This piece does cross-attention, so we don't have it, and we don't have the encoder; we just have the decoder. And the reason we have a decoder-only, uh, is because we are just uh generating text and it's unconditioned on anything. We're just—we're just blabbering on according to a given data set. What makes it a decoder is that we are using the triangular mask in our uh Transformer, so it has this autoregressive property where we can just uh go and sample from it.”
Separate communication from computation inside the block
Attention lets tokens exchange information across the sequence, while the MLP processes the representation at each position after that information has been gathered. This division is a useful mental model for understanding how stacked Transformer blocks build richer representations.
“So the attention is the reduce, and the MLP is the map. And what you end up with is that the Transformer just ends up just being a repeated application of map produce if you want to think about it that way. So this is where they communicate, and this is where they think individually about the information that they've gathered. And every one of these blocks iteratively refines the representation at the residual stream.”
In his own words
The quotes behind the lesson, word for word from the transcripts. Numbers match the lesson above.
“A lot of the issues that may look like just issues with the neural network architecture or the large language model itself are actually issues with the tokenization…”
“So tokenization is at the heart of a lot of weirdness in large language models, and I would advise that you do not brush it off. A lot of the issues that may look like just issues with the neural network architecture or the large language model itself are actually issues with the tokenization and fundamentally trace back to it. So if you've noticed any issues with large language models — can't, you know, not able to do spelling tasks very easily — that's usually due to tokenization. Simple string processing can be difficult for the large language model to perform natively. Non-English languages can…”
“More efficient in tokens. Um, so for example, these are the same in JSON and in YAML. The JSON is 116 and the YAML is 99, so quite a bit of an improvement. And so in the token economy, where we are paying per token in many ways, and you are paying in the context length, and you're paying in, um, dollar amount for the cost of processing all this kind of structured data, when you have to, um, so prefer to use the YAML over JSON. And in general, kind of like, the tokenization density is something that you have to, um, sort of care about and worry about at all times and try to find efficient encoding schemes, and spend a lot of time in the tokenizer, and measure the different token efficiencies of different formats and settings and so on.”
“Transformer is the neural net that actually does all the heavy lifting under the hood…”
“What we implemented here is a decoder-only Transformer. So there's no component here — this part is called the encoder — and there's no cross-attention block here. Our block only has a self-attention and the feed-forward, so it is missing this third in-between piece here. This piece does cross-attention, so we don't have it, and we don't have the encoder; we just have the decoder. And the reason we have a decoder-only, uh, is because we are just uh generating text and it's unconditioned on anything. We're just—we're just blabbering on according to a given data set. What makes it a decoder is that we are using the triangular mask in our uh Transformer, so it has this autoregressive property where we can just uh go and sample from it.”
“The future cannot communicate with the past…”
“So the attention is the reduce, and the MLP is the map. And what you end up with is that the Transformer just ends up just being a repeated application of map produce if you want to think about it that way. So this is where they communicate, and this is where they think individually about the information that they've gathered. And every one of these blocks iteratively refines the representation at the residual stream.”
“…everything here is just the other weights and biases and everything else of this Transformer…”
Reflect
- Think of a prompt or workflow you use with LLMs. Where might tokenization, rather than reasoning ability, be causing failures such as formatting mistakes, spelling issues, or poor handling of structured data?
- Take one piece of structured input you often send to a model. How could you rewrite or reformat it to use fewer tokens while keeping the same meaning?
- When a GPT model gives an answer, how does viewing it as next-token prediction over a causal context change your interpretation of its strengths and limits?
Check your understanding
Go deeper
Andrej Karpathy on Tokenization: Building GPT's Tokenizer2019 · 134 min · full transcript
Andrej Karpathy on GPT From Scratch: How ChatGPT Works2022 · 116 min · full transcript
Andrej Karpathy on GPT-2 Reproduction: Rebuilding 124M From Scratch2022 · 241 min · full transcript
Andrej Karpathy on Large Language Models: How ChatGPT Works2022 · 211 min · full transcript
- What did Andrej Karpathy say about why tokenization causes strange LLM behavior?
- What did Andrej Karpathy say about the risks of tokenization?
- What tokenizers did Andrej Karpathy recommend using?
- What did Andrej Karpathy say about attention and MLPs in Transformers?
- Why did Andrej Karpathy say the model he built is decoder-only?
- How does Andrej Karpathy compare GPT-3 scale to the model he built in the lecture?