Lesson 4 of 4 Advanced Prompt Engineering for Agents

Deciding When and How to Fine-Tune Models for Agents

This lesson explains when to fine-tune, which technique to use, how to build the data and how often to retrain. Fine-tuning changes a model's weights so that it behaves differently without long prompts, at the cost of data, training and evaluation work.

Learning outcomes

After completing this lesson, you will be able to:

  • Decide when fine-tuning is the right tool.
  • Choose between supervised fine-tuning, DPO, reinforcement fine-tuning and distillation.
  • Plan training data and retraining frequency.

When fine-tuning is the right tool

Prompting and retrieval come first because they are cheaper to change. Fine-tuning is considered when evaluations show a persistent gap that they cannot close: an output format or style the model follows inconsistently, tool-calling patterns that a domain needs, a narrow task where a small model must reach a large model's quality to meet cost or latency targets, or very long prompts full of examples that are expensive at volume. Fine-tuning is not suited to facts that change, which belong in retrieval, because a fine-tuned model goes stale and cannot cite sources.

Sort into groups

Fine-tune, or prompt and retrieve?

Exam skills 2.1.3

Decide whether each problem calls for fine-tuning or for prompting and retrieval.

Log in to try this activity Interactive activities are free with an account.

Techniques

Foundry supports several fine-tuning techniques for Azure OpenAI and other models. The following table summarizes them.

TechniqueTraining dataBest for
Supervised fine-tuning (SFT)Input and ideal output pairs, including tool callsFormats, style, domain tool use
Direct preference optimization (DPO)A preferred and a rejected output per inputTone and choices between acceptable outputs
Reinforcement fine-tuning (RFT)Tasks with graders that score outputsReasoning models on gradable tasks
DistillationA large model's outputs, often from stored completionsA cheaper, faster model for one agent's task

Distillation is especially relevant to multi-agent systems: production traces of a high-quality but expensive agent provide training data for a small model that then serves that agent's role at a fraction of the cost.

Match the pairs

Fine-tuning techniques

Exam skills 2.1.3

Match each fine-tuning technique with its training data.

Log in to try this activity Interactive activities are free with an account.

Data and frequency

Training data comes from curated production traces, outputs corrected by human reviewers and synthetic examples that fill gaps. It is cleaned of personal data, balanced across intents and edge cases, and split into training, validation and a held-out evaluation set that is never trained on. Quality matters more than quantity: a few hundred excellent examples often outperform thousands of inconsistent ones.

Retraining frequency is driven by evidence rather than habit. Triggers include quality drift detected by continuous evaluation, a change in the task definition or output schema, a new base model version, and accumulated corrections that cover new situations. Each new fine-tuned model is compared with the current one on the evaluation set and released like any other version, with rollback available.

Select all that apply

When to retrain

Exam skills 2.1.3

Pick the events that should trigger retraining a fine-tuned agent model.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reducing the cost of a classifier agent

A routing agent uses a large model with 40 few-shot examples to classify 2 million messages a month into 25 categories with 96% accuracy. The cost is too high, and a small model with the same prompt reaches only 88%. The categories change about twice a year.

Distilling or supervised fine-tuning a small model on labelled production messages (including the large model's verified outputs) is appropriate: it removes the long example prompt and can close the accuracy gap at much lower cost. Retraining is triggered when categories change or when continuous evaluation shows accuracy dropping. Adding more examples to the small model's prompt would raise token costs again.

Check yourself

What does distillation do for a multi-agent solution?

Show answer

It uses an expensive agent's outputs to train a smaller specialist model that performs the task at lower cost.

Exam Tips

Remember for the exam

  • Changing knowledge belongs in retrieval; fine-tune for behaviour, format or cost.
  • Distillation turns an expensive agent's traces into a cheap specialist model.
  • Retrain on evidence (drift, schema change, new base model), evaluated against the current model.