HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1402 min read

Knowledge Distillation

Teaching a Small Model Your Big Model's Job

AI engineeringAI engineering / model optimization & cost

The situation

You have one narrow LLM call running hot: classify a support ticket, extract fields from an invoice, rewrite a product title. 2M calls/month, ~800 in / 60 out tokens.

Frontier model (~$3/M in, $15/M out): ≈ $6,600/month, p95 ≈ 1.9s.

Fine-tuned 8B on your own task: ≈ $200/month, p95 ≈ 200ms.

That ~30x gap is the whole reason distillation exists. You are not chasing a better model — you are buying the same answer at a different price.

The recipe (4 steps)

1️⃣ Harvest — log REAL production inputs + the teacher's outputs. Not synthetic prompts you invented. For one narrow task, 10k–50k examples is usually plenty.

2️⃣ Clean — the teacher is wrong sometimes, and every uncaught error becomes a permanent student belief. Cheap filter: sample the teacher 3x at temp 0.7, keep only rows where all 3 agree. Better: verify against anything mechanical you have (a regex, a DB lookup, the user's later correction).

3️⃣ Fine-tune small — LoRA on a 7–8B base is usually enough for a single narrow task. You are not teaching knowledge, you are teaching a format and a decision boundary.

4️⃣ Cascade, don't replace — student answers first; if its confidence is low, escalate to the teacher. A 90/10 split keeps ~90% of the savings and almost all of the accuracy.

The bit people miss

"Distillation" is not the same as "fine-tune on the teacher's text."

Classical distillation trains on the teacher's probability DISTRIBUTION — soft targets. "refund 0.6 / billing 0.3 / other 0.1" carries far more signal per example than the single word "refund": it tells the student which mistakes are near-misses and which are absurd. Same data, several times the learning rate per row.

Via an API you can't get the full distribution — but on a classification task, top-k logprobs get you most of the way there. Most people never ask for them.

Three traps

  • Eval against the teacher, not the truth. "97% agreement with teacher" can be 97% agreement on being wrong, and it hides your real ceiling. Keep a human-labelled holdout.
  • Distribution shift. The student is only as good as the traffic you harvested. Launch a new product line in March and the student has never seen it — while the teacher would have coped fine. Monitor INPUT drift, not just accuracy.
  • Distilling reasoning badly. If you train a small model only on the teacher's final answers to multi-step problems, you teach it to guess confidently. Train on the reasoning traces too — or go on-policy: sample the student, have the teacher correct it, train on the corrections.

What to expect

Narrow, well-specified task (classify / extract / rewrite): student lands within 1–3 points of the teacher.

Open-ended judgement, planning, multi-step agent loops: the gap stays wide. Don't distill your agent's planner.

Your exercise

Pick the highest-volume narrow LLM call in your stack. Pull 1,000 real input/output pairs from logs.

Before any fine-tuning, run those 1,000 inputs through a cheap small model with 5 few-shot examples and measure agreement with the teacher. That's a 1-hour experiment instead of a 1-week one — and sometimes the answer is that you never needed the fine-tune at all.

Then ask the real question: what agreement % would you need before you'd actually ship it?