AI Advanced · Training & Fine-Tuning Models

Training & Fine-Tuning Models

Almost everything in this course has treated the model as a finished engine you drive — you prompt it, ground it with RAG, wrap it in agents. This page steps into the other seat: how the engine itself is built and tuned. You’ll get the honest, oriented version — pretraining, fine-tuning, and alignment; when to change the weights versus just changing the prompt; and the tricks (LoRA, distillation, quantization) that make custom models practical instead of ruinously expensive.

☺ Explain it like I’m 10

A model is like a huge robot brain. First it reads a mountain of books until it can talk (that’s the expensive part — done once by a big lab). Then people give it a shorter “manners class” so it answers helpfully and safely. You usually just talk to that finished robot. But sometimes you want it to sound exactly like your company or do one job really well — so you give it a little extra private lesson. That extra lesson is fine-tuning, and there are clever ways to do it without rebuilding the whole brain.

🦫Your host for this topic: Benny the Beaver — Benny doesn’t just use the engines — he builds and tunes them.

Two paths: the model-user and the model-builder

☺ Like you’re 10: Most of this course taught you to drive the car really well. This page pops the hood and shows you how the engine gets made — and when it’s worth learning to tune one yourself instead of just driving.

Nearly every page so far has been about the model-user path: take a capable, already-trained model — Claude, GPT, Gemini, Llama — and get the most out of it with prompting, retrieval, tools, and agent design. That path is where most real value is created today, and it’s where most careers in applied AI live. You never touch the weights; you change what’s in front of the model.

This page is your orientation to the model-builder path: the work of actually shaping the weights so the model itself behaves differently. You almost certainly won’t pretrain a frontier model from scratch — that’s a hundred-million-dollar, thousand-GPU undertaking reserved for a handful of labs. But fine-tuning an existing open model, or an API-hosted one, is very much within reach of a small team, and knowing when it’s the right tool is a genuine skill.

PathWhat you changeWho does itCovered where
🧑‍✈️ Model-userThe prompt, the context, the tools — never the weightsAlmost everyone building with AIMost of this course
🔧 Fine-tunerNudge an existing model’s weights on your dataSmall teams with a specific behaviour needThis page
🏭 PretrainerBuild the base model from raw text, from zeroA few large, well-funded labsOrientation only

Think of this lesson as the bridge between the applied world you already know and the deeper fields next door: deep learning is the machinery underneath, MLOps is how you run training and serving as a real system, and data engineering is where the training data comes from. Here we stay at the orientation altitude: enough to make good build-vs-buy decisions and know where to dig deeper.

Pretraining, fine-tuning, alignment: the three stages

☺ Like you’re 10: First the robot reads the whole library so it can talk (pretraining). Then it practises specific homework so it’s good at a job (fine-tuning). Then it learns manners — be helpful, be honest, don’t say scary things (alignment). Three different lessons, in order.

A modern chat model is built in stages, and they do genuinely different things. Mixing them up is the source of a lot of confusion, so here’s the clean version:

StageWhat happensWhat it produces
1. PretrainingTrain on a vast pile of text (much of the public internet, books, code), predicting the next token over and overA base model — knows language and facts, but just autocompletes; not yet a helpful assistant
2. Supervised fine-tuning (SFT)Continue training on curated examples of the behaviour you want — e.g. instruction → good response pairsA model that follows instructions and takes on a task, tone, or format
3. Alignment (RLHF / DPO)Teach the model human preferences: which of two answers people prefer, so it learns to be helpful, honest, and harmlessThe polished assistant you actually chat with

Pretraining is the expensive, once-per-model step. The result is a “base” model that has soaked up an astonishing amount of knowledge but doesn’t know it’s supposed to be an assistant — feed it “The capital of France is” and it happily continues the sentence, but ask it a question and it might just write more questions. (Why next-token prediction produces so much capability is the story in How models work.)

