Skip to content
AI Engineering
How LLMs Work

Pretraining, fine-tuning and RL

Models are built in stages. Each stage changes what the model is good at and why it behaves the way it does.

Chapter 2: How LLMs WorkLesson 5 of 6Level: CoreAbout 15 min
Words on this page (17)
pretraining
: The first, large training stage: predict the next token across a huge amount of text.
token
: A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
fine-tuning
: Further training on a smaller, targeted set of examples to change behavior or style.
RLHF
: Reinforcement learning from human feedback: tune a model toward answers that people rate higher.
DPO
: Direct preference optimization: a simpler way to tune a model toward answers people prefer, without a separate reward model.
reinforcement learning
: Learning by trial and error: take actions, get rewards, and favor what earned reward.
reward
: A number that says how good an outcome was. RL tries to maximize it.
tool use
: The model asks your program to run a function and reads the result.
tool
: A function the model can ask your program to run, described by a name, description and argument schema.
prompt
: The input text you give a model, including instructions, examples and data.
training
: Adjusting a model's parameters on data so its predictions improve.
policy
: A rule for choosing actions. In a language model, the model itself is the policy.
retrieval
: Finding the most relevant pieces of text for a query.
weights
: The learned numbers that define a trained model. A model whose weights are published is called open-weight.
loss
: A number measuring how wrong the model's output is. Training tries to reduce it.
gradient
: The direction and rate at which the loss changes when each parameter changes. Training steps against it.
gradient descent
: Repeatedly nudging parameters opposite to the gradient to reduce the loss.

What it is

A chatbot is built in stages. Each stage teaches it something different.

Stage one is . The model reads a huge amount of text and learns to predict the next . This is where its knowledge and its ability with language come from. The result is a base model. It can continue any text, but it does not know it is supposed to help you.

Stage two is supervised , or SFT. People write thousands of example conversations: a question, then a good answer. The model trains on them and starts to answer like an assistant.

Stage three is preference tuning. People compare two answers and pick the better one, and the model is nudged toward the kind people prefer. You will see the names and for this. Both mean learning from human preferences.

Stage four, used for reasoning models, is on tasks with checkable answers, such as math and code. The model gets a when the final answer is right, so it learns to think longer and more carefully.

Why it matters

Pretraining has a cutoff date, so the model knows nothing newer. Politeness, refusals and come from the later stages. That is why a can change how a model behaves, but cannot reliably teach it new facts.

When you need new facts, retrieve them and put them in the prompt. When you need the same style or format every time at large scale, consider fine-tuning.

A worked example: what each stage adds

Ask a base model "How do I bake bread?" It might continue with "How do I bake a cake? How do I bake cookies?", like a list of search suggestions. After instruction tuning it gives steps. After preference tuning the steps get clearer, safer and better organized. After reasoning it can work through a long problem, such as scaling a recipe for 37 people.

What one training example looks like

In pretraining, one sentence produces many practice questions. Take the six tokens "The cat sat on the mat" (invented split). The model is shown "The" and must predict "cat". Then "The cat" and must predict "sat". That gives 5 predictions from one sentence, each scored by how much probability the right token got. Billions of such sentences make up the run.

Fine-tuning uses the same scoring, but on conversations, and usually only the assistant's reply counts. Same mechanism, different data.

Trade-offs: prompt, retrieve or fine-tune

Match the to what is missing.

Missing facts that change often: retrieve them. A company with 200 pages updated weekly cannot re-tune each week, and can show its sources.

Missing behavior you can describe: write a clearer prompt first, since it is free and instant.

Missing consistency at huge scale: fine-tune a small model. A brand voice in a fixed three line format, millions of times a day, is a good fit. The costs are collecting good examples and the risk of side effects.

What goes wrong after tuning

Symptoms to watch for:

  1. A tuned model gets worse at unrelated tasks, because the update shifted it needed.
  2. Answers have the right format but wrong facts. Format can be taught. Knowledge mostly cannot.
  3. Reinforcement learning on checkable tasks can be gamed. A coding model might special-case the exact tests instead of solving the problem.

To confirm, test on examples that were not used in tuning. Also rerun a small set of general tasks before and after the change.

Common mistake

Fine-tuning a model to "teach" it your company facts. Facts are better supplied when the question is asked, through retrieval. That keeps them up to date and shows the sources.

The math, when you are ready

Training is repeated guessing, measuring the error and nudging the weights. The numbers behind that loop are in Loss and gradient descent. It is optional for now.

Check yourself

A model does not know about a product released last month. What is the right fix?

Which stage mainly produces instruction-following behavior?

You want every answer in a strict house format, millions of times a day. What should you consider?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page