Knowledge distillation is a training technique in which a small “student” model learns to imitate a larger, already-trained “teacher” model, so the student ends up much cheaper and faster to run while keeping a large share of the teacher’s ability. Instead of learning only from correct answers, the student learns from the teacher’s outputs: its full probability distribution over answers in the classic form, or its generated responses in the form used with today’s language models. Many of the small and “mini” models you can call through an API or run on a laptop were built this way.
This article explains how the teacher-student setup works with a small worked example, shows the two main ways distillation is done for language models, and covers what you give up when you pick a distilled model, along with the licensing limits that apply if you try it yourself.
The teacher-student setup
Distillation always has the same three ingredients: a capable teacher model, a smaller student model, and a set of inputs to run through both. The teacher produces outputs for those inputs, and the student is trained to reproduce them. The idea of compressing a large model into a small one predates deep learning, but the version most people mean today was set out by Geoffrey Hinton, Oriol Vinyals and Jeff Dean in their 2015 paper Distilling the Knowledge in a Neural Network. Their motivation was practical: ensembles of many models predict well but are too expensive to deploy, and they showed the ensemble’s knowledge could be compressed into a single model, including for the acoustic model of a heavily used commercial speech recognition system.
Hard labels vs soft targets
A normal training example has a hard label: this image is a “fox.” That tells the model what is right, but nothing about how wrong the other answers are. A trained teacher, however, outputs a probability for every class, and those probabilities carry extra information. A teacher that gives “dog” far more probability than “truck” for a fox photo is telling the student that foxes look like dogs, not vehicles. Hinton and colleagues called these teacher probabilities “soft targets,” and the extra information they carry is often described as “dark knowledge.”
Worked example: what temperature does
A well-trained teacher is usually very confident, so the useful signal in the small probabilities is almost invisible. Distillation fixes that with a temperature (T): the teacher’s raw scores, called logits, are divided by T before being turned into probabilities with the softmax function. Higher T flattens the distribution.
Take illustrative logits for one image: fox 9.0, dog 6.0, cat 4.0, truck 0.0. Computing the softmax at three temperatures gives:
| Class | T = 1 | T = 2 | T = 4 |
|---|---|---|---|
| fox | 0.946 | 0.760 | 0.536 |
| dog | 0.047 | 0.170 | 0.253 |
| cat | 0.006 | 0.062 | 0.154 |
| truck | 0.0001 | 0.008 | 0.057 |
At T = 1 the student would mostly see “fox, and nothing else matters.” At T = 4 it clearly sees the ranking dog, then cat, then truck. That ranking is information a hard label cannot carry, and it is why a student trained on soft targets can learn more per example than one trained on labels alone. The teacher still has the same opinion at every temperature; temperature only changes how loudly the small preferences come through.
The training loss
In the classic recipe, the student is trained on a weighted mix of two losses: matching the teacher’s softened distribution, and getting the true label right. A minimal PyTorch version looks like this:
import torch.nn.functional as F
def distillation_loss(student_logits, teacher_logits, labels,
T=4.0, alpha=0.5):
# 1) Soft part: match the teacher's softened distribution
soft_teacher = F.softmax(teacher_logits / T, dim=-1)
log_soft_student = F.log_softmax(student_logits / T, dim=-1)
kd = F.kl_div(log_soft_student, soft_teacher,
reduction="batchmean") * (T * T)
# 2) Hard part: still learn the correct label
ce = F.cross_entropy(student_logits, labels)
return alpha * kd + (1 - alpha) * ce
What each part does:
- Dividing both models’ logits by T compares them at the same softened scale.
- KL divergence measures how far the student’s distribution is from the teacher’s; minimizing it pushes the student toward the teacher’s full ranking, not just its top answer.
- Multiplying by T squared keeps the soft loss’s gradients at a comparable size when you change T. The Hinton paper explains this scaling; without it, raising T quietly shrinks the soft loss’s influence.
- The cross-entropy term anchors the student to the true labels, so it does not copy the teacher’s mistakes blindly.
- alpha balances the two. The values here are placeholders; in practice T and alpha are tuned on a validation set.
How distillation is done for language models
Language models predict the next token, so the same idea applies per token, but in practice two broad approaches are used depending on what you can access.
| Approach | What the student learns from | Needs access to | Public example |
|---|---|---|---|
| Logit (soft-target) distillation | The teacher’s probability distribution over the vocabulary at each token | Teacher’s internal outputs; usually both models trained in-house | DistilBERT |
| Sequence-level distillation (synthetic data) | Complete responses generated by the teacher, used as ordinary fine-tuning data | Only the teacher’s text output, e.g. through an API | DeepSeek-R1-Distill models; API distillation workflows |
Logit distillation during pre-training
DistilBERT is the textbook case. Its authors pre-trained a compressed BERT with a loss that combined language modeling, distillation and alignment of hidden states, and reported a model about 40% smaller and 60% faster that kept 97% of BERT’s language-understanding performance (DistilBERT paper). This approach is mostly available to labs that own both models and the training infrastructure.
Sequence-level distillation from generated outputs
Sequence-level distillation skips the probabilities and trains the student on text the teacher wrote. It is simpler, works with closed teachers, and is how reasoning behavior has been transferred to small models. The DeepSeek-R1 model card lists six distilled dense models, from 1.5B to 70B parameters, built on Qwen2.5 and Llama 3 base models and fine-tuned on samples generated by DeepSeek-R1; for the Qwen-based ones it cites 800k curated samples (DeepSeek-R1 model card). If you want background on why reasoning traces are valuable training material, see our explainer on AI reasoning models and extended thinking.
The same pattern works at application scale: run a large model on your real task, store and filter its good outputs, fine-tune a smaller model on them, and compare both on an eval. Some API providers have offered this as a managed workflow, but hosted fine-tuning offerings change and are sometimes withdrawn, so check your provider’s current documentation for which models can be fine-tuned before planning around it.
Distillation vs related techniques
Distillation is often confused with other ways of making models smaller or more specialized. The differences come down to what changes and whether training happens.
| Technique | What changes | Requires training? | Typical goal |
|---|---|---|---|
| Knowledge distillation | A new, smaller model learns the teacher’s behavior | Yes, of the student | Smaller, faster model with similar behavior |
| Quantization | Numeric precision of an existing model’s weights, e.g. 16-bit to 8- or 4-bit | Not necessarily | Less memory, faster inference, same architecture |
| Pruning | Weights, neurons or layers are removed from an existing model | Usually some retraining | Fewer parameters |
| Fine-tuning | An existing model’s weights are adjusted on your data | Yes | Adapt behavior or style to a task |
These combine freely. A common path is to distill a small model and then quantize it to run on a phone or laptop; our article on quantization of AI models covers that second step. Sequence-level distillation through an API is technically a form of fine-tuning, with the difference that the training data comes from a stronger model rather than from humans.
What you give up with a distilled model
A distilled student rarely matches its teacher everywhere, and the gaps tend to appear in predictable places.
- Coverage follows the training data. The student learns what the teacher showed it on the inputs used for distillation. Tasks and domains that were thin in that data are where the student drifts furthest from the teacher.
- Long, multi-step reasoning. Smaller models have less capacity to hold intermediate steps, so the gap usually widens as tasks get longer.
- Broad factual recall. Fewer parameters means less room to store rare facts, so distilled models are more likely to be wrong or vague about obscure topics.
- Inherited errors. The student copies the teacher’s mistakes and biases along with its strengths. In sequence-level distillation, every flawed teacher output that is not filtered out becomes training data.
In practice, distilled and small models tend to suit high-volume, well-defined work such as classification, extraction, routing, short summaries and simple chat, and larger models remain the safer choice for open-ended research, complex coding and nuanced judgment. Our guide to small language models discusses where that line sits. The only reliable way to decide is to run both on your own task, which is what an evaluation set is for.
Distilling a model yourself: a checklist
Distilling your own model is a real machine learning project, not a setting. If you are considering it, these are the decisions that matter, in order.
- Check the teacher’s license and terms first. Many commercial providers restrict using their outputs to build competing models. OpenAI’s terms of use, for example, list using output “to develop models that compete with OpenAI” among prohibited uses (OpenAI terms of use). Open-weight models have their own licenses, and some distilled models inherit conditions from both teacher and base model. Read them before you generate any data.
- Define the task narrowly. Distilling “general intelligence” into a small model is a lab-scale effort. Distilling “classify support tickets into 12 categories” is achievable.
- Build the eval before the dataset. Decide how you will score the student against the teacher on held-out examples. Without this, you cannot tell whether distillation worked.
- Use real, varied inputs. The student only learns the teacher’s behavior on the inputs you provide, so use production-like inputs, including the hard and unusual ones.
- Filter teacher outputs. Drop wrong, refused or malformed responses. Every bad example teaches the student the wrong thing.
- Pick the student and the method. With access to teacher logits (both models open), logit distillation is an option. With only API access, you are doing sequence-level distillation via fine-tuning.
- Compare cost, latency and quality together. A student that is much cheaper but fails your eval on the cases that matter is not a win.
If the task is small enough that a good prompt on a small model already passes your eval, you do not need distillation at all. Prompting and retrieval are cheaper to change than a trained model.
How to tell whether a model you use was distilled
Providers do not always say. The most reliable place to look is the model card or technical report, which for open-weight models often states the training method, as the DeepSeek-R1 model card does. Names that include “distill” are explicit. “Mini,” “small,” “flash” or “lite” labels tell you the model is a smaller tier, but not how it was trained. Our guide to reading an AI model card explains where training details usually appear.



