Skip to content
GenAI Learn/Adapting Models: SFT, LoRA & Distillation
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Supervised Fine-Tuning: Teaching by Example

4 min readChapter 1 of 4

You'll learn to

  • -Describe supervised fine-tuning as next-token training on prompt and answer pairs
  • -Explain why the loss is counted on the answer tokens and not on the prompt
  • -Say what fine-tuning changes well, such as format and style, and what it does not, such as new facts
  • -Spot the averaging choice that gives long answers more weight

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

A pretrained language model has read a huge amount of text and can continue almost anything. What it has not learned is your job: answer in your format, in your tone, using your tools. Supervised fine-tuning, usually shortened to SFT, teaches that by example. You show the model many pairs of a prompt and the answer you wish it had given, and you keep training it to produce those answers.

An Analogy: An Apprentice and Worked Examples

A new support hire already speaks fluent English. On day one you hand them a binder of past tickets, each with the customer's message and the reply a senior colleague wrote. You mark them only on their own replies, not on how well they copied the customer's message. After a few hundred examples they sound like your team. The apprentice did not learn your refund policy from the binder alone, and a reply is only as good as the colleague who wrote it.

What the Training Data Looks Like

Each example is a short conversation. Many tools accept a list of role-tagged messages like the one below, and a chat template turns it into the exact string of special tokens the model was built to read. Quality matters far more than quantity: a few hundred careful, consistent, reviewed examples usually beat thousands of sloppy ones, because the model copies the habits it sees, mistakes included.

One training example: the assistant message is what the model is taught to produce
{
  "messages": [
    {"role": "system", "content": "You are a concise support agent for Northbeam Market."},
    {"role": "user", "content": "Where is my refund?"},
    {"role": "assistant", "content": "Refunds reach your card within 5 business days of approval."}
  ]
}

Score the Answer, Not the Prompt

The training signal is the same next-token loss used in pretraining: at every position, how surprised was the model by the true next token. The key detail is the mask. The prompt tokens are fed in as context, but their loss is thrown away, and only the answer tokens, including the end-of-answer marker, are scored. Otherwise you would waste effort teaching the model to predict the customer's questions instead of your replies.

Press Run: loss with and without the mask, and two ways to average a batch

For the first example the mean loss over every token is 2.214, but over the answer tokens alone it is 1.300, because in these made-up numbers the model finds the prompt harder to predict than the answer. The last lines show the build step: every token of the prompt is marked context only, and only the answer and the end marker are scored. The middle of the output shows a quiet detail. Averaging per example gives 1.525 and averaging over all answer tokens gives 1.480. They differ whenever answers differ in length, because the token average gives long answers more weight. Know which one your training library uses.

What SFT Is Good For, and What It Is Not

  • -Good for: output format, tone, following a house style, calling tools in the right shape, and specializing in a narrow task.
  • -Weak for: adding facts that change, such as prices or policies. Retrieval keeps them fresh and citable, and a fine-tuned model cannot be told to forget a fact.
  • -Risky when overdone: too many passes over a small set make the model memorize the examples and get worse on everything else. Hold out a test set the model never trains on and watch it.
  • -A first step, not the last: models are often tuned with SFT first and then refined with preference methods, which the DPO chapter in the reinforcement learning module covers.

Build the held-out test set before you train, use the same one for every run, and compare against the untuned model with a confidence interval. The Evals modules show how. A tuned model counts only if the held-out numbers say so.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!