Learn
Training & fine-tuning

What is Knowledge Distillation in AI?

The short answer

Knowledge distillation trains a smaller student model to imitate a larger teacher. The teacher can supply soft probability distributions over tokens, sampled answers, or full reasoning traces. The student learns the behaviour at lower inference cost, which is why most small models shipped today are distilled from larger ones.

On this page

The core idea

Distillation replaces a hard training signal with a soft one. In ordinary supervised learning the target for each token is a single correct next token, encoded as a one-hot vector. In distillation the target is the teacher's full probability distribution over the vocabulary at that position. The student is trained to match that distribution, usually by minimising the Kullback-Leibler divergence between teacher and student outputs, optionally mixed with the ordinary cross-entropy against the true next token.

The idea predates large language models. Hinton, Vinyals and Dean introduced the modern formulation in 2015 as a way to compress an ensemble of models into a single network, using a temperature parameter to soften the teacher's distribution so that the small probabilities carry usable information. The same mechanism now runs in reverse: a large expensive model teaches a small cheap one, and the small one is deployed.

It helps to be precise about what is transferred. Distillation does not copy weights, and it does not copy a database of facts. It transfers a function: the mapping from context to a distribution over next tokens. If the student has enough capacity and sees enough of the input distribution, it can approximate that function well enough to behave similarly, at a fraction of the compute per token.

Three forms in LLM practice

The forms differ in what the student is allowed to see, and that determines both cost and fidelity. White-box distillation needs access to the teacher's logits, which means running the teacher yourself or having provider support for returning them. Black-box distillation needs only sampled text, which works with any API but throws away most of the signal.

Reasoning distillation is a special case of the black-box form that has become dominant for small reasoning models. The teacher is prompted to produce a long chain of thought before its answer, the whole trace is captured, and the student is trained on the trace as well as the final response. The bet is that the student learns a procedure rather than a lookup table, and that the procedure generalises to problems the teacher never saw.

  • Logit distillation: match the teacher's per-token distribution. Highest fidelity, requires shared tokenizer and logit access, and is the most expensive to run because the teacher must score every training sequence.
  • Sequence distillation: train the student on teacher-generated text with ordinary cross-entropy. Simple, works through any API, and discards the information in the teacher's uncertainty.
  • Reasoning distillation: train on the teacher's intermediate traces plus final answers. Effective for maths, code and multi-step tasks, but sensitive to trace quality and length.
  • On-policy distillation: the student generates, the teacher scores or corrects, and the student trains on its own trajectories. Best match between training and inference, most complex to implement.

Why soft targets carry more signal

A one-hot label says that one token is right and every other token is equally wrong. A teacher's distribution says much more: that two continuations are nearly equally plausible, that a third is unlikely but not impossible, and that a fourth is essentially excluded. This is the dark knowledge that makes distillation work. For a prompt like naming the capital of a country, the teacher's distribution over neighbouring countries encodes a similarity structure that a hard label cannot express.

The gradient consequences are concrete. Soft targets produce smaller, better-conditioned gradients per example because the student is pulled toward a whole distribution rather than pushed violently away from every alternative. The variance of the gradient across examples is lower, which is why distilled students often train stably at higher learning rates and generalise better than students trained on hard labels alone, even when both see identical prompts.

The effect is strongest when the teacher is genuinely uncertain and the uncertainty is informative. If the teacher is near-deterministic, its distribution is almost one-hot and the advantage shrinks to nothing. This is part of why distillation of a highly confident specialist into a general student transfers less than people expect: there is no nuance to transfer.

Temperature, divergence and the objective

Temperature divides the logits before the softmax. At temperature one you get the model's ordinary distribution; at temperature two or five the distribution flattens and the small probabilities become visible; at very high temperature it approaches uniform and the signal is destroyed. The classic recipe uses a temperature of two to twenty for small classification problems, then multiplies the distillation loss by temperature squared to keep gradient magnitudes comparable when you change it. Large language models usually work at temperature one or two, because the vocabulary is enormous and the tail is already informative.

The choice of divergence matters more than most tutorials admit. Forward KL, the teacher's distribution in the numerator, is mode-covering: the student is penalised heavily for assigning near-zero probability where the teacher assigns mass, so it spreads probability broadly and can produce vague output. Reverse KL is mode-seeking: the student is penalised for putting mass where the teacher does not, so it concentrates on the teacher's dominant modes and ignores the tail. Reverse KL often produces sharper, more confident students; forward KL often produces students that better match the teacher's diversity. Some pipelines mix both, or use a Jensen-Shannon-style symmetric objective.

In practice most LLM distillation uses plain cross-entropy on teacher samples, which is a crude approximation to forward KL with a single sample. That is fine when data is abundant and the teacher is strong, and it is the only option when you have text but not logits. Knowing which objective you are actually optimising prevents a lot of confusion when the student turns out more repetitive than the teacher.

Storing logits is a real engineering constraint. A single token position over a 128,000-token vocabulary costs 256KB in fp16, so a one-million-token corpus needs about 256GB before compression. Most pipelines keep only the top few hundred logits per position plus the teacher's chosen token, which preserves nearly all of the useful signal at a fraction of the storage. If you are paying an API for logprobs, the per-token cost of returning them usually dominates the bill, which is another reason sequence distillation is the common default.

