LLM Foundations
What a language model is, how it is trained, and what actually happens when it answers.
Sections
Interview questions answered at staff level
How LLMs work
Tokens, transformers, prompting and why models make things up.
10 questions02Training, tuning & inference
Pretraining, fine-tuning, RLHF, and the economics of serving a model.
10 questionsRead first
Blog posts for this volume, easiest first
What happens when you ask an AI a question
From pressing enter to the last word: tokens, the desk the model reads from, one-token-at-a-time writing, thinking, web search, and where your words go.
The three dials: tokens, temperature and context
Two bugs show up in nearly every first LLM integration. Both trace back to the same three settings, and both are fixable in a few lines of code.
Prompt, retrieve or fine-tune? A decision guide
Diagnose the failure first, then pick the technique. A worked claims-triage case shows where fine-tuning pays in 2026: cost, not knowledge.
The real cost of an LLM feature: latency, tokens and the bill
Five terms make up the bill and two make up the wait. Reasoning tokens, cache writes, batch discounts and the self-host break-even, worked with stated assumptions.
Thinking models, explained without the hype
What a reasoning model does with its extra time, why the same question can cost ten times more and take forty seconds longer, and a simple rule for when that is worth it.
Structured outputs that don't break
Getting JSON out of a model is easy; getting JSON your code can trust on every call is a system. Schemas, provider structured-output modes versus prompting, validation with targeted retries, and rendering partial JSON while it streams.
Self-host or API? A break-even memo with the maths shown
A decision memo for serving Open-weights vs closed models models yourself versus paying per token, with every price, throughput and headcount stated as an assumption, a cost-versus-volume curve, and a calculator you can rerun with your own numbers.
Key terms
44 pages with diagrams and quizzes
A program that has read a staggering amount of text and learned one trick very well: guess what comes next. Chat, agents and search all run on that trick.
The bite-sized chunk of text a model reads and writes — usually a word or part of one. Length limits, speed and price are all counted in tokens.
How much text a model can hold in one go — instructions, chat, documents and the answer, all together. Outside the window, it does not exist for the model.
Everything you send the model in one request: the instructions, the examples, the documents and the user's question. Your main steering wheel.
The facts a model soaked up during training and stores inside its own numbers. Broad, instant, and frozen on the day training stopped.
When a model says something fluent, confident and simply not true — in the same tone it uses when it is right, which is what makes it tricky.
Training an existing model a little further on your own examples, so it picks up your format, tone or task. Lovely for style, poor for facts.
Search first, then answer: find the documents that matter, paste them into the prompt, and ask the model to answer from them — with citations.
The whole pile of documents your search can see. If something is not in there, no answer will ever mention it.
How a model works out which earlier words matter for the word it is writing right now.
Reusing the work already done on a prompt's repeated opening, so the next request that starts the same way is cheaper and faster.
Forcing the model to produce output that fits your format — valid JSON every time — by blocking any next token that would break it.
Training a small, cheap model to copy a big model's answers on your task. Most of the quality, a fraction of the bill.
Models that take in more than words — photos, scanned PDFs, charts, audio, video — and reason about them alongside your text.
Someone asks in plain words, the model writes the database query, the system runs it safely and turns the result into an answer.
Retrieval where the model decides what to look up, where, and whether to search again — a short loop instead of one fixed search.
Using a model with a clear marking scheme to grade your AI's answers, so a test run can cover hundreds of cases instead of five.
A program that learns a pattern from examples instead of being given rules. The machinery under every model on this site.
The learned numbers inside a model. '70B' means 70 billion of them — which sets memory and cost, and on its own says little about quality.
The design inside nearly every modern model: stacked layers where each word gathers context from the others, then refines itself.
The first and most expensive training stage, where a model learns language and world knowledge by guessing the next word, trillions of times.
The one task a model is trained on and runs every time it answers: given the text so far, score every possible next token.
The text, code and examples a model learned from. Its mix, quality, recency and licensing decide what the model knows and how it behaves.
The measured pattern that error falls predictably as parameters, data and compute grow — with each step costing many times more than the last.
Training a base model on pairs of 'instruction' and 'good response', which turns a text continuer into something that answers you.
Training on human preferences: people pick the better of two answers, those picks train a scoring model, and the model learns to score well.
Methods like DPO that train straight from 'chosen vs rejected' answer pairs — RLHF-like results without the reward model or the RL loop.
Fine-tune by training a small add-on while the original weights stay frozen. Cheap, fast, and swappable per task or customer.
A model picking up a task purely from the instructions and examples in your prompt, with nothing about it changed.
Showing the model a handful of worked examples in the prompt so it copies the format, labels and tone you want.
Having the model write its working before the answer. Better on multi-step problems, paid for in tokens.
Models trained to think at length before answering: much better at hard maths, code and planning, slower and pricier per answer.
The dial between predictable and adventurous when the model picks its next word. It changes variety, not knowledge.
Sample only from the smallest group of likely words that together cover p of the probability — 90%, say — and ignore the weird tail.
The raw scores a model gives every possible next word, before they are turned into probabilities.
Running a trained model to get output. Training happens occasionally; this happens on every single request.
The saved intermediate results for earlier words, so each new word doesn't re-read the whole conversation. It eats GPU memory.
Storing a model's weights in 8 or 4 bits instead of 16, to cut memory and speed things up — at a quality cost you have to measure.
How long the user waits before any words appear. With streaming, this is what 'fast' actually means to them.
Running many requests through the GPU together, so one read of the weights serves lots of tokens. More throughput, slightly slower per stream.
A small model guesses the next few words and the big model checks them all in one pass. Same output, fewer slow steps.
A model made of many sub-networks where each word only uses a few, so a huge model computes like a much smaller one.
Models whose trained numbers you can download and run yourself. Closed models live behind an API. You are trading control for convenience.
The date a model's training data stops. Anything after it is unknown — and the model usually doesn't realise that.