Sub topic 8
Fine-tuning: changing the model's behavior
This chapter assumes you have called a chat-style LLM API, can read Python, and know what a context window is. It also assumes you have an evaluation set and a prompt baseline, as covered earlier in this tutorial.
What fine-tuning changes
Fine-tuning is further training of a pretrained model on your own examples. Each example pairs an input with the exact output you want. The training process adjusts the model's weights so that those outputs become more likely.
Prompting and RAG change what the model sees at query time. Fine-tuning changes the model itself. This makes it a tool for behavior gaps: tone, format, labeling conventions, or a house style that a prompt cannot hold steady. It is a poor tool for adding facts. A fine-tuned model may pick up a fact from training, but it will recall that fact unreliably, and you cannot update it without retraining.
What the training data looks like
For chat models, training data is usually a JSONL file. Each line is one complete conversation, and the assistant message is the answer you want the model to learn.
{"messages": [{"role": "system", "content": "You write release notes in our house style."}, {"role": "user", "content": "Fixed crash when exporting empty reports."}, {"role": "assistant", "content": "Fix: exporting an empty report no longer crashes the app."}]}A real file has hundreds or thousands of such lines. Quality matters more than volume. Inconsistent or wrong outputs teach the model to be inconsistent or wrong.
The workflow
Most failed fine-tuning projects fail at the data step, not the training step. When results disappoint, look at your examples first.
Parameter-efficient tuning
Full fine-tuning updates every weight, which needs a lot of GPU memory and produces a full copy of the model. Parameter-efficient methods such as LoRA freeze the original weights and train a small set of added weights. The result is cheaper to train and small enough to store one adapter per task. You will see this covered in more detail in a later chapter.
Costs and limits
| Concern | What to expect |
|---|---|
| Upfront cost | Data preparation and training runs, plus evaluation time |
| Per-request cost | Often lower, because a tuned model needs shorter prompts and fewer examples |
| Freshness | Frozen at training time. New facts need new training. |
| Control | Strong for style and format, weak for guaranteeing specific facts |
| Maintenance | You retrain when the base model, your policy, or your data changes |
Common mistakes
- Fine-tuning to add knowledge that RAG would supply more cheaply and keep current.
- Skipping the prompt baseline, so you cannot show that tuning helped.
- Training on outputs that contain mistakes, then expecting the model to be better than its data.
- Evaluating on examples that also appear in the training set.
- Tuning when a few well-chosen few-shot examples would have been enough.
Checking that it worked
Split your labeled examples before training. Keep the evaluation portion out of the training file. After training, run both the prompt baseline and the tuned model on that portion and compare scores.
def accuracy(model_fn, eval_set):
correct = sum(1 for text, label in eval_set if model_fn(text) == label)
return correct / len(eval_set)
# model_fn wraps one call to a model and returns its answer.
# baseline = accuracy(prompted_model, eval_set)
# tuned = accuracy(tuned_model, eval_set)
# Keep the tuned model only if tuned clearly beats baseline.Exercise
A support bot answers questions about your product accurately when given the right documentation, but its replies are chatty and ignore your required three-part reply format. Is this a knowledge gap or a behavior gap, and is fine-tuning a reasonable next step after prompting?
Show answer
It is a behavior gap, because the facts are correct and the format is wrong. Try prompting with explicit structure and few-shot examples first. If it still misses the format on your evaluation set, fine-tuning on examples of the required format is reasonable. Adding RAG would not help, since knowledge is not the problem.
Exercise
You fine-tuned a model and it scores 98% on its own training examples, but only matches the prompt baseline on your held-out set. What does this suggest, and what do you do?
Show answer
The model has memorized the training examples (overfitting) without learning a general improvement. Check for weak or inconsistent training data, add more varied examples, and judge success only on the held-out set. If tuning still does not beat the baseline, keep the prompt.