Lesson 31.4 · 4 of 5 · 6 min read · Reviewed 24 Nov 2026
Distillation: teacher and student models
By the end, you can
- Explain how a large teacher model trains a smaller student.
- Plan a distillation project with a fair test of the student.
Large models are capable but slow and costly to run at volume. Many products need one narrow skillSkill: A folder of instructions, scripts and resources that an agent loads when relevant to a task. millions of times a day. Distillation moves that one skill from a large teacher into a small student.
Two ways to copy a teacher
| Logit distillation | Data distillation | |
|---|---|---|
| What the student learns from | The teacher’s probability for every possible next token | The teacher’s finished answers, as text |
| Needs | Access to the teacher’s internals, and the same tokenizer | Only the teacher’s outputs |
| Training | A loss that matches the student’s spread to the teacher’s | Ordinary supervised fine-tuning (29.2) |
| Typical use | Model builders shrinking their own models | Teams using a large model to label data for a small one |
Logit distillation carries more information per example. If the teacher thinks “refund” is 70% likely and “return” 25%, the student learns that both are reasonable, not just that one is right. The original paper calls this the teacher’s “dark knowledge”. Data distillation is simpler and is what most application teams do.
A worked example
A support team routes 200,000 tickets a day into 12 categories. A large model does it well, but at about 20x the cost and 5x the latencyLatency: The time from sending a request until you get the response, or until the first token arrives when streaming. they want.
- Collect inputs. Take 30,000 real, de-identified tickets covering every category.
- Teacher labels. The large model labels each ticket with a category and a one-line reason, using a carefully tested prompt.
- Gold set. People label 1,000 separate tickets. These are never used for training.
- Train the student. Fine-tuneFine-tuning: Continuing to train a model on your own examples to change its behaviour or specialise it. a small open model on the teacher’s labels, with LoRALoRA: Low-rank adaptation: fine-tuning a small set of added weights instead of the whole model. (29.3).
- Test. On the gold set: teacher 94% accurate, student 92%. Student cost is a small fraction of the teacher’s per ticket.
- Route the hard cases. When the student is unsure, send the ticket to the teacher (30.5).
Model builders do the same at a larger scale. For example, reasoning traces from a large model have been used to fine-tune much smaller open modelsOpen-weight model: A model whose trained weights are published so you can download, run and fine-tune it yourself., which then reason far better than models of their size trained without them.
What the student loses
- Breadth. It learns the task you trained, not everything the teacher knows.
- A ceiling. It copies the teacher’s errors, and rarely beats the teacher on the task.
- Freshness. When the task changes, you re-label and retrain.
Common mistakes
- Scoring only against the teacher. High agreement can hide shared mistakes. Use a human-labelled gold set.
- Narrow inputs. If the training tickets are all from one month, the student fails on the next season’s problems.
- Ignoring the terms. Many model providers restrict using their outputs to train other models. Read the teacher’s licence or terms of service before you start.
- Skipping the baseline. A small model with a good prompt and a few examples is sometimes enough. Test that before you train anything.
Key points
- Distillation trains a small student model to copy a large teacher's behaviour on a task.
- Logit distillation copies the teacher's full probability spread; data distillation trains on the teacher's written answers.
- A distilled student can be far cheaper and faster on its narrow task, but it rarely beats the teacher there and loses breadth.
- Test the student on held-out examples with trusted labels, not only on agreement with the teacher, and check the teacher's terms of use.
Distil a teacher into a tiny student
Train a small model on a teacher's labels and see why agreement is not accuracy.
You will have: A word-count student trained on teacher labels, with agreement and gold-set accuracy for both models.
Steps
- Read the teacher: a keyword router with one known bias.
- Train the student by counting words under the teacher's label for each training ticket.
- Measure agreement with the teacher on the gold set.
- Measure the teacher's and the student's accuracy against the gold labels, and compare.
Done when
- The student agrees with the teacher on most gold tickets.
- Student accuracy on gold is lower than its agreement.
- The script prints both accuracies.
Open in the Playground No API key needed.
Starter code (Python)
# Distil a "teacher" ticket router into a tiny word-count "student", then test both on gold labels.
import random
from collections import Counter, defaultdict
rng = random.Random(7)
WORDS = {
"billing": ["charged", "invoice", "refund", "payment", "card", "price"],
"delivery": ["parcel", "late", "courier", "tracking", "arrived", "address"],
"account": ["password", "login", "email", "profile", "locked", "settings"],
}
FILLER = ["please", "help", "my", "the", "today", "again", "order", "thanks"]
def make_ticket():
label = rng.choice(list(WORDS))
words = rng.sample(WORDS[label], 2) + rng.sample(FILLER, 3)
if rng.random() < 0.3: # some tickets mention a word from another category
other = rng.choice([c for c in WORDS if c != label])
words.append(rng.choice(WORDS[other]))
rng.shuffle(words)
return " ".join(words), label
def teacher(text):
# A strong but imperfect labeller: it counts keywords, and it over-uses "billing" for "refund".
scores = {c: sum(w in text.split() for w in ws) for c, ws in WORDS.items()}
if "refund" in text.split():
return "billing"
return max(scores, key=scores.get)
train = [make_ticket() for _ in range(400)]
gold = [make_ticket() for _ in range(200)] # labelled by people, never used for training
counts = defaultdict(Counter)
# TODO: for each training ticket, add its words to counts[teacher(text)] (train on the TEACHER's labels)
def student(text):
if not counts:
return "billing"
return max(counts, key=lambda c: sum(counts[c][w] for w in text.split()))
agree = sum(student(t) == teacher(t) for t, _ in gold) / len(gold)
print(f"student agrees with teacher: {agree:.0%}")
# TODO: print the teacher's and the student's accuracy against the gold labels (the second item of each pair)
Show a solution
# Distil a "teacher" ticket router into a tiny word-count "student", then test both on gold labels.
import random
from collections import Counter, defaultdict
rng = random.Random(7)
WORDS = {
"billing": ["charged", "invoice", "refund", "payment", "card", "price"],
"delivery": ["parcel", "late", "courier", "tracking", "arrived", "address"],
"account": ["password", "login", "email", "profile", "locked", "settings"],
}
FILLER = ["please", "help", "my", "the", "today", "again", "order", "thanks"]
def make_ticket():
label = rng.choice(list(WORDS))
words = rng.sample(WORDS[label], 2) + rng.sample(FILLER, 3)
if rng.random() < 0.3: # some tickets mention a word from another category
other = rng.choice([c for c in WORDS if c != label])
words.append(rng.choice(WORDS[other]))
rng.shuffle(words)
return " ".join(words), label
def teacher(text):
# A strong but imperfect labeller: it counts keywords, and it over-uses "billing" for "refund".
scores = {c: sum(w in text.split() for w in ws) for c, ws in WORDS.items()}
if "refund" in text.split():
return "billing"
return max(scores, key=scores.get)
train = [make_ticket() for _ in range(400)]
gold = [make_ticket() for _ in range(200)] # labelled by people, never used for training
# Train the student on the teacher's labels, not the true ones.
counts = defaultdict(Counter)
for text, _ in train:
counts[teacher(text)].update(text.split())
def student(text):
return max(counts, key=lambda c: sum(counts[c][w] for w in text.split()))
agree = sum(student(t) == teacher(t) for t, _ in gold) / len(gold)
teacher_acc = sum(teacher(t) == y for t, y in gold) / len(gold)
student_acc = sum(student(t) == y for t, y in gold) / len(gold)
print(f"student agrees with teacher: {agree:.0%}")
print(f"teacher accuracy on gold: {teacher_acc:.0%}")
print(f"student accuracy on gold: {student_acc:.0%}")
print("Agreement is not accuracy: check the gold set." if agree > student_acc else "Student matches gold as well as it matches the teacher.")