How large language models work
How largelanguage modelswork
A large language model does one thing: it predicts the next piece of text. Every chatbot answer is built from that one move, repeated.
Slide 1 of 15: How large language models work.
How largelanguage modelswork
A large language model does one thing: it predicts the next piece of text. Every chatbot answer is built from that one move, repeated.
Predict the next word. Then do it again.
Strictly, the next token. We will get to tokens in two slides.
Next-token prediction
- 1The model reads the text so far.
- 2It gives every token in its vocabulary a probability. These are the top five.
- 3One token is picked, usually a likely one, with a little randomness.
- 4The token is added to the text, and the whole thing runs again.
Probabilities are illustrative.
Tokens, not words
- Token1
- ization2
- ␣is3
- ␣fun4
Models read text as tokens. Common words are one token; rarer words are split into pieces.
For English, one token is roughly four characters, or about three quarters of a word.
This is why a model can stumble on spelling or on counting the letters in a word: it never saw the letters, only the pieces.
From text to vectors
- 1Start with text.
- 2Split it into tokens.
- 3Each token has an ID in the model’s vocabulary.
- 4Each ID is looked up as a vector: a list of numbers called an embedding. GPT-2 small uses 768 numbers per token; 8 are drawn here.
Splits and IDs are illustrative. ␣ stands for the leading space; each tokenizer marks it its own way.
“You shall know a word by the company it keeps.”
Meaning as position
During training, tokens used in similar ways end up with similar vectors. Their embeddings sit near each other in space.
Directions can carry meaning too. In the word2vec model from 2013, king − man + woman lands close to queen.
An embedding is where a token starts. Attention is how it learns from its neighbours.
Attention
- 1Each token makes a query (what am I looking for?), a key (what do I contain?) and a value (what do I pass on?).
- 2“it” compares its query with the key of every token up to and including itself. The scores become weights that add up to 1.
- 3Most of the weight lands on “cat”. The new vector for “it” is mostly cat’s value.
- 4Later words are hidden. In a model like GPT, a token can only look back.
Weights are illustrative.
The transformer stack
- 1Tokens become embeddings.
- 2A block of attention, then a small feed-forward network, refines every vector.
- 3The same kind of block repeats: 12 times in GPT-2 small, 96 times in GPT-3.
- 4The final vector is turned into a probability for every token in the vocabulary.
Pretraining: read an enormous amount of text and guess every next token.
Each wrong guess nudges billions of weights, exactly as in the neural network deck.
From text predictor to assistant
- 1PretrainingLearn to continue text from a huge collection of documents. The result is a base model: it completes documents, it does not answer questions.
- 2Fine-tuningTrain further on example conversations written by people, so the model learns the shape of a helpful reply.
- 3FeedbackPeople compare answers, and reinforcement learning pushes the model toward the ones they prefer.
What it is not
A language model does not look facts up unless it is given a tool such as search. It produces text that is likely, which is not the same as text that is true.
When it states something false with full confidence, that is called a hallucination.
Its knowledge stops at the date its training data was collected. Check anything that matters.
Video · 3Blue1Brown
In eight minutes
A short, visual overview of everything in this deck.

Video · Andrej Karpathy
The one-hour version
What LLMs are, how they are trained and where they are heading.

Go further
Start with the interactive explainer; finish by building one.
- Transformer ExplainerSite · Georgia Tech Polo Club (opens in a new tab)
- Transformers, the tech behind LLMsVideo · 3Blue1Brown (opens in a new tab)
- Attention in transformers, step-by-stepVideo · 3Blue1Brown (opens in a new tab)
- The Illustrated TransformerSite · Jay Alammar (opens in a new tab)
- Let’s build the GPT TokenizerVideo · Andrej Karpathy (opens in a new tab)
- Let’s build GPT: from scratch, in codeVideo · Andrej Karpathy (opens in a new tab)
End of the deckBack to slide decks