AI ENGINEERING · FINE-TUNING AND ADAPTATION · TRAINING · 28 OF 35

Model Distillation: Small Models From Big Ones

How a big teacher model trains a small student: why it pays at scale with a worked cost comparison at one million requests a day, building a distillation set from real traffic, filtering teacher outputs with checks and a judge, soft labels and temperature versus plain text, task-specific versus general students, measuring quality kept, routing hard cases back, licence terms and keeping the student fresh.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
1 / 19

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Real prompts
  • teacher answers
  • filter
  • fine-tune student
  • measure per slice
  • route hard cases
  • check terms
  • refresh

Notes

The idea

The idea is simple. You have a big teacher model that already does your task well, and a small student model that does not, yet. Run the teacher on many real inputs and keep its answers. Then fine-tune the student on those pairs, exactly like the previous episode. In production you serve the student, which is far cheaper and faster, and you can host it yourself. The price is breadth: it is good at your task and little else.

Teacher and student
  • teacher: a big model that already does the task well
  • student: a small model that does not, yet
  • run the teacher on many real
  • inputs, keep its answers, then fine-tune the student on them
  • serve the student

Cost: the teacher

Let us put numbers on it, with illustrative prices. Each request sends a thousand input tokens and gets two hundred output tokens back. Say the teacher costs three dollars per million input tokens and fifteen per million output tokens. That is zero point three cents for input plus zero point three cents for output, so zero point six cents a request. Times a million requests, that is six thousand dollars a day, about one hundred and eighty thousand a month.

Teacher, 1M req/day
per request: 1,000 in, 200 out
illustrative price per 1M tokens:
  input 3 USD, output 15 USD

in:  1,000 x 3 / 1M  = 0.003
out:   200 x 15 / 1M = 0.003
per request          = 0.006 USD

x 1M a day  = 6,000 USD / day
x 30 days   = ~180,000 USD / month

Cost: the student

Now the student, at fifteen cents per million input tokens and sixty cents per million output. Per request that is zero point zero two seven cents. A million requests cost two hundred and seventy dollars a day, roughly twenty two times cheaper. It is also several times faster. Building it is a one off cost: fifty thousand teacher labels is about three hundred dollars, plus judging and training. It pays for itself within days.

Student, 1M req/day
illustrative price per 1M tokens:
  input 0.15 USD, output 0.60 USD

in:  1,000 x 0.15 / 1M = 0.00015
out:   200 x 0.60 / 1M = 0.00012
per request           = 0.00027

x 1M a day = 270 USD / day
about 22x cheaper than teacher
one-off cost: labels + training,
a few days of teacher traffic

Real prompts

The inputs should come from real traffic, not prompts you made up, because the student only learns the distribution it sees. Sample from your logs, remove duplicates and near duplicates, and strip personal data. Then deliberately add the rare cases: very long inputs, other languages, messy formatting. First, set aside a test set the student never trains on. Ten thousand to a hundred thousand prompts is a common range for one task.

Building the prompt set
  • sample from production logs, not prompts you invented
  • remove duplicates and near-duplicates
  • strip personal data
  • cover rare cases on purpose (long inputs, odd languages)
  • hold out a test set first
  • typical: ~10k-100k prompts

Teacher answers

Labelling runs offline, so the teacher can take its time. Use your best and longest prompt, full of examples and rules. Let it reason step by step. Sample several answers per input and keep the best one. None of that slowness reaches users. A bonus: the student absorbs the long prompt's behaviour, so in production it needs only a short one.

Get the best answers
  • labelling runs offline, so latency does not matter:
  • your best long prompt with examples and rules
  • let it reason step by step
  • sample several answers, keep the best one
  • batch APIs: often ~half price
  • the student sees only the result

Filter the outputs

The student copies the teacher's mistakes as faithfully as its good answers, so filter before training. Run cheap checks first. Is it valid JSON with the right schema? Is the label in the allowed list? Does the code compile and pass tests? Does it agree with the labels you already trust? Then a model judge, with a rubric, for what code cannot check. Expect to drop five to twenty percent, and read a sample yourself.

Checks, then a judge
  • cheap checks first:
  • valid JSON, right schema
  • label is in the allowed list
  • code compiles, tests pass
  • agrees with known labels
  • then an LLM judge with a rubric for the rest
  • drop ~5-20%, and read a sample

Soft labels

So far the student learns from text, the teacher's final answer. Classic distillation goes further, with soft labels. Take a support ticket: card charged twice. A hard label says billing, full stop. The teacher's probabilities say seventy percent billing, twenty percent fraud, eight percent technical. That tells the student billing and fraud are close cousins and technical is far away. Training to match the whole distribution passes on more knowledge per example.

Hard vs soft label
ticket: "card charged twice"
classes:  billing  fraud  tech  other
hard:       1.00   0.00  0.00  0.00
soft:       0.70   0.20  0.08  0.02

soft says: billing, but it looks
a bit like fraud, never like tech

student loss: match the whole
distribution (KL divergence)

Temperature

Soft labels come from the teacher's logits, the raw scores before softmax. Say they are four, two, one and zero. A plain softmax gives about eighty three percent, eleven, four and two. The small ones barely register. Divide every logit by a temperature of two first, and you get fifty eight, twenty one, thirteen and eight. Now the second choices show. Most recipes mix this soft loss with the ordinary loss on the true label.

Softmax of logits [4, 2, 1, 0]
T = 1:  exp = 54.6, 7.39, 2.72, 1
        p   = 0.83, 0.11, 0.04, 0.02

T = 2:  logits / 2 = [2, 1, 0.5, 0]
        exp = 7.39, 2.72, 1.65, 1
        p   = 0.58, 0.21, 0.13, 0.08

higher T: the small classes get
enough weight to learn from
usual mix: soft loss + hard loss

Logits or text

Which kind can you actually do? Text distillation works with any teacher, even one behind an API, because it is just fine-tuning on the teacher's answers. Logit distillation needs the teacher's probabilities. For a classifier that is easy if you host the teacher. For generated text it means probabilities over every token, which usually needs an open weights teacher you run yourself, and student and teacher sharing one tokenizer so the tokens line up.

Which kind can you do?
  • text (sequence-level): works with any teacher, even behind an API. It is just fine-tuning on its answers
  • logits (token-level): needs teacher probabilities, so usually an open-weights teacher you host, and a shared tokenizer for text

Narrow or general

Distillation shines on narrow tasks. Classifying tickets, extracting invoice fields, tagging intent. The student can be tiny, sometimes a small encoder model rather than a chat model, and still keep most of the teacher's quality, because the task needs only a thin slice of what the teacher knows. Distilling a general assistant is a different game. It needs vast, varied data, and the small model still falls behind on reasoning and rare facts.

Task-specific vs general
  • task-specific: classify tickets, extract invoice fields, tag intent student can be tiny, even a small encoder; keeps most of the teacher's quality
  • general-purpose: a small all-round chat model huge data, big quality gap on reasoning and rare knowledge

Measure what you kept

Train the student with LoRA or a full fine-tune, then measure what you kept on the held-out test set. Say the teacher scores ninety two percent and the student eighty nine. The student keeps eighty nine over ninety two, about ninety seven percent of the teacher's quality. Then look per slice, because averages hide damage. Common classes might sit at ninety three while rare ones drop to seventy one. That gap is what to fix next, or route away.

Quality retained
held-out set, gold labels:
  teacher accuracy   92%
  student accuracy   89%
  retained = 89 / 92 = ~97%

per slice, student:
  common classes     93%
  rare classes       71%  <- gap

also: agreement with teacher,
latency p95, cost per request

Route hard cases back

You do not have to choose one model. Put a router in front. The student answers first. If its confidence is low, say its top class probability is under zero point eight, or its output fails a check, the request goes to the teacher. If ten percent go back, the daily cost is ninety percent of two hundred and seventy plus ten percent of six thousand, about eight hundred and forty dollars. Still seven times cheaper, with quality close to the teacher.

A cascade
student answers first
if confidence < threshold
  or a check fails:
  ask the teacher instead

cost with 10% sent back:
  0.9 x 270 + 0.1 x 6,000
  = 243 + 600 = 843 USD / day

~7x cheaper, near-teacher quality

Licence and terms

Check you are allowed to do this. Hosted model terms often restrict using outputs to train a model that competes with the provider, so read them for your exact use. Open weights licences vary too. Some limit how derived models may be used, or require attribution in their names. And the prompts are your users' data, so confirm your privacy policy allows training on them. Record where every example came from, and involve legal early.

Before you distil
  • hosted model terms: many restrict using outputs to build a competing model
  • open-weights licences vary: some limit use or require attribution of derivatives
  • user prompts: does your privacy policy allow training?
  • record where each example came from; ask legal early

Keep it fresh

A student is a snapshot. Your traffic drifts, with new products, new phrasings and new edge cases, and the student will not notice. So send about one percent of requests to the teacher in the background and track how often the two agree. When agreement drops, refresh the set from recent logs, retrain, and rerun the evals. Version every student, and swap only when it wins. When a better teacher arrives, distil again.

Staying up to date
  • shadow ~1% of traffic to the teacher, track agreement
  • alert when agreement drops or new inputs appear
  • refresh the set from recent logs, retrain, re-run evals
  • version every student; swap only if it beats the old one
  • new teacher? re-distil

Common mistakes

The common mistakes are mostly about data. Training on invented prompts that look nothing like real traffic. Skipping the filter, so the student learns the teacher's errors. Testing on prompts that leaked into training, which flatters the score. Trusting one average number and missing the rare slices. Skipping the baseline, when a small model with a good prompt might already be enough. And shipping without a fallback for the cases the student cannot handle.

Avoid these
  • invented prompts that do not match real traffic
  • no filtering: student learns teacher mistakes
  • testing on training prompts
  • trusting the average score
  • no baseline: a small model with a good prompt may do
  • no fallback for hard cases

Numbers worth knowing

Some ballparks. A distilled student is often ten to fifty times cheaper per request and two to ten times faster. One task usually needs five thousand to a hundred thousand examples. Filtering keeps eighty to ninety five percent of the teacher's answers. On a narrow task the student keeps around ninety to ninety nine percent of the teacher's quality, with five to twenty percent of traffic sent back.

Ballparks
cost per request      ~10-50x lower
latency               ~2-10x faster
examples per task     ~5k-100k
teacher answers kept  ~80-95%
quality kept, narrow  ~90-99%
traffic to fallback   ~5-20%
shadow sample         ~1% of traffic
logit temperature     ~2-5
refresh               monthly-quarterly

Say it like this

How to say it. The frontier model cost six thousand dollars a day at a million requests. We sampled fifty thousand real prompts, had the frontier model answer them, filtered with schema checks and a judge, and fine-tuned a small model. On a held-out set it keeps ninety seven percent of the teacher's quality. Low confidence requests, about ten percent, fall back to the big model. Cost dropped about seven times, and we monitor agreement for drift.

In the interview
  • "The frontier model costs 6k a day at 1M requests. We labelled 50k real prompts with it, filtered with checks and a judge, and fine-tuned a small model. It keeps 97% of the quality; 10% low-confidence traffic falls back. Total cost dropped about 7x."

Interview questions

Q1What is model distillation?

Training a small student model to imitate a larger teacher model on a task, using the teacher's outputs or probabilities as training targets. You then serve the student, which is much cheaper and faster.

Q2Where should the prompts for a distillation set come from?

From real production traffic, deduplicated and stripped of personal data, plus deliberately added rare cases. The student only learns the distribution it trains on, so invented prompts leave gaps. Hold out a test set before training.

Q3How do you stop the student learning the teacher's mistakes?

Filter the teacher's outputs before training: cheap programmatic checks such as schema validity, allowed labels, compiling code or agreement with trusted labels, then an LLM judge with a rubric. Sampling several teacher answers and keeping the best also helps.

Q4What are soft labels and why do they help?

Soft labels are the teacher's full probability distribution rather than just its top answer. They show which wrong answers are close, so the student learns more per example. A temperature above 1 softens them, and the loss is usually KL divergence mixed with the normal hard-label loss.

Q5Can you distil from a model behind an API?

You can do text, or sequence-level, distillation by fine-tuning on its answers, as long as the terms of use allow it. Logit distillation needs the teacher's probabilities, which usually means hosting an open-weights teacher and, for text, sharing its tokenizer.

Q6How do you measure how much quality the student keeps?

Score teacher and student on the same held-out set from real traffic and divide: 89 percent over 92 percent is about 97 percent retained. Break it down by slice, since rare classes often drop far more than the average, and also track agreement with the teacher, latency and cost.

Q7How do you handle inputs the student gets wrong?

Use a cascade: the student answers first, and requests with low confidence or failed checks are routed to the teacher. Sending 10 percent back keeps quality near the teacher while most of the cost saving remains.

Q8How do you keep a distilled model up to date?

Shadow a small share of traffic to the teacher and track agreement. When it drops or new kinds of input appear, refresh the set from recent logs, retrain, re-run the evals, and promote the new version only if it beats the current one.