Supervised fine-tuning takes that base and trains it further on examples that demonstrate the behaviour you want — thousands of high-quality “here’s a request, here’s an ideal answer” pairs. This is what turns an autocomplete engine into something that follows instructions.

Alignment is the manners class, and it’s where the two acronyms you’ll hear live:

Labs also layer in related techniques — Anthropic’s Constitutional AI uses a written set of principles to have the model help critique and improve its own responses, reducing how much human labelling is needed. The details move fast; the shape is stable: learn language → learn the task → learn the preferences. The Responsible AI page goes deeper on what “aligned” should even mean.

When to fine-tune (vs RAG or just better prompting)

☺ Like you’re 10: Three ways to help your robot: tell it clearly what you want (prompting), hand it the right pages to read (RAG), or send it to a private class so the new habit is baked in (fine-tuning). Try the cheap ones first — you bake in a habit only when the first two aren’t enough.

This is the single most important practical decision on this page, and people get it backwards constantly. The reflex to fine-tune is usually wrong. Reach for the cheapest tool that solves your problem, in this order:

  1. Better prompting first. A clearer instruction, a few examples in the prompt (few-shot), or a system prompt that sets tone and format solves a huge fraction of “the model won’t do X” problems — with zero training, instantly editable. Start here always. (See Prompting.)
  2. RAG for knowledge. If the gap is that the model doesn’t know your facts — your docs, your product, this week’s prices — retrieval is almost always the answer. You can update the knowledge the instant a doc changes, and the model can cite its sources.
  3. Fine-tuning for behaviour. If the gap is how the model behaves — a consistent house tone, a strict output format, a specialised skill or domain style it can’t reliably hit even with good prompts — then baking it into the weights can win.
◆ Rule of thumb

Fine-tune for behaviour and style; retrieve for knowledge; prompt for everything you can. If the thing you want to add is a fact, use RAG — facts go stale and fine-tuning can’t cite. If it’s a habit, voice, or format you need consistently, that’s the fine-tuning sweet spot.

Here’s the crux: fine-tuning is a poor way to add knowledge. It’s expensive to redo whenever facts change, it can’t easily point at a source, and models still hallucinate confidently about baked-in facts. It shines instead at consistency of behaviour — matching a brand voice on every single reply, always producing valid JSON in a fixed schema, classifying support tickets in your taxonomy, or writing in a narrow professional style. And these aren’t exclusive: a real system often fine-tunes for style and uses RAG for facts at the same time.

Your problemReach forWhy
“It doesn’t know our internal policies / latest data”RAGKnowledge changes; retrieval updates instantly and cites sources
“It won’t follow the format / tone when I ask”Prompting → then fine-tuningTry instructions first; bake in only if prompts can’t hold it
“Every reply must sound exactly like our brand”Fine-tuningConsistent style across all outputs is what tuning does best
“It must always emit our strict JSON schema”Fine-tuning (or structured-output modes)Reliable format adherence at scale; fewer tokens spent instructing
“It’s bad at our niche domain’s jargon & conventions”Fine-tuningDomain style/skill is a behaviour, learned from examples

One more angle: fine-tuning can also make a smaller, cheaper model good enough at one narrow task to replace a big expensive one — you trade a training cost up front for lower per-call cost forever. That economic case, not knowledge, is often the best reason to do it.

Parameter-efficient fine-tuning: LoRA & PEFT

☺ Like you’re 10: Instead of rewriting the robot’s whole giant brain to teach one new trick, you clip on a tiny extra notebook of changes. The huge brain stays exactly as it was; the little notebook holds just what’s different — and it’s cheap to make, store, and swap out.

A full fine-tune updates every weight in the model. For a modern model that’s billions of parameters — you’d need the memory and compute of the original training run, plus a full-size copy of the model saved for every variant you make. It’s so expensive that almost nobody outside the big labs does it. The answer is parameter-efficient fine-tuning (PEFT): change only a tiny slice of the model and freeze the rest.

The dominant PEFT method is LoRA (Low-Rank Adaptation). The intuition: instead of editing the enormous weight matrices directly, you freeze them and train small “adapter” matrices alongside that capture just the difference your task needs. Because those adapters are tiny — often well under 1% of the model’s parameters — LoRA fine-tuning fits on modest hardware and produces an adapter file measured in megabytes rather than a full model copy in the tens of gigabytes.

Full fine-tuneLoRA / PEFT
What’s trainedAll billions of weightsSmall adapter matrices only (base frozen)
HardwareCluster-scale; rarely feasible outside labsOften a single GPU
Artifact per variantA full model copy (tens of GB)A small adapter (often < 100 MB)
Swapping tasksLoad a whole different modelHot-swap adapters on one base model

That last row is the quietly huge win: you can keep one base model in memory and attach a different LoRA adapter per customer, per task, or per tone — “tune once per behaviour, swap cheaply.” Variants like QLoRA push this further by also quantizing the frozen base (more on quantization below), letting you fine-tune surprisingly large models on a single consumer-grade GPU. This is exactly why the open-model ecosystem exploded: PEFT made custom models a weekend project instead of a data-centre project. Hosted fine-tuning APIs from the major providers use these efficient methods under the hood too, so even when you never see the word “LoRA,” it’s often what’s running.

Base model billions of weights ❄ frozen unchanged, shared LoRA adapter 🦫 tiny & trainable < 1% of params Tuned behaviour base + adapter attach combine

Data: the part that actually decides everything

☺ Like you’re 10: A fine-tune only ever learns from the examples you show it. Show it a few hundred excellent examples and it learns the good habit. Show it a huge pile of sloppy or contradictory ones and it learns to be sloppy. Neat, honest examples beat lots of messy ones.

If you take one thing from this page: fine-tuning quality is data quality. The model copies the patterns in your examples — including the mistakes. A small set of clean, consistent, on-target examples routinely beats a large noisy one. What matters:

Building this dataset is real work, and it overlaps heavily with data engineering. A common and effective shortcut: use a strong model to help draft candidate examples, then have humans review and correct them — you get scale from the model and quality from the humans. (Watch the licence terms: some providers restrict using their outputs to train competing models.)

Distillation & quantization: shrinking models to fit

☺ Like you’re 10: Two ways to make a big heavy robot small enough to carry. One: a clever small student robot watches the big one and copies how it thinks (distillation). Two: you round off the numbers in the robot’s brain so they take less room — a little less precise, a lot lighter (quantization).

These two techniques answer a different question from fine-tuning: not “how do I change the behaviour?” but “how do I make this model small and cheap enough to run where I need it?” — on a phone, at the edge, or just at lower cost and latency. They’re central to the whole local & on-device story.

DistillationQuantization
IdeaSmall student model imitates a big teacherStore the same weights at lower precision
You getA genuinely smaller, faster modelThe same model, lighter to run
Trade-offLoses some of the teacher’s breadthSmall accuracy drop, occasional glitches at aggressive settings
WhenYou want a permanent cheap model for a taskYou want to run an existing model on smaller hardware

They stack, and they stack with everything above: you might take an open base model, fine-tune it with LoRA for your task, then quantize the result to 4-bit so it runs cheaply — a fully custom, deployable model built without ever pretraining anything. For how these shrunken models get served in production, see Production & Ops.

🎬 At the AI Academy
🦫

Benny the Beaver: The client wants every reply in their voice — warm, plain, never salesy. I’m going to fine-tune a small open model on 400 of their best past replies so it just… sounds like them.

🦊

Foxy: Wait — isn’t RAG easier? Just retrieve their style guide and paste it in the prompt?

🦫

Benny the Beaver: For their facts, absolutely — RAG all the way, so it’s always current. But tone isn’t a fact you look up; it’s a habit. Prompting the style works okay and drifts by reply three. Baking it in makes every answer land in-voice, with fewer prompt tokens. Behaviour → fine-tune; knowledge → retrieve.

🐢

Timmy the Turtle: Before we ship it, I ran the same 50 prompts through the base model and the fine-tuned one and had reviewers score “sounds like the client.” Tuned won 44 to 6 — and I checked it didn’t get worse at plain Q&A. That’s a real before/after, not a vibe.

🦉

Professor Owl: Well run. And keep the eval set — when the base model updates next quarter, you’ll re-tune, and that’s how you’ll know the new version is still on-voice.

Evaluating a fine-tune — and the pitfalls

☺ Like you’re 10: Always test the robot before and after the extra lesson, on the same questions. Otherwise you can’t tell if the lesson helped, did nothing, or accidentally made it forget things it used to know.

A fine-tune isn’t done when training finishes — it’s done when you’ve measured that it’s better and not secretly worse. The non-negotiable habit is a held-out evaluation set the model never trained on, run against both the original and the tuned model so you have a real before/after. Judge it on the behaviour you tuned for (does it match the target style/format?) and on the general skills you didn’t want to break. The main ways fine-tunes go wrong:

⚠ Measure, don’t assume

“We fine-tuned it, so it must be better” is how teams ship regressions. Without a held-out before/after eval, a fine-tune can quietly make the model worse at things you weren’t looking at. The eval set is the whole safety net — build it before you train.

Evaluation is a discipline of its own — building good eval sets, using LLM-as-judge carefully, tracking metrics over time — and it lives at the heart of MLOps and Production & Ops. Treat a fine-tuned model like any other software artifact: versioned, tested, monitored, and re-validated whenever the world underneath it moves.

🦫 Benny’s workshop · 20 min

Pick a small open model and a hosted fine-tuning API and run the whole loop once on a toy task — say, rewriting blunt sentences into a friendly tone. (1) Hand-write 30–50 clean prompt→completion examples in JSONL. (2) Set aside 10 as a held-out eval set. (3) Fine-tune with LoRA (most hosted APIs do this for you). (4) Run your 10 eval prompts through both the base and the tuned model and compare. You’ll feel firsthand why data quality dominates — and why the before/after eval is the part you can’t skip.

🐢 Timmy’s checkpoint

(1) Name the three stages that build a chat model, and say what RLHF and DPO each do. (2) Your model doesn’t know your latest pricing — fine-tune or RAG, and why? What if instead it won’t match your brand voice? (3) What does LoRA freeze and what does it train, and why does that make custom models cheap? (4) Name two ways a fine-tune can go wrong, and the one habit that catches both.

Check your answers
  1. The three stages: pretraining (train on a vast pile of text by predicting the next token, giving a base model), supervised fine-tuning (continue training on curated instruction→response examples so it follows instructions), and alignment. RLHF (Reinforcement Learning from Human Feedback) has humans rank outputs to train a reward model, then optimises the model to score well against it; DPO (Direct Preference Optimization) learns directly from “A is better than B” preference pairs without a separate reward model, making it simpler, more stable, and cheaper.
  2. Latest pricing vs brand voice: use RAG for the pricing — that’s missing knowledge, and retrieval updates the instant a doc changes and can cite its source, whereas fine-tuning is a poor way to add facts that go stale. For matching brand voice, fine-tune — a consistent tone is a habit/behaviour, and baking it into the weights makes every reply land in-voice rather than drifting as prompting does.
  3. What LoRA freezes vs trains: LoRA freezes the enormous base weights and trains only small “adapter” matrices alongside that capture just the difference your task needs — often well under 1% of the parameters. That makes custom models cheap because training fits on modest hardware (often a single GPU) and produces a tiny adapter file (megabytes) instead of a full model copy, and you can hot-swap adapters on one shared base.
  4. Two failure modes and the one habit: a fine-tune can suffer catastrophic forgetting (losing general abilities), overfitting (memorising the training set), dating quickly, or distribution mismatch — name any two. The one habit that catches them is a held-out evaluation set the model never trained on, run against both the original and tuned model for a real before/after that checks both the target behaviour and the general skills you didn’t want to break.