How LLMs Actually Work
Strip away the hype and a large language model is a next-token predictor. You feed it a sequence of tokens, and it outputs a probability distribution over what token comes next. That's it. Everything impressive (and everything frustrating) about LLMs flows from this single idea.
Autoregressive Token Generation
Training: Learning Patterns from Text
Training an LLM starts with a massive corpus of text — books, websites, code, forums, academic papers. The model reads a sequence, predicts the next token, checks whether it was right, and adjusts its internal weights via gradient descent to be slightly less wrong next time. Repeat this billions of times across terabytes of text.
The weights that result from this process are the model's parameters. A 70B parameter model has 70 billion learned floating-point numbers. These aren't stored facts — they're compressed statistical patterns about how language works. The model learns grammar, reasoning patterns, factual associations, and coding conventions all from the same objective: predict the next token.
The Transformer Architecture
Before 2017, sequence models were dominated by RNNs (recurrent neural networks), which processed tokens one at a time, left to right. The transformer architecture changed everything. Here's the pipeline:
- Embeddings — each token is mapped to a high-dimensional vector
- Positional encoding — position information is added (transformers have no built-in sense of order)
- Stacked attention + feed-forward blocks — the core of the model, repeated dozens or hundreds of times
- Output layer — a probability distribution over the entire vocabulary
The key innovation is self-attention. For each token in the sequence, the model computes how much it should attend to every other token. It does this using three learned projections — queries, keys, and values. Each token's query is matched against every other token's key to produce attention weights, then those weights are used to create a weighted sum of the values.
This is why transformers beat RNNs: attention lets every token directly relate to every other token regardless of distance. No more vanishing gradients across long sequences. And because all positions are processed in parallel (not sequentially), training is dramatically faster on GPUs.
Training Stages
Modern LLMs go through multiple training stages:
Pretraining is the expensive part — predicting next tokens across the entire corpus. This produces a base model that's good at continuing text but not at following instructions.
Supervised fine-tuning (SFT) teaches the model to follow instructions by training on curated prompt/response pairs. This is where "be a helpful assistant" behavior comes from.
RLHF / preference tuning further aligns the model using human preferences. Humans rank multiple model outputs, and the model learns to prefer the higher-ranked style. This step reduces harmful outputs and improves response quality.
Inference: Generating Text
When you send a prompt to an LLM, inference is autoregressive: the model predicts one token, appends it to the sequence, then predicts the next token given the new longer sequence. This repeats until the model produces a stop token or hits a length limit.
At each step the model produces probabilities for every token in its vocabulary. How you select from those probabilities matters:
- Temperature controls randomness. Temperature 0 always picks the highest-probability token (deterministic but repetitive). Higher temperatures flatten the distribution, producing more varied output.
- Top-p (nucleus sampling) considers only the smallest set of tokens whose cumulative probability exceeds p. This dynamically adjusts the number of candidates — narrow when the model is confident, wider when it's uncertain.
temperature=0.0 → always picks the most likely token (greedy)
temperature=0.7 → moderate creativity, good default for most tasks
temperature=1.5 → high randomness, useful for brainstorming
top_p=0.9 → only sample from tokens covering 90% of probability mass
Why LLMs Hallucinate
LLMs aren't databases. They were trained to produce plausible continuations of text, not to verify facts against a ground truth. When the model doesn't "know" something, it doesn't say "I don't know" by default — it generates the most plausible-sounding next tokens, which can be confidently wrong.
This is a fundamental property of the architecture, not a bug that will be patched out. The model is always doing the same thing: predicting what token would most plausibly come next given the context.
Why They Can't Count Letters
Ask an LLM how many r's are in "strawberry" and it might get it wrong. This isn't because the model is bad at counting — it's because the model never sees individual characters. The tokenizer splits "strawberry" into subword tokens (something like str|aw|berry). The model operates on these tokens, not on individual letters. It has no native mechanism for character-level inspection, which is why tasks involving character counting, spelling, or anagram solving are unreliable.
Key Takeaways
- LLMs are next-token predictors trained on massive text corpora — parameters are compressed patterns, not stored facts
- The transformer's self-attention mechanism lets every token attend to every other token, enabling parallelism and long-range dependencies
- Training has three stages: pretraining (predict next token), supervised fine-tuning (follow instructions), and RLHF (align to human preferences)
- Inference is autoregressive — one token at a time — with temperature and top-p controlling the randomness of sampling
- Hallucination is inherent: the model is trained to be plausible, not factual
- Token-level processing explains why LLMs struggle with character-level tasks like counting letters
Next up, we'll look at the building blocks the model actually operates on — tokens, embeddings, and the context window that constrains everything.
Enjoyed this breakdown?
Get new lessons in your inbox.