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:

Do not use it when:

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.

AspectFine-tuningRAG
CostHigher (training plus inference)Lower (retrieval plus prompting)
Time to implement1 to 2 weeks1 to 2 days
When to useLarge datasets, custom format or styleExternal knowledge, fast iteration
Best forDomain behaviour, writing styleFAQ answering, document lookup
LatencyModel itself, unchangedSlight 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

Choosing a base model

Your choice comes down to budget, data privacy, and how much control you want.

OptionBest forTrade-off
OpenAI managed modelsFastest results, no infrastructureData leaves your servers, per-token cost
Anthropic (Claude)High-quality reasoning tasksFine-tuning access is more limited
Open-source (Llama, Mistral, Qwen)Full control, privacy, cheapest at scaleYou manage GPUs, setup, and hosting

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.

What fine-tuning actually costs

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

Common pitfalls to avoid

  1. Too little data. Under 100 examples rarely teaches anything useful. Aim for 500 or more.
  2. Low-quality data. Garbage in, garbage out. The model copies your examples, mistakes and all.
  3. Fine-tuning when RAG would do. If the problem is missing facts, use RAG.
  4. No evaluation set. Without a hold-out set you cannot tell improvement from overfitting.
  5. Over-training. Too many epochs make the model rigid and worse at anything novel.
  6. 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.