Fine-Tuning / When & How
Teaching a model to follow instructions from examples.
Reviewed by Yuvaraj
Supervised fine-tuning (SFT) is how a raw pretrained model becomes something that actually does what you ask. A base model has only ever learned to predict the next token across a giant, unlabeled corpus, so prompting it with a question often yields a plausible continuation rather than an answer. SFT fixes this by continuing that same training on a curated set of demonstrations: pairs of an input and the exact output you want the model to produce. The objective does not change; the data does.
SFT uses the identical next-token cross-entropy loss as pretraining. The model is penalized for assigning low probability to each correct next token in the target:
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
The crucial twist is that the sum runs only over the response tokens. The prompt is fed in as context but is excluded from the loss, so the model is graded solely on producing the target completion.
For a curious beginner
You show the model thousands of worked examples of "when asked this, respond like that," and nudge it to imitate the responses, not to re-generate the questions.
How it is actually used
Continue training with a small learning rate on (prompt, response) pairs rendered through the model's chat template. Tokens belonging to the prompt are masked out of the loss; only the assistant tokens produce gradients.
The underlying mechanism
Maximize the conditional likelihood of the demonstrated response given prompt , via token-level cross-entropy summed over . Prompt tokens supply context but contribute zero gradient.
Instruction tuning is simply SFT applied at scale across many diverse tasks (summarize, translate, classify, extract, reason), each phrased as an (instruction, response) pair. Training on this variety teaches a general behavior: read an instruction, then satisfy it. This is what separates a base model from an instruct (or chat) model.
Data is typically stored as JSONL, one conversation per line, using role-tagged messages:
{
"messages": [
{ "role": "system", "content": "You are a concise assistant." },
{ "role": "user", "content": "Convert 3 miles to kilometers." },
{ "role": "assistant", "content": "3 miles is about 4.83 km." }
]
}
A tokenizer applies the model's chat template, flattening this into one token sequence with special role markers. During training each token gets a label; prompt tokens are set to an ignore index (-100 in most frameworks) so they are skipped by the loss:
| Segment | Example tokens | Label / mask |
|---|---|---|
| system + user + role markers | "You are... Convert 3 miles..." | ignored (masked) |
| assistant response | "3 miles is about 4.83 km." | learned (graded) |
Only the assistant span contributes to . The model still reads the prompt (it conditions on it) but is never rewarded for reproducing it.
Keep epochs low, usually 1-3: demonstration sets are tiny relative to pretraining data, and extra passes quickly memorize phrasing, producing repetition and brittle answers. SFT is only the first alignment stage; preference optimization (DPO) or RLHF usually follows to refine which good response the model prefers.
Common mistakes