On-policy versus off-policy

Off-policy distillation trains the student on sequences the teacher produced. It is simple and parallel, but it suffers from the same exposure bias as ordinary SFT: the student is trained on the teacher's trajectory and then asked to continue its own, and its own mistakes take it into states it never saw in training. A student that goes slightly wrong early in a derivation will not have been taught how to recover, because the teacher never went wrong.

On-policy methods close that gap. The student generates sequences, the teacher evaluates or corrects them, and the student is trained on its own outputs with the teacher's feedback as the target. Generalised knowledge distillation formalises this by mixing student-generated and teacher-generated sequences with a student-generated proportion that increases during training, and it consistently outperforms pure off-policy distillation in the reported settings. The cost is that generation is sequential and the teacher must score every sampled sequence, so throughput drops by an order of magnitude.

A pragmatic middle ground is rejection sampling: sample several student attempts per prompt, keep those the teacher or a verifier rates highly, and add them to the SFT set. It captures some of the on-policy benefit at a fraction of the engineering complexity, and it is the method most small teams actually use.

Reasoning distillation and its discontents

Training a student on a teacher's chain of thought looks like it should transfer reasoning. Sometimes it does: small models trained on long, well-structured traces from a strong teacher show large gains on competition mathematics, and the gains persist on problems that were not in the training set. The mechanism is plausibly that the trace teaches the student a decomposition it would not have discovered from answers alone.

The failure modes are equally well documented. Traces can be unfaithful: a model may write a plausible derivation that does not reflect how it reached its answer, and a student trained on it learns the performance of reasoning rather than the substance. Traces can be wrong: a teacher that reaches the right answer through a flawed argument teaches the flaw. Traces can be verbose: the student learns to pad, and its inference cost rises until the efficiency argument for distillation disappears. And traces can leak: a teacher may reproduce memorised text from pretraining, which the student then memorises too.

Filtering is therefore part of the method, not an optional extra. Keep traces only when the final answer verifies, when the length is within budget, when the reasoning does not contradict the answer, and when a separate judge model rates the derivation as sound. For code and mathematics you can verify mechanically with tests or symbolic checks, which is far more reliable than a judge. If you cannot verify, expect to inherit the teacher's error rate roughly unchanged.

Risks and limits

  • Capacity gap: a very small student cannot represent a very large teacher's function, and the mismatch shows up as a student that imitates style without the underlying competence.
  • Error inheritance: systematic teacher mistakes become systematic student mistakes, and they are harder to remove later because they are baked into the weights.
  • Diversity collapse: training repeatedly on the same teacher distribution narrows the student's output distribution, which is good for consistency and bad for exploration.
  • Licence and terms: many providers prohibit using outputs to train a competing model, and open-weight licences may impose attribution or naming conditions.
  • Contamination: if the teacher saw the evaluation set during pretraining, the student inherits an inflated score that does not reflect real ability.
  • Distillation is not compression of the same model; it is training a new model, and it needs its own evaluation.

When it is the right tool

Distillation pays when you have a strong teacher, a well-defined input distribution, and a hard constraint on inference cost or latency. Typical cases: replacing a frontier API with a self-hosted small model for a narrow task, building an on-device model for a phone, or reducing the cost of a high-volume classification or extraction pipeline by an order of magnitude.

It does not pay when the task requires the teacher's full breadth, when you cannot legally use the teacher's outputs, or when you have fewer than a few thousand diverse prompts. It is also the wrong first move when the real problem is that you are sending everything to an expensive model: routing easy requests to a small model and hard ones to the large one often captures most of the savings with a fraction of the effort, and it keeps a correct answer available when the student fails.

Distillation is also not the same as quantisation, though the two are often combined. Quantisation keeps the same weights at lower precision and loses a little accuracy; distillation trains different, smaller weights. A common production recipe is to distil into a smaller architecture, then quantise the result, then evaluate the pair together, because the errors interact and the best quantisation setting for the teacher is not necessarily the best for the student.

A workable pipeline

Start by defining the distribution you care about and sampling a few thousand prompts that represent it, including the tails. Generate teacher outputs, and where possible logits. Filter aggressively for correctness, format and length. Split into train and held-out sets at the prompt level, not the response level. Train the student with a mix of distillation loss on teacher outputs and ordinary cross-entropy on any human-written data you have, because a small amount of ground truth anchors the student when the teacher is wrong.

Evaluate on three axes: quality against the held-out set, agreement with the teacher on prompts neither saw, and cost per thousand requests at the latency you need. The last one is the whole point, and it is the one people forget to measure. A student that is ninety-five percent as good at forty percent of the cost is a win; a student that is ninety-eight percent as good at eighty percent of the cost probably is not, once you count the engineering time.

Page feedback

Was this helpful?

A quick signal helps improve these notes.

Report a mistake Straight to the lab inbox

Put it to work

See the models, datasets and gateway this lab runs.