Book a free call

How to Implement Model Distillation Step by Step

Model distillation trains a small student model to copy a large teacher model, so you get most of the quality at a fraction of the cost. Here is how to run it.

Hertiv engineering teamReviewed by a senior engineer
6 min read
How to Implement Model Distillation Step by Step
On this page

Short answer

Model distillation is a technique that trains a small student model to copy the behaviour of a large teacher model, usually by learning from the teacher's outputs on a set of example inputs. The student ends up smaller, faster and cheaper to run, while keeping most of the teacher's quality on the task it was trained for.

What is model distillation?

Model distillation is a technique that trains a small student model to copy the behaviour of a large teacher model. Instead of training the student on labelled data alone, you ask the teacher to answer a set of example inputs, then train the student to produce the same answers.

The teacher's answers carry more information than a plain label. For a classification task, the teacher gives a probability for every class, not just the winning one, which tells the student which answers are close and which are far apart. For a generation task, the teacher's full response shows the student the phrasing and structure to aim for.

The result is a model that is far smaller than the teacher, often ten to a hundred times smaller, yet performs close to it on the specific task. That is why distillation is used when a model must run on a phone, in a browser, or on a server where every millisecond and every cent of compute matters. What model distillation is explains the concept in more depth.

When does distillation make sense?

Distillation makes sense when a large model works well but is too slow or too expensive to run in production, and the task is narrow enough for a small model to learn. It is the right tool for one well-defined job, not for a general-purpose assistant.

Option When to pick it Trade-off
Distillation One task, teacher works well Student only learns that task
Fine-tuning You have labelled data and a base model Needs more data and compute
Prompting Task changes often Slow and costly at scale
Smaller model Task is simple May not reach the quality bar
Model routing Some queries are easy, some hard Needs a routing rule that works

Distillation is a poor fit when the task keeps changing, when you have no teacher that already does it well, or when the student must handle open-ended questions. In those cases, keep the large model and optimise elsewhere. Model distillation costs are worth reading before you commit, since the teacher's inference bill is part of the price.

How do you prepare the teacher and the student?

Preparation decides most of the result. The teacher must be good at the task, the student must be the right size, and the training inputs must look like real production traffic.

  • Pick the teacher. Use the best model you can run for training, even if it is too slow for production. Its quality sets the ceiling for the student.
  • Pick the student. Choose a model small enough to meet your latency and cost targets, but large enough to hold the task. A student that is too small cannot learn the behaviour, however much data you give it.
  • Gather inputs. Collect real inputs from production logs, support tickets or user queries, and remove personal data before using them.
  • Generate teacher outputs. Run the teacher over the inputs and save its full outputs, including probabilities where the task allows it.
  • Hold out a test set. Keep a separate set of inputs and teacher outputs that the student never trains on, so you can measure honestly.

Clean the inputs before training. Duplicates, broken text and off-topic queries teach the student the wrong behaviour, and the teacher's answers to bad inputs are bad training data.

How do you run distillation step by step?

You run distillation as a short loop: generate teacher outputs, train the student, measure the gap, and repeat with better data. A workable sequence looks like this:

  1. Define the task and the quality bar. Write down what the student must do and how good it must be, such as matching the teacher on 90 of 100 test cases.
  2. Collect inputs. Gather a few thousand real inputs that cover the task, including the awkward cases, not only the easy ones.
  3. Generate teacher outputs. Run the teacher over the inputs and store the full outputs with the settings you used.
  4. Train the student. Train the student to copy the teacher's outputs, starting from a small pre-trained model rather than from scratch.
  5. Measure on the test set. Compare the student against the teacher on the held-out set, using the same checks you would use in production.
  6. Fix the gaps. Look at the cases where the student fails, add more inputs like them, and train again.
  7. Ship with a fallback. Route the hardest queries to the teacher at first, and expand the student's share as it proves itself.

Two or three rounds of this loop are usually enough. If the gap is still large after that, the student is probably too small or the task too broad. If you are unsure where to start, LLM fine-tuning services often cover distillation as part of the same work.

