All courses

From base model to assistant

A base model only continues documents in the style of its training text. Post-training turns it into an assistant in three ways. Fine-tuning on example conversations teaches the format. Optimizing toward responses that people prefer, scored by a reward, is reinforcement learning from human feedback (RLHF). Reinforcement learning (RL) on problems with checkable answers produces 'reasoning' models. In RL the model is treated as a policy that chooses tokens and is rewarded for good outcomes. A chat is stored as a single token sequence with markers for system, user and assistant turns.

▶ Watch this lesson Start the whole course

What the animation says

A base model continues documents

A model pretrained on internet text learns one skill: continue a document. Ask it how to reverse a list in Python. It may carry on like a forum thread: I tried reverse, but it gave me nothing back. Or it just adds more questions, like a quiz page. Same network, wrong job. So what turns it into an assistant that answers, follows instructions, and stops when it's done?

The chat is one token sequence

Post-training starts with fine-tuning on example chats, using the same next-token training as pretraining. First, a chat is rendered as one token sequence. Each turn gets a role header, its text, and an end marker. This loop builds the sequence and, alongside it, a mask saying which tokens are learned. The system and user turns are only context. Headers are given, not learned. Tap the two tokens the loss is computed on.

The model as a policy

When the model is used, it writes until it emits the end marker, then the serving code stops. Each token gets a probability, given everything before it. Multiply them to get the whole response's probability. That makes the model a policy: each token is an action. Fine-tuning raises the probability of a demonstration answer someone wrote. Reinforcement learning instead raises the average reward of answers the model samples itself. The reward comes from a model of human preferences, or from a checker. What does a math checker look at?

One RL step: 17 × 3

Only the final answer. Here's one step on: what is seventeen times three? The model samples four responses. They end in fifty-one, forty-one, fifty-one, and fifty-four. The checker gives one, zero, one, zero. The average is one half. The update compares each response with that average. Above it, its tokens become more likely. Below it, less likely. Tap the responses whose tokens become more likely.

Habits, and the three stages

Now repeat that over thousands of problems. Whatever habits lead to correct answers become more likely, like writing out steps or re-checking. That's how reasoning models arise. So, three stages. Fine-tuning learns from example chats someone wrote. RLHF learns from people picking the better of two answers. RL on checkable problems needs a checker for the final answer. And underneath it all, the chat is one token sequence with role markers.

Why doesn't a base model just answer you?

A model pretrained on internet text learns one skill: continue a document in the style of its training text. Give it *"How do I reverse a list in Python?"* and it may continue like a forum thread ("I tried `x.reverse()` but...") or add more questions. So what turns the same network into an assistant that answers, follows instructions, stops when done, and works through hard problems?

Three stages of post-training

1. **Fine-tuning on example conversations**: the same next-token training as pretraining (learn to predict each next token of the text), but on curated chats. It teaches the format and the helpful persona. 2. **RLHF**: the model writes answers, people pick the better of two, and a reward model learns to score answers the way they do. The model is then pushed toward high scores. 3. **RL on checkable problems** (math, code): a checker grades the final answer. This produces **reasoning models**.

Chat as a sequence, model as a policy

A chat is one token sequence: the prompt $x$ (system and user turns, with marker tokens) followed by the response $y=(y_1,\dots,y_n)$. The model gives a probability $p_\theta(y_t\mid x,y_{<t})$ to each possible next token. Here $\theta$ stands for all its adjustable numbers (parameters). As a **policy**, the model picks each token as an action, so a whole response has probability $$\pi_\theta(y\mid x)=\prod_{t=1}^{n}p_\theta(y_t\mid x,y_{<t})$$ - Fine-tuning: adjust $\theta$ to raise $\log\pi_\theta(y^*\mid x)$, and with it the probability of a demonstration answer $y^*$. - RL: adjust $\theta$ to raise $\mathbb{E}_{y\sim\pi_\theta(\cdot\mid x)}[r(x,y)]$, the average reward over responses the model itself samples. The reward $r$ comes from a reward model trained on human preferences (RLHF) or from a checker (reasoning).

One RL step on a checkable problem

Prompt: *What is 17×3?* The model samples 4 responses, which end in **51, 41, 51, 54**. The checker gives rewards $r = 1, 0, 1, 0$, which average 0.5. Nobody grades the working, only the final answer. The update (a small change to $\theta$) compares each response with that average. The two correct responses scored above it, so the probability of their tokens is raised. The two wrong ones scored below it, so theirs is lowered. Repeat this over thousands of problems, and whatever habits lead to correct answers become more likely, such as writing out steps or re-checking.

Rendering a chat into one sequence

A chat template wraps each turn in markers. Words stand in for tokens here. Fine-tuning computes the loss (the error score that training reduces) only on the assistant's tokens, including `<|end|>`. The other turns are just context. At inference (when the trained model is used), the prompt ends with `<|assistant|>`, and the model writes until it emits `<|end|>`. The serving code (the program running the model) then stops and hands the turn back to the user.
chat = [("system", "Be brief."), ("user", "2+3?"), ("assistant", "5")]
seq, train = [], []
for role, text in chat:
    toks = [f"<|{role}|>"] + text.split() + ["<|end|>"]
    seq += toks
    # header is given; only the assistant's own tokens are learned
    train += [False] + [role == "assistant"] * (len(toks) - 1)
print(" ".join(seq))
print([t for t, m in zip(seq, train) if m])  # ['5', '<|end|>']

Remember

A base model continues documents. Fine-tuning on example chats teaches the assistant format. RL treats the model as a policy and pushes it toward high reward: human-preference scores in RLHF, and checked answers for reasoning models. The whole chat is one token sequence with role markers.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.