Fine-tuning
Learn what Fine-tuning means in AI and machine learning, with examples and related concepts.
Definition
Fine-tuning is the process of taking a pre-trained model and training it further on a smaller, task-specific dataset to specialize its behavior.
A base LLM like GPT-4 or Claude is trained on the entire internet. Fine-tuning narrows that broad knowledge: you feed it hundreds or thousands of examples showing the exact input/output behavior you want, and the model adjusts its weights to match.
Common use cases: matching a specific writing style, learning domain terminology (medical, legal), or consistently outputting in a structured format.
How It Works
Pre-trained Model (general knowledge)
↓
+ Your Dataset (500-10,000 examples)
↓
Fine-tuning Process (hours on GPU)
↓
Specialized Model (your behavior)
Each training example is an input-output pair:
{"messages": [
{"role": "system", "content": "You are a customer support agent for Acme Corp."},
{"role": "user", "content": "How do I reset my password?"},
{"role": "assistant", "content": "Hi! To reset your password: 1. Go to acme.com/reset 2. Enter your email 3. Click the reset link sent to your inbox. Takes about 2 minutes!"}
]}
Why It Matters
- Consistency — The model reliably follows your format, tone, and rules without long prompts
- Prompt savings — Fine-tuned models need shorter prompts, reducing token costs
- Domain expertise — The model learns specialized terminology and reasoning
- Privacy — For self-hosted models, your data never leaves your infrastructure
When to Use Fine-tuning vs RAG vs Prompting
| Approach | Cost | Speed | Best For |
|---|---|---|---|
| Prompt Engineering | Free | Instant | Most tasks, start here |
| RAG | Low | Hours | Factual Q&A, current data |
| Fine-tuning | High | Days | Style, format, behavior |
| LoRA | Medium | Hours | Resource-efficient fine-tuning |
Example
# Fine-tuning with OpenAI API
from openai import OpenAI
client = OpenAI()
# 1. Upload training data
file = client.files.create(
file=open("training_data.jsonl", "rb"),
purpose="fine-tune"
)
# 2. Create fine-tuning job
job = client.fine_tuning.jobs.create(
training_file=file.id,
model="gpt-4o-mini-2024-07-18"
)
# 3. Use the fine-tuned model
response = client.chat.completions.create(
model="ft:gpt-4o-mini:my-org:custom-model:abc123",
messages=[{"role": "user", "content": "How do I cancel my subscription?"}]
)
Key Takeaways
- Fine-tuning changes model behavior, not knowledge — use RAG for adding facts
- Start with prompt engineering; only fine-tune if prompts can’t achieve the consistency you need
- You need quality training data — garbage in, garbage out
- LoRA lets you fine-tune large models with much less compute
- OpenAI, Google, and Hugging Face all offer fine-tuning APIs
Part of the DeepRaft Glossary — AI and ML terms explained for developers.