Addaly is in open beta. Things will change, and AI answers can be wrong — check anything that matters.

How a Language Model Actually Works

The machinery under the chat box, explained without matrices.

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 Explained

Download the textbook (PDF) · free to print and teach from, with the exam paper and every answer at the back.

Module 1

9 lessons · 74 min

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

  1. 1What a language model actually isLocked — this takes you to what opens it. 8 minA 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.
  2. 2Tokens, and why a token is not a wordLocked — this takes you to what opens it. 7 minA token is a chunk of characters chosen by a compression algorithm, not a word and not a letter.
  3. 3How the vocabulary gets built, by handLocked — this takes you to what opens it. 9 minA 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.
  4. 4Tokenizer failures you will actually hitLocked — this takes you to what opens it. 8 minSeveral 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.
  5. 5Embeddings: meaning as a directionLocked — this takes you to what opens it. 7 minAn embedding is not a fixed label on a word; it is a position in space that context rewrites.
  6. 6Working in embedding space, with free toolsLocked — this takes you to what opens it. 10 minSemantic 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.
  7. 7How the model knows what order the words were inLocked — this takes you to what opens it. 8 minAttention 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.
  8. 8Counting the parameters yourselfLocked — this takes you to what opens it. 9 minParameter 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.
  9. 9What is actually inside a model you downloadLocked — this takes you to what opens it. 8 minA 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

10 lessons · 86 min

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

  1. 10Attention, without a single matrixLocked — this takes you to what opens it. 8 minAttention blends information from earlier positions into the current one; it selects nothing and replaces nothing.
  2. 11Attention in forty lines of NumPyLocked — this takes you to what opens it. 10 minWriting attention once, with real numbers, converts it from an analogy into an operation you can predict the behaviour of.
  3. 12Why training is parallel and generation is notLocked — this takes you to what opens it. 8 minThe 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.
  4. 13What one transformer block actually doesLocked — this takes you to what opens it. 8 minEvery layer adds to a running total rather than replacing it, and most parameters sit in the feed-forward half.
  5. 14Many heads, and what has actually been found inside themLocked — this takes you to what opens it. 9 minSome 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.
  6. 15The feed-forward half, where most of the model isLocked — this takes you to what opens it. 9 minTwo-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.
  7. 16Normalisation, and why training is fragileLocked — this takes you to what opens it. 8 minNormalisation 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.
  8. 17From the final vector to a score for every wordLocked — this takes you to what opens it. 8 minThe last step compares the final vector against every vocabulary entry, and doing that comparison at intermediate layers lets you watch an answer form.
  9. 18Mixture of experts: big model, small billLocked — this takes you to what opens it. 9 minA 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.
  10. 19The key-value cache, and the engineering built around itLocked — this takes you to what opens it. 9 minGeneration 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

10 lessons · 81 min

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

  1. 20Next-token prediction, and where the reasoning comes fromLocked — this takes you to what opens it. 8 minReasoning that appears in the output is partly reasoning being done in the output, one token of compute at a time.
  2. 21Greedy, beam search, and why chat models refuse bothLocked — this takes you to what opens it. 9 minAlways taking the most likely token produces text that is more probable and less human, which is why generation samples instead of maximising.
  3. 22Temperature and sampling: what the knob really doesLocked — this takes you to what opens it. 6 minTemperature reshapes a distribution the model already produced; it cannot add knowledge the model lacks.
  4. 23Reading the numbers the model will give youLocked — this takes you to what opens it. 9 minLog-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.
  5. 24Special tokens, stop conditions, and where a turn endsLocked — this takes you to what opens it. 8 minA 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.
  6. 25Making output structurally valid, without asking nicelyLocked — this takes you to what opens it. 9 minStructure can be enforced by masking impossible tokens at each step, which makes invalid output impossible rather than unlikely.
  7. 26Asking the same question several timesLocked — this takes you to what opens it. 8 minSampling 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.
  8. 27Speculative decoding: a small model guesses, a big model checksLocked — this takes you to what opens it. 8 minA 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.
  9. 28Why temperature zero still gives you different answersLocked — this takes you to what opens it. 8 minFloating-point addition is not associative, so a request batched differently produces slightly different logits, and a near-tie between two tokens can flip.
  10. 29Streaming, and the two latencies users feelLocked — this takes you to what opens it. 8 minTime 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

9 lessons · 74 min

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

  1. 30The context window is not memoryLocked — this takes you to what opens it. 7 minThe model is stateless; anything it appears to remember was re-sent to it as text this turn.
  2. 31Budgeting the window, line by lineLocked — this takes you to what opens it. 8 minA 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.
  3. 32Where in a long prompt things get missedLocked — this takes you to what opens it. 9 minA 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.
  4. 33Prompt caching, and the ordering it demandsLocked — this takes you to what opens it. 8 minA 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.
  5. 34Retrieval, as a pipeline that can break in six placesLocked — this takes you to what opens it. 9 minRetrieval-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.
  6. 35Chunking: the decision that quietly sets your ceilingLocked — this takes you to what opens it. 8 minA 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.
  7. 36Tool use, with the magic removedLocked — this takes you to what opens it. 8 minA model does not call a function; it emits text describing a call, and your code decides whether to run it.
  8. 37Why there is no boundary between instructions and dataLocked — this takes you to what opens it. 9 minEverything 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.
  9. 38Images and audio, as more tokens in the same streamLocked — this takes you to what opens it. 8 minA 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

10 lessons · 86 min

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

  1. 39Pretraining, fine-tuning, and preference trainingLocked — this takes you to what opens it. 8 minPretraining is where knowledge comes from; the later stages mostly shape behaviour, not facts.
  2. 40What is actually in the training dataLocked — this takes you to what opens it. 9 minPretraining 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.
  3. 41What the training objective actually measuresLocked — this takes you to what opens it. 8 minCross-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.
  4. 42How a number inside the model gets changedLocked — this takes you to what opens it. 9 minTraining 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.
  5. 43Scaling laws, and the correction that changed everythingLocked — this takes you to what opens it. 9 minLoss 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.
  6. 44What a training run actually costsLocked — this takes you to what opens it. 8 minTwo 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.
  7. 45Fine-tuning: what it changes and what it cannotLocked — this takes you to what opens it. 9 minFine-tuning is good at teaching form and poor at adding facts, and confusing the two is the commonest expensive mistake in applied work.
  8. 46Preference training, from reward models to DPOLocked — this takes you to what opens it. 9 minPreference 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.
  9. 47Training against answers you can checkLocked — this takes you to what opens it. 9 minWhen 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.
  10. 48Emergent abilities, and the argument about whether they existLocked — this takes you to what opens it. 8 minA 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

10 lessons · 85 min

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

  1. 49Why hallucination is a property of the methodLocked — this takes you to what opens it. 8 minThe model always outputs a plausible continuation; nothing in the mechanism checks whether it is true.
  2. 50Measuring what the model does not knowLocked — this takes you to what opens it. 9 minUncertainty 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.
  3. 51Sycophancy: agreement as a trained reflexLocked — this takes you to what opens it. 8 minA 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.
  4. 52Two places a fact can live, and what happens when they disagreeLocked — this takes you to what opens it. 9 minA 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.
  5. 53What it means to know a fact: the reversal curseLocked — this takes you to what opens it. 8 minA 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.
  6. 54Reasoning that is pattern matching, and how to tellLocked — this takes you to what opens it. 9 minA 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.
  7. 55Why a comma changes the answerLocked — this takes you to what opens it. 8 minFormat 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.
  8. 56Drift: the same name, a different modelLocked — this takes you to what opens it. 8 minA 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.
  9. 57Why jailbreaks work: two failures of safety trainingLocked — this takes you to what opens it. 9 minRefusal 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.
  10. 58Why it is worse in Hindi, and costs moreLocked — this takes you to what opens it. 9 minA 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

10 lessons · 89 min

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

  1. 59The bandwidth wall: why decode speed is a divisionLocked — this takes you to what opens it. 9 minSingle-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.
  2. 60Will it fit: the memory budget of a running modelLocked — this takes you to what opens it. 9 minA 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.
  3. 61Quantisation: what the numbers loseLocked — this takes you to what opens it. 10 minQuantisation 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.
  4. 62Running a model on your own machineLocked — this takes you to what opens it. 9 minA 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.
  5. 63Serving a hundred people from one cardLocked — this takes you to what opens it. 9 minA 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.
  6. 64Splitting a model across GPUsLocked — this takes you to what opens it. 8 minThe 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.
  7. 65The price of a token, from both sidesLocked — this takes you to what opens it. 9 minEvery 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.
  8. 66Distillation: how small models got goodLocked — this takes you to what opens it. 9 minA 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.
  9. 67Cascades and routers: paying for the hard cases onlyLocked — this takes you to what opens it. 8 minA 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.
  10. 68Beyond the transformer: what the alternatives tradeLocked — this takes you to what opens it. 9 minEvery 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

10 lessons · 92 min

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

  1. 69Superposition: more features than dimensionsLocked — this takes you to what opens it. 9 minA 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.
  2. 70Probing: asking the activations a questionLocked — this takes you to what opens it. 9 minA 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.
  3. 71Sparse autoencoders: a dictionary for the residual streamLocked — this takes you to what opens it. 10 minA 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.
  4. 72Patching: finding which parts do the workLocked — this takes you to what opens it. 9 minCopying 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.
  5. 73Steering: pushing on a directionLocked — this takes you to what opens it. 9 minAdding 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.
  6. 74Where a fact lives, and whether you can edit itLocked — this takes you to what opens it. 9 minCausal 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.
  7. 75Is the written reasoning what the model did?Locked — this takes you to what opens it. 9 minA 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.
  8. 76Memorisation, regurgitation, and the dispute that hangs on themLocked — this takes you to what opens it. 10 minA 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.
  9. 77Unlearning: why removing something is harder than adding itLocked — this takes you to what opens it. 9 minKnowledge 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.
  10. 78What interpretability can and cannot yet tell youLocked — this takes you to what opens it. 9 minModels 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.

No ads. No data sale. No public scores on people. Ever.

© 2026 Addaly