How a Language Model Actually Works
The machinery under the chat box, explained without matrices.
- Level
- Some background helps
- Lessons
- 78
- Reading time
- 667 min
- Price
- Free, no sign-up to read
The machinery under the chat box, for people who already use these tools daily and want to know what is actually happening inside them. It covers how your text becomes numbers, what attention really does, what one transformer block adds, why the context window is not memory, what temperature does and does not fix, how the three training stages differ, and why making things up is a property of the method rather than a defect.
Opens after the AI, Actually Explained exam
Sign in, finish that course, and pass its exam. You can read this syllabus meanwhile.
Go to AI, Actually ExplainedModule 1
From characters to vectors
Everything a model does begins with turning your text into integers and those integers into positions in space. This block walks that path end to end — the vocabulary, the lookup table, the way order is encoded — and finishes with the arithmetic that tells you how big a model is and what is actually inside the file you download.
By the end you can
Trace a piece of text from characters to token ids to vectors and back to a score for every possible next token, predict where a tokenizer will split unfamiliar input, and count the parameters of a model from four numbers on its config page
- 1What a language model actually isA language model is a function from a sequence of tokens to a probability distribution over the next token; the chat interface, the persona and the tools are all layers built on top of that one function.
- 2Tokens, and why a token is not a wordA token is a chunk of characters chosen by a compression algorithm, not a word and not a letter.
- 3How the vocabulary gets built, by handA tokenizer's vocabulary is the frozen output of a compression run over one corpus, so what it splits cleanly is a fact about that corpus and not about language.
- 4Tokenizer failures you will actually hitSeveral everyday model failures are not reasoning failures at all but artefacts of where the tokenizer happened to cut, and they are diagnosable by printing the split.
- 5Embeddings: meaning as a directionAn embedding is not a fixed label on a word; it is a position in space that context rewrites.
- 6Working in embedding space, with free toolsSemantic search is three operations — embed, normalise, take the dot product — and building it once teaches you more about what embeddings can and cannot do than any explanation.
- 7How the model knows what order the words were inAttention has no built-in sense of order, so position is injected as extra information — and the way it is injected sets the ceiling on how far a model can usefully read.
- 8Counting the parameters yourselfParameter count, memory footprint and cost per token all follow from four numbers on a config page, and doing the arithmetic once makes model comparisons concrete rather than vibes.
- 9What is actually inside a model you downloadA model is a folder of numbers plus a configuration and a tokenizer, with no code, no data and no knowledge you can inspect by opening it.
Module 2
Inside the block
The transformer is one small design repeated dozens of times. This block opens it up: attention written out in code you can run, the mask that makes training parallel, the feed-forward half where most of the parameters sit, and the engineering — grouped-query attention, key-value caching, sparse experts — that decides what a model costs to serve.
By the end you can
Implement single-head attention from scratch in NumPy, explain which half of a block holds most of the parameters and which half moves information between positions, and predict how grouped-query attention, key-value caching and a mixture-of-experts design each change memory and speed
- 10Attention, without a single matrixAttention blends information from earlier positions into the current one; it selects nothing and replaces nothing.
- 11Attention in forty lines of NumPyWriting attention once, with real numbers, converts it from an analogy into an operation you can predict the behaviour of.
- 12Why training is parallel and generation is notThe causal mask lets one pass over a document supply as many training examples as it has tokens, which is why training scales with hardware while generation is stuck at one token at a time.
- 13What one transformer block actually doesEvery layer adds to a running total rather than replacing it, and most parameters sit in the feed-forward half.
- 14Many heads, and what has actually been found inside themSome attention heads have been shown to implement identifiable algorithms, most have not, and reading attention weights as an explanation of an output is a documented mistake.
- 15The feed-forward half, where most of the model isTwo-thirds of the parameters sit in a per-position network that acts something like a large set of pattern-triggered lookups, and its neurons respond to many unrelated things at once.
- 16Normalisation, and why training is fragileNormalisation layers exist to keep numbers in a workable range, and where they are placed determines whether a very deep model can be trained at all.
- 17From the final vector to a score for every wordThe last step compares the final vector against every vocabulary entry, and doing that comparison at intermediate layers lets you watch an answer form.
- 18Mixture of experts: big model, small billA sparse mixture-of-experts model has a large parameter count but activates only a fraction per token, which decouples what it knows from what it costs to run.
- 19The key-value cache, and the engineering built around itGeneration is fast only because every earlier token's keys and values are cached, and that cache — not the weights — is what usually limits how many users a GPU can serve.
Module 3
Turning scores into text
The model hands over a list of scores; something else has to turn that into words. This block covers the decoding layer — greedy and beam search, penalties, stop tokens, grammar-constrained output, speculative drafting — and the operational facts that follow, including why temperature zero is not actually deterministic.
By the end you can
Choose and justify a decoding strategy for a given task, read raw log-probabilities to get a usable confidence signal, force structurally valid output without prompt-begging, and explain why identical requests at temperature zero can still differ
- 20Next-token prediction, and where the reasoning comes fromReasoning that appears in the output is partly reasoning being done in the output, one token of compute at a time.
- 21Greedy, beam search, and why chat models refuse bothAlways taking the most likely token produces text that is more probable and less human, which is why generation samples instead of maximising.
- 22Temperature and sampling: what the knob really doesTemperature reshapes a distribution the model already produced; it cannot add knowledge the model lacks.
- 23Reading the numbers the model will give youLog-probabilities are the only quantitative signal a model exposes about its own output, and they are useful for ranking even though they are unreliable as absolute confidence.
- 24Special tokens, stop conditions, and where a turn endsA conversation is one long string containing learned boundary markers, and most "the model went haywire" reports are a boundary marker that was missing or wrong.
- 25Making output structurally valid, without asking nicelyStructure can be enforced by masking impossible tokens at each step, which makes invalid output impossible rather than unlikely.
- 26Asking the same question several timesSampling several answers and aggregating them buys accuracy with compute at inference time, and the gain is largest where the model is nearly right and inconsistent.
- 27Speculative decoding: a small model guesses, a big model checksA large model can verify several drafted tokens in one pass for almost the cost of producing one, so a small draft model makes generation faster with no change to the output distribution.
- 28Why temperature zero still gives you different answersFloating-point addition is not associative, so a request batched differently produces slightly different logits, and a near-tie between two tokens can flip.
- 29Streaming, and the two latencies users feelTime to first token and time between tokens have different causes and different fixes, and conflating them produces optimisation work that users never notice.
Module 4
What the model can see
The context window is the model's entire world for one call, and almost every practical decision about building with these systems is a decision about what goes into it. This block covers the budget, the position bias, caching, retrieval, tools, and the security consequence of there being no boundary between instructions and data.
By the end you can
Budget a context window across system prompt, history, retrieved material and output; predict where in a long prompt information is most likely to be missed; and explain why prompt injection is a structural property of the input format rather than a bug awaiting a patch
- 30The context window is not memoryThe model is stateless; anything it appears to remember was re-sent to it as text this turn.
- 31Budgeting the window, line by lineA context window is a budget shared by the system prompt, tool definitions, history, retrieved material and the response, and every one of those competes with the others.
- 32Where in a long prompt things get missedA model's advertised context length is a capacity, and the length over which it actually uses information reliably is a separate number you have to measure.
- 33Prompt caching, and the ordering it demandsA server can reuse the stored keys and values for a prompt prefix it has seen before, but only if the prefix is byte-identical, which turns prompt ordering into an engineering decision.
- 34Retrieval, as a pipeline that can break in six placesRetrieval-augmented generation is a search system with a model on the end, and most of its failures are search failures that get blamed on the model.
- 35Chunking: the decision that quietly sets your ceilingA chunk must be small enough to be a precise retrieval unit and complete enough to be a usable answer, and those two demands pull in opposite directions.
- 36Tool use, with the magic removedA model does not call a function; it emits text describing a call, and your code decides whether to run it.
- 37Why there is no boundary between instructions and dataEverything in the context is the same kind of thing — tokens — so text from a document or a web page can act on the model exactly as an instruction does.
- 38Images and audio, as more tokens in the same streamA multimodal model converts an image into a sequence of vectors placed in the same context as text, which explains both what it does well and its specific blindness to fine detail.
Module 5
Where the weights come from
Every capability and every bias in a model was put there by a training process with a specific objective, a specific corpus and a specific budget. This block covers all three: what the data is, what the loss actually measures, how the numbers get updated, what the run costs, and what the later stages — supervised tuning, preference training, and reinforcement learning against checkable answers — each change and each fail to change.
By the end you can
Explain what each training stage optimises and what it cannot fix, estimate the compute cost of a training run from parameter and token counts, and choose between prompting, retrieval and fine-tuning by what each one actually changes
- 39Pretraining, fine-tuning, and preference trainingPretraining is where knowledge comes from; the later stages mostly shape behaviour, not facts.
- 40What is actually in the training dataPretraining corpora are mostly filtered web text, and the filtering decisions — what counts as quality, what gets deduplicated, what gets removed — shape the model as much as the architecture does.
- 41What the training objective actually measuresCross-entropy loss measures how surprised the model is by the true next token, and the entire capability of the system is a side effect of driving that one number down.
- 42How a number inside the model gets changedTraining is a loop of measure the error, compute how each parameter contributed, nudge every parameter slightly, repeat — and the memory needed for the nudging is why training costs far more than inference.
- 43Scaling laws, and the correction that changed everythingLoss falls predictably as a power law in parameters, data and compute — and the 2022 correction showing most models were badly undertrained is why small, capable models exist.
- 44What a training run actually costsTwo formulas — 6ND for training compute and 2N per token for inference — let you estimate the cost of any model from public numbers and check any claim you read.
- 45Fine-tuning: what it changes and what it cannotFine-tuning is good at teaching form and poor at adding facts, and confusing the two is the commonest expensive mistake in applied work.
- 46Preference training, from reward models to DPOPreference training optimises a learned proxy for what humans approve of, and every characteristic behaviour of chat assistants — the hedging, the length, the agreeableness — comes from optimising that proxy rather than truth.
- 47Training against answers you can checkWhen correctness can be verified by a program, the reward is exact rather than a learned proxy, and models trained this way learn to spend more tokens working before answering.
- 48Emergent abilities, and the argument about whether they existA capability that appears to switch on abruptly with scale may be a smoothly improving capability measured with an all-or-nothing metric, and the disagreement matters for anyone forecasting what models will do next.
Module 6
Why it goes wrong, by mechanism
Every characteristic failure of a language model — invention, agreement, forgetting what it was told, brittleness, drift, being talked out of its rules, being worse in your language — traced back to a specific part of the machinery from the first five modules, with the test that tells you which one you are looking at.
By the end you can
Diagnose a wrong or unreliable answer by naming the mechanism behind it — sampling, training signal, tokenisation, knowledge conflict, position, version change or data share — run the two-minute test that confirms the diagnosis, and choose the mitigation that acts on that mechanism rather than on the prompt
- 49Why hallucination is a property of the methodThe model always outputs a plausible continuation; nothing in the mechanism checks whether it is true.
- 50Measuring what the model does not knowUncertainty has to be measured at the level of meaning rather than tokens, it only becomes a probability after calibration against labelled outcomes, and no measure of uncertainty can catch an answer the model is confidently wrong about.
- 51Sycophancy: agreement as a trained reflexA model agrees with the user because raters rewarded agreement and the user's belief sits in the context as tokens the model conditions on, so the fix is to keep your view out of the prompt rather than to instruct the model harder.
- 52Two places a fact can live, and what happens when they disagreeA memorised fact and a fact in the context reach the output as two contributions added into the same vector, so the larger one wins and nothing in the architecture prefers what the model was told over what it remembers.
- 53What it means to know a fact: the reversal curseA fact in the weights is a one-way association written in the direction the training text ran, so a model can know that A is B without being able to answer what B is — while a fact in the context works in both directions.
- 54Reasoning that is pattern matching, and how to tellA benchmark score mixes recognising a familiar template with executing a procedure, and the drop in accuracy when names, numbers and an irrelevant sentence are changed is the measurement that separates the two.
- 55Why a comma changes the answerFormat is part of the input distribution — through tokens, learned layouts, position and near-ties — so a score is a range across equivalent formats, and a design that permutes, constrains and ensembles is more robust than a search for the one prompt that works.
- 56Drift: the same name, a different modelA model name is a pointer that providers move, so behaviour changes have to be separated into jitter, version drift and input drift by test rather than by guess, and only a pinned snapshot with a scheduled canary set makes a change attributable.
- 57Why jailbreaks work: two failures of safety trainingRefusal is a trained behaviour covering a narrow slice of inputs, so jailbreaks work either by pitting it against other trained objectives or by moving the request to a region — an encoding, a language, a fiction — where capability generalised and the refusal did not.
- 58Why it is worse in Hindi, and costs moreA tokenizer trained mostly on English splits Devanagari into several times as many tokens, which multiplies cost, halves the usable context and slows every response — and the same data imbalance that caused it also thins the model's knowledge and safety training in that language.
Module 7
Running it: memory, speed and money
What it takes to run a model rather than call one — the hardware limit that sets every speed you will ever see, the memory arithmetic that says whether it fits, what quantisation does to the numbers, how a server keeps a hundred people on one card, what a token really costs, and the architectures being built to escape the transformer's bill.
By the end you can
Predict the tokens-per-second a given model will produce on a given machine from memory bandwidth alone, say whether a model and context will fit in a stated amount of memory, choose a quantisation level and defend it with a measurement, estimate the cost of a token from a GPU's hourly price, and explain the trade an alternative architecture is making
- 59The bandwidth wall: why decode speed is a divisionSingle-stream generation reads every weight once per token, so its speed is memory bandwidth divided by model bytes — which is why quantisation, mixture-of-experts and batching help and why a faster-compute GPU with the same bandwidth does not.
- 60Will it fit: the memory budget of a running modelA running model's memory is weights plus a key-value cache that grows with every token of every concurrent user, so the context and concurrency you can afford is free memory after the weights load divided by the per-token cache cost.
- 61Quantisation: what the numbers loseQuantisation works because rounding errors average out across long dot products, which is exactly why it fails where they cannot — small models, multi-step arithmetic, long-context recall and weakly represented languages — and why perplexity is the wrong number to choose a level by.
- 62Running a model on your own machineA local model is chosen from the memory you have at 0.6 gigabytes per billion parameters, runs at the bandwidth division, and is undone most often by a model that spills into swap or a chat template that does not match the file.
- 63Serving a hundred people from one cardA server fills the compute that single-stream decode leaves idle by scheduling at the token step with a paged cache, so throughput rises almost free with concurrency until compute saturates — and the capacity of a card is the concurrency at which it stops meeting your latency target.
- 64Splitting a model across GPUsThe three ways to cut a model across cards differ in what crosses the wire — a small activation vector for a layer split, an all-reduce per block for a tensor split, routed tokens for an expert split — and only the tensor split adds bandwidth and therefore speed.
- 65The price of a token, from both sidesEvery line on a price list is a serving mechanism invoiced — output costs more because decode is bandwidth-bound, cached input is cheap because prefill is skipped, batch is half price because idle capacity is already paid for — and self-hosting beats the API only at high, steady utilisation.
- 66Distillation: how small models got goodA small model trained to match a large model's full next-token distribution learns from the ranking of every wrong answer as well as the right one, which transfers behaviour and reasoning habits well and long-tail knowledge poorly.
- 67Cascades and routers: paying for the hard cases onlyA cascade is only as good as the signal that decides to escalate, so prefer a deterministic check, then a calibrated score, then consistency — and measure the routed system by re-running a sample of cheap-routed requests through the expensive model.
- 68Beyond the transformer: what the alternatives tradeEvery alternative to attention replaces an exact, growing cache with a fixed-size summary, buying constant per-token cost at the price of exact long-range recall — and none of them changes the failure modes, because those come from the training setup rather than the attention layer.
Module 8
Looking inside: what the weights actually hold
The methods researchers use to read a model from the inside — probes, sparse dictionaries, patching, steering, causal tracing — what each has actually found, what each cannot show, and what that evidence says about memorisation, unlearning, the faithfulness of a model's stated reasoning, and the disputes that hang on those questions.
By the end you can
Explain how a probe, a sparse autoencoder, an activation patch and a steering vector each extract evidence about what a model represents, state what each finding does and does not prove, run a steering experiment on an open model, and say where the questions of memorisation, unlearning and faithful reasoning currently stand
- 69Superposition: more features than dimensionsA model stores more features than it has dimensions by placing them along nearly orthogonal directions that rarely fire together, which is why single neurons look meaningless and why every method for reading a model has to find directions instead.
- 70Probing: asking the activations a questionA linear probe shows that a property is decodable from a layer's activations, which is necessary but not sufficient for the model using it — only an intervention that changes the direction and moves the output shows use.
- 71Sparse autoencoders: a dictionary for the residual streamA wide autoencoder trained to reconstruct activations under a sparsity penalty pulls superposed directions apart into thousands of single-meaning features without labels — a real advance in discovering what a model represents, and not yet a reliable instrument for auditing it.
- 72Patching: finding which parts do the workCopying one activation from a clean run into a corrupted run and measuring the recovery is the only method here that yields a causal claim, and a circuit is the set of components that patching shows to be sufficient and necessary for one narrow behaviour.
- 73Steering: pushing on a directionAdding a feature's direction to the residual stream changes behaviour in the way the feature's meaning predicts, and the discovery that refusal in open chat models is one such direction — removable in an afternoon — is why the model's own refusals cannot be the safety boundary.
- 74Where a fact lives, and whether you can edit itCausal tracing shows a fact is retrieved by mid-layer feed-forward blocks at the subject's position and copied forward by attention, and editing that lookup works on its own terms while failing on the reverse question, on neighbours, and after a few dozen edits.
- 75Is the written reasoning what the model did?A model's stated reasoning is partly the computation and partly a plausible account generated afterwards, with no training signal that rewards reporting the real cause — so a rationale is a set of claims to verify, never evidence of process.
- 76Memorisation, regurgitation, and the dispute that hangs on themA model stores no copies, yet text duplicated many times in the corpus gets a path through the weights that a prefix can retrieve verbatim — memorisation is driven by duplication, measurable by extraction tests, and distinct from the stylistic imitation it is usually confused with.
- 77Unlearning: why removing something is harder than adding itKnowledge is not stored in a place that can be deleted, so current unlearning methods mostly disconnect the prompt from the lookup rather than erase it — and a little fine-tuning, or even quantisation, reconnects it.
- 78What interpretability can and cannot yet tell youModels demonstrably build partial, task-shaped internal models of what their text describes, and no current method can show that a model lacks a property — so a claim of verified absence is a claim nobody can yet make.