Fine-tuning has a reputation for being hard and expensive. For a lot of use cases it is neither, as long as you know when it is the right tool and how to prepare your data. This guide walks through the whole process: what fine-tuning is, when to reach for it (and when not to), how to prepare data, how to run a job with real code, and how to evaluate the result without fooling yourself.
What fine-tuning is, and why you might need it
Fine-tuning means taking a pre-trained model and training it further on your own examples so it performs better on one specific task. You are not building a model from scratch, you are nudging an existing one towards your data, your format, and your style.
Use it when:
- You have domain-specific data (medical, legal, or company-specific knowledge and phrasing).
- You want output in your own voice, template, or house style, consistently.
- You need a reliable output format every time (always valid JSON, always the same structure).
- You want to reduce errors on a narrow, well-defined task.
Do not use it when:
- You just need a general chatbot: use the API as-is, it is cheaper.
- You have fewer than about 100 examples: too little for the model to learn from.
- You need the very latest capabilities: fine-tuned models are frozen and lag new releases.
The trade-off: fine-tuning costs time and money, but on a specific, repetitive task it can unlock large gains a prompt alone cannot reach. The skill is knowing which tasks qualify.
Fine-tuning versus RAG
The most common mistake is fine-tuning when retrieval-augmented generation (RAG) would do. If your problem is "the model does not know my facts", that is a knowledge problem, and RAG solves it by fetching the right documents at query time. If your problem is "the model does not respond in my style or format", that is a behaviour problem, which is where fine-tuning shines.
| Aspect | Fine-tuning | RAG |
|---|---|---|
| Cost | Higher (training plus inference) | Lower (retrieval plus prompting) |
| Time to implement | 1 to 2 weeks | 1 to 2 days |
| When to use | Large datasets, custom format or style | External knowledge, fast iteration |
| Best for | Domain behaviour, writing style | FAQ answering, document lookup |
| Latency | Model itself, unchanged | Slight retrieval overhead |
Rule of thumb: try RAG first. Reach for fine-tuning if RAG is not enough, or you have a large, high-quality dataset and a consistent format to enforce.
Before you start: prepare your data
This is 80 per cent of the work, and where most projects succeed or fail.
Step 1: gather training data
You want at least 100 examples, ideally 500 or more, of input-output pairs. For customer support that is (question, ideal reply). For code generation it is (problem description, clean code). For classification it is (text, label).
Step 2: format it correctly
Providers expect a specific structure. The OpenAI chat format looks like this, one JSON object per line in a .jsonl file:
{
"messages": [
{"role": "system", "content": "You are a support agent for SaaS Company X."},
{"role": "user", "content": "How do I reset my password?"},
{"role": "assistant", "content": "Go to Settings, then Security, then Change Password. Enter your current password, then the new one twice."}
]
}
Anthropic's format is similar but typically omits a separate system turn in the example itself:
{
"messages": [
{"role": "user", "content": "How do I reset my password?"},
{"role": "assistant", "content": "Go to Settings, then Security, then Change Password..."}
]
}
Step 3: validate quality
Check for typos, inconsistencies, and bias in the responses. Remove vague questions and unhelpful answers. Aim for maybe 10 to 20 per cent of your set to cover diverse and edge cases, so the model learns the boundaries, not just the easy middle.
Step 4: split your data
- Training set: about 80 per cent, the examples the model learns from.
- Validation set: about 20 per cent, held back to check it is not just memorising.
- Never let the same example sit in both.
- Keep a small eval set of 10 to 20 hard cases you check by hand after training.
Choosing a base model
Your choice comes down to budget, data privacy, and how much control you want.
| Option | Best for | Trade-off |
|---|---|---|
| OpenAI managed models | Fastest results, no infrastructure | Data leaves your servers, per-token cost |
| Anthropic (Claude) | High-quality reasoning tasks | Fine-tuning access is more limited |
| Open-source (Llama, Mistral, Qwen) | Full control, privacy, cheapest at scale | You manage GPUs, setup, and hosting |
- New to this? Start with a managed fine-tuning API. It is the simplest path from data to working model.
- Need privacy or high volume? Choose an open-source model you can host yourself.
- Smaller is often smarter. A fine-tuned small model frequently beats a giant general model on your narrow task, at a fraction of the cost.
Running the fine-tune: three paths
Path A: managed API (easiest)
Upload your .jsonl file, start a job, wait, then call the resulting model by its ID. The code sketch below uses the OpenAI client, but every managed provider follows the same shape.
from openai import OpenAI
client = OpenAI()
# 1. Upload data
file = client.files.create(
file=open("train.jsonl", "rb"),
purpose="fine-tune"
)
# 2. Start the job
job = client.fine_tuning.jobs.create(
training_file=file.id,
model="gpt-4o-mini-2024-07-18"
)
# 3. Use the resulting model once training completes
response = client.chat.completions.create(
model=job.fine_tuned_model, # e.g. "ft:gpt-4o-mini:my-org::abc123"
messages=[{"role": "user", "content": "How do I reset my password?"}]
)
Path B: open-source with LoRA (most control)
LoRA (Low-Rank Adaptation) trains a small set of adapter weights instead of the whole model, which makes it far cheaper and faster. It runs on a single consumer or rented GPU, even a 24GB card for smaller models. The rough flow: load the base model in 4-bit, attach LoRA adapters, train on your dataset, then merge or serve the adapters. Tools like Hugging Face transformers with peft and bitsandbytes, or wrappers such as Axolotl and Unsloth, handle the plumbing. Choose this when you want the model on your own hardware and to avoid ongoing API fees.
Path C: managed platforms (middle ground)
Services like Together AI, Replicate, or Predibase run the GPUs for you while still giving you open-model flexibility. A good fit if you want open-source models without managing infrastructure yourself.
Evaluating your fine-tuned model
Do not trust "it looks good". Measure it.
- Hold-out test: run your 10 to 20 hand-picked eval examples through both the base and the fine-tuned model, and compare side by side.
- Automatic metrics: for structured tasks, measure exact-match or valid-JSON rate. For free text, score against a rubric using a strong model as a judge.
- Watch for overfitting: if it nails training examples but stumbles on slightly different ones, you trained too long or had too little data.
- A/B in production: route a small percentage of real traffic to the new model and compare quality and cost before a full rollout.
What fine-tuning actually costs
- Managed API: training is billed per token in your dataset, typically a few dollars to low tens for small sets, plus a modest premium on inference versus the base model.
- Open-source (LoRA): GPU rental only, roughly a few dollars per hour for a capable card, and small jobs often finish in under an hour.
- Hidden costs: data preparation time is the real expense, along with evaluation and re-training when your task or data drifts.
Rule of thumb: budget more for data cleaning and evaluation than for the compute itself. The GPUs are rarely what blows the budget.
Deploying your model
- Managed API: nothing to host. Call the returned model ID like any other model.
- Open-source: serve with vLLM, TGI, or Ollama for local use.
- Keep the base updated: fine-tuned models are frozen, so plan to re-fine-tune when a better base ships or your data changes.
- Version everything: tag each model with its training-data version so you can roll back cleanly.
Common pitfalls to avoid
- Too little data. Under 100 examples rarely teaches anything useful. Aim for 500 or more.
- Low-quality data. Garbage in, garbage out. The model copies your examples, mistakes and all.
- Fine-tuning when RAG would do. If the problem is missing facts, use RAG.
- No evaluation set. Without a hold-out set you cannot tell improvement from overfitting.
- Over-training. Too many epochs make the model rigid and worse at anything novel.
- Ignoring cost drift. Fine-tuned inference can cost more per token, so check the maths at your volume.
Verdict: should you fine-tune?
Fine-tune if you have 500 or more high-quality examples, you need a consistent format or style, and prompting plus RAG are not cutting it. Skip it if you mainly need the model to know facts (use RAG), you have little data, or you need the newest model features. Best first step: try prompt engineering, then RAG, then fine-tuning, in that order of increasing cost and effort.
Frequently asked questions
Do I need a PhD or an ML background to fine-tune a model?
No. With a managed fine-tuning API you mainly need good data and a few lines of code. An ML background helps for open-source and LoRA workflows, but the easiest path is open to any competent developer.
How much data is enough to see a difference?
Aim for at least 100 high-quality examples, ideally 500 or more. Quality beats volume: a few hundred clean, consistent examples usually outperform thousands of noisy ones.
Can I fine-tune on private company data safely?
Yes, with care. Check the provider's data handling and retention terms, or host an open-source model yourself for maximum control. Strip out secrets and personal data you do not need, and follow your own compliance requirements.
Fine-tuning, RAG, or prompting: which first?
In reverse order. Start with better prompting, then RAG for knowledge, and only reach for fine-tuning when you need a consistent style or format and the cheaper options fall short.
Will my model get worse when a new base model comes out?
Your fine-tuned model is frozen, so it does not degrade, but newer base models may outperform it. Plan to re-fine-tune periodically when a better base ships or your data drifts.
How long does a fine-tuning job take?
For a small dataset on a managed API, from ten minutes to a few hours. Open-source LoRA jobs on a single rented GPU often finish in under an hour for modest models.
Build with the latest
Fine-tuning APIs, open models, and tooling move fast. The AI Cook keeps you current with a hands-on digest every morning before 8am.
You're in. First issue lands tomorrow morning.