Understanding Fine-Tuning
This lesson explains where fine-tuning fits among the customization options, what it can achieve, how training data is formatted, and how a fine-tuning job runs in Foundry.
Learning outcomes
After completing this lesson, you will be able to:
- Compare prompt engineering, RAG and fine-tuning.
- Identify the gains fine-tuning can provide.
- Describe the SFT training data format.
- Describe the stages of a fine-tuning job.
Where fine-tuning fits
Generative AI applications can be customized in three main ways, which are usually applied in order. Prompt engineering changes the instructions and examples sent with each request, and costs nothing to try. Retrieval-augmented generation (RAG) supplies current or private knowledge at request time. Fine-tuning changes the model's weights with training examples, so that the behaviour is built into the model rather than repeated in every prompt.
The following table compares the three approaches by what each changes and when it is the appropriate choice.
| Approach | What it changes | Appropriate when |
|---|---|---|
| Prompt engineering | The instructions and examples in each request | The behaviour can be described or shown in a few examples |
| RAG | The knowledge available in each request | Answers depend on documents that are private, large or change over time |
| Fine-tuning | The model's weights | A consistent format, style or narrow skill is needed that prompts cannot achieve reliably, or long prompts make requests slow and costly |
Fine-tuning is not a reliable way to add facts that change. A model fine-tuned on this year's price list will still answer from it next year, and it may mix memorized facts with invented ones. Changing knowledge belongs in retrieval; fine-tuning teaches how to respond.
What fine-tuning can achieve
Fine-tuning is worth its cost when it produces a measurable gain that the other approaches cannot. Typical gains fall into four groups.
- Format and style: always returning a specific JSON structure, following a house style, or using the terminology of a domain.
- Narrow task accuracy: classification, extraction or routing tasks where the base model makes consistent errors.
- Shorter prompts: behaviour that previously required long instructions and many examples is learned, which reduces input tokens and latency on every request.
- Smaller models: a small model fine-tuned for one task can match a larger general model on that task at lower cost, which is the goal of distillation.
Training data format
Fine-tuning data for chat models is stored in JSON Lines (JSONL) files, with one training example per line. For supervised fine-tuning, each example is a conversation in a messages array, and the final assistant message is the response the model should learn to produce.
{"messages": [{"role": "system", "content": "You classify insurance claims. Reply with JSON only."}, {"role": "user", "content": "Pipe burst in kitchen, water damage to floor."}, {"role": "assistant", "content": "{\"category\": \"water_damage\", \"urgency\": \"high\"}"}]}A job uses a training file and, optionally, a validation file in the same format. The validation file is not used to update weights; the job computes loss on it to show whether the model generalizes beyond the training examples. Each model has its own minimum number of examples and file size limits, which must be checked for the selected model. Quality and consistency matter more than volume: a few hundred carefully reviewed examples usually outperform thousands of inconsistent ones.
The fine-tuning job lifecycle
In Foundry, fine-tuning runs as a job on a base model in a Foundry resource. The job uploads the data files, trains, evaluates on the validation file, and produces a fine-tuned model that must then be deployed before it can serve requests.
-
Prepare data
Create, review and split JSONL files into training, validation and held-out test sets.
-
Upload files
Upload the training and validation files to the Foundry resource.
-
Create the job
Select the base model, method, training type and hyperparameters.
-
Monitor training
Watch loss, accuracy or reward metrics and job events while the job runs.
-
Evaluate
Compare the fine-tuned model or a checkpoint with the base model on the held-out test set.
-
Deploy
Create a deployment of the fine-tuned model to serve requests.
The fine-tuned model and its training costs are separate from its deployments. Training is charged per job, and deployments of fine-tuned models are charged while they exist, as the second module explains.
Deciding whether to fine-tune
A claims team uses a chat model to classify incoming claims into 40 categories and return JSON. The prompt contains 3,000 tokens of instructions and 25 examples. Accuracy on a labelled test set is 81 percent, the JSON is occasionally malformed, and the team has 2,500 claims labelled by adjusters. The category definitions change about once every two years.
This is a strong case for supervised fine-tuning. The task is narrow and stable, the desired output can be demonstrated with the labelled claims, the prompt is long and repeated on every request, and the remaining errors are behavioural, not a lack of current knowledge. A fine-tuned model can learn the categories and the JSON format, which removes most of the 3,000-token prompt and its cost.
Adding more examples to the prompt increases tokens further and has limited room to improve. RAG would help if answers depended on changing documents, but the categories are stable. The team should hold out part of the labelled claims as a test set and confirm that the fine-tuned model beats the current 81 percent before deploying it.
Prices in a product catalogue change every week. Should a model be fine-tuned on the catalogue so that it can quote prices?
Show answer
No. Fine-tuning does not reliably add changing knowledge; the model would quote outdated prices. Prices belong in retrieval, and fine-tuning is for consistent behaviour.
Exam Tips
Remember for the exam
- Try prompt engineering first, use RAG for changing knowledge, and fine-tune for consistent behaviour.
- Fine-tuning reduces prompt length and can let a smaller model perform a narrow task.
- SFT data is JSONL with one messages conversation per line, ending with the desired assistant response.
- The validation file measures generalization; it does not update weights.