All posts

How a large language model works

/AI/5 min read

An LLM does one thing — predict the next token — and repeats it. Learning to do that well on 15 trillion tokens takes about 6.3 × 10²⁴ operations for a 70B model, roughly 18 days on 10,000 GPUs; answering one question with what it learned takes 70 trillion.

A large language model is a program that, given some text, predicts what comes next. Everything it appears to do — answer questions, write code, summarise, translate, reason — is that prediction, repeated one piece at a time.

That sounds too simple to explain what these systems can do. The explanation is in the scale at which the prediction is learned, and in what a model has to absorb to predict well.

Step 1: text becomes tokens

The model never sees characters. A tokenizer cuts text into pieces from a fixed vocabulary — common words whole, rarer words in parts — and replaces each with an integer. "The cat sat on the" becomes five token IDs.

Step 2: tokens become vectors

Each token ID is looked up in an embedding table and replaced by a vector of a few thousand numbers. From here on the model works only with vectors.

Step 3: a stack of transformer layers

The vectors pass through dozens of layers. Each layer does two things:

  • Attention lets each position look at every earlier position and pull in information from the ones that matter. This is how "it" gets connected to the noun it refers to, or a closing bracket to its opening one.
  • A feed-forward network transforms each position's vector on its own. Much of what the model "knows" is stored in these weights.

Residual connections carry each layer's input forward alongside its output, so information is refined layer by layer rather than replaced.

Step 4: a distribution over the next token

The final vector at the last position is multiplied by an output matrix to give one score — a logit — for every token in the vocabulary. A softmax turns the scores into probabilities. After "The cat sat on the", the model might put 40% on " mat", 15% on " floor", 10% on " sofa", and tiny amounts on tens of thousands of others.

Step 5: pick one, append, repeat

One token is chosen — the most likely, or a sample — appended to the text, and the whole process runs again to predict the token after that. A 500-token answer is 500 passes. This loop is called autoregressive generation, and nothing else is happening: no separate reasoning module, no database lookup, no plan held outside the text.

ONE PREDICTION, REPEATED "The cat sat on the" tokens → vectors N layers mat 40%floor 15%sofa 10% append " mat", run again learning it: ~6.3 × 10²⁴ operations · using it: ~140 billion per token 70B parameters, 15 trillion training tokens, one answer of 500 tokens = 70 trillion training costs about 90 billion such answers
Tokens in, a distribution out, one token chosen, and round again.

How it learns: pretraining

The model starts with random weights and predicts garbage. Training shows it vast amounts of text and, at every position, compares its predicted distribution with the token that actually came next. Backpropagation adjusts all the weights slightly to make the actual next token more likely. Repeat for trillions of tokens.

The cost is easy to estimate: about 6 × parameters × training tokens operations. For a 70B model trained on 15 trillion tokens:

6 × 70 × 10⁹ × 15 × 10¹² = 6.3 × 10²⁴ operations

At 40% utilisation of an H100-class GPU, that is about 184,000 GPU-days — 18 days on 10,000 GPUs.

Why does predicting the next token teach anything beyond grammar? Because good prediction requires it. To predict the next word of a physics textbook, it helps to model physics. To predict the next line of a program, it helps to model what the program does. To predict the rest of a sentence about a historical event, it helps to know the event. The training objective is simple; what the model must internalise to satisfy it is not.

From predictor to assistant: post-training

A model trained only on internet text continues documents. Ask it a question and it may produce three more questions, because that is what often follows a question on a web page.

Post-training turns it into an assistant. Supervised fine-tuning on examples of instructions and good responses teaches the format. Reinforcement learning from human feedback — or from AI feedback, or from verifiable rewards such as passing tests and correct maths answers — teaches it to prefer responses people rate as helpful, honest and safe. Reasoning models are further trained to produce long chains of thought before answering, rewarded for reaching correct results.

Post-training uses far less compute than pretraining. It shapes behaviour; the knowledge comes from pretraining.

Using it: inference

Generating each token costs about 2 × parameters operations — 140 billion for a 70B model — plus attention over the context. A 500-token answer is about 70 trillion operations. Training cost about 90 billion times that.

Inference has its own engineering: a KV cache so earlier tokens are not reprocessed, batching many users' requests together, quantization to shrink the weights, speculative decoding to produce several tokens per step.

What follows from how it works

  • It has no memory between requests. Each conversation is text fed in again; "memory" features place stored text in the prompt.
  • Its knowledge stops at its training data, unless current information is provided in the context.
  • It can be fluently wrong. When it does not know, the next-token distribution is spread over plausible options, and one is written with the same confidence as a fact.
  • Context is its working memory. Everything it can use for a given answer must fit in the context window.

What to take away

An LLM turns text into tokens, tokens into vectors, runs them through a stack of attention and feed-forward layers, and outputs a probability for every possible next token. One is chosen and appended, and the process repeats.

Pretraining on trillions of tokens — about 6.3 × 10²⁴ operations for a 70B model — is what makes those predictions knowledgeable; post-training makes them helpful. Each generated token then costs about twice the parameter count in arithmetic.