How do you measure whether it worked?

You measure distillation by comparing the student with the teacher on the same held-out test set, and by checking latency and cost in production. Quality alone is not enough; the point of distillation is quality per unit of cost.

Measure What it tells you Good sign
Agreement rate How often the student matches the teacher Close to the target you set
Task accuracy How often the student is right Near the teacher's score
Latency Time per request Inside your product's limit
Cost per request Compute and API spend Clearly below the teacher
Error types Where the student fails Same kinds of errors as the teacher

Check the error types, not just the score. A student that fails on the same cases as the teacher is a faithful copy; a student that fails on different cases may have learned a shortcut that will break in production. Machine learning development teams usually track these five measures on a fixed test set.

Example: distilling a support-ticket classifier

Example (hypothetical): a support team uses a large model to sort 10,000 tickets a day into 12 categories. The teacher takes 900 milliseconds per ticket and gets 94 of 100 test tickets right, but it is too slow and too costly to run on every ticket.

The team collects 8,000 real tickets, runs the teacher over them, and trains a student model that is roughly twenty times smaller. On the held-out test set, the student gets 91 of 100 tickets right, three fewer than the teacher, and takes 120 milliseconds per ticket.

The time saving is large: 10,000 tickets at 900 milliseconds is 9,000 seconds, or 2.5 hours of compute per day, while at 120 milliseconds it is 1,200 seconds, or 20 minutes. The team ships the student for the 10 most common categories and routes the 2 hardest categories to the teacher, which keeps accuracy high while cutting most of the cost.

What are the common mistakes?

The most common distillation mistakes are training on the wrong inputs, picking a student that is too small, and skipping the held-out test set.

  • Training on synthetic inputs only. Inputs made up by the team do not match real traffic, so the student learns a task that does not exist.
  • A student that is too small. A tiny model cannot copy a large teacher's behaviour, however much data you give it.
  • No held-out test set. Measuring on training data makes the student look better than it is.
  • Copying the teacher's mistakes. The student learns whatever the teacher does, including its errors, so check the teacher's outputs first.
  • One round only. The first student is rarely good enough; the fix-the-gaps loop is where the quality comes from.
  • Shipping without a fallback. If the student fails on a hard query with no teacher to catch it, the failure reaches the user.

Key takeaways

  • Distillation trains a small student to copy a large teacher's outputs on real inputs
  • It fits one narrow task well, and fails when the task keeps changing or is too broad
  • Clean, real inputs and a held-out test set decide most of the result
  • Measure agreement, accuracy, latency and cost together, not quality alone
  • Ship with a fallback to the teacher for the hardest queries, and expand the student's share over time

Frequently asked questions

How is distillation different from fine-tuning?

Fine-tuning trains a model on labelled data to learn a task directly. Distillation trains a small model on a large teacher's outputs, so the student learns the teacher's behaviour. Distillation usually needs fewer labelled examples, but it needs a good teacher first.

How much data do you need for distillation?

A few thousand real inputs is a common starting point for a narrow task. More helps, but the quality of the inputs matters more than the count. Inputs that match real production traffic teach the student the behaviour that actually matters.

Does the student ever beat the teacher?

Rarely, and only on the narrow task it was trained for. The teacher sets the ceiling, so the student usually lands a little below it. If the student beats the teacher, the teacher was probably not well suited to the task.

Can you distil a model you only access through an API?

Yes, as long as the API terms allow it and the outputs are complete enough to learn from. Check the provider's terms first, since some providers restrict using outputs to train competing models.

What happens if the teacher is wrong?

The student learns the wrong answers too, which is why checking teacher outputs matters. Review a sample of teacher outputs before training, and fix the task definition if the teacher makes systematic errors.

When should you not distil?

Do not distil when the task changes often, when no teacher does it well, or when the student must answer open-ended questions. In those cases, keep the large model and optimise cost elsewhere, such as caching or routing.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: AI and agent systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Planning to shrink a model for production and unsure where to start? Talk to Hertiv.

A senior engineer replies within one business day. Or see our LLM Fine-Tuning service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call