Fine-Tuning Fundamentals
Fine-tuning is the process of further training a pre-trained model on your specific data. It customizes the model's behavior, knowledge, and output style for your use case.
When to Fine-Tune (and When Not To)
Fine-Tune When:
- You need a consistent output format that prompt engineering can't achieve
- You have a specific domain with specialized terminology
- You need to reduce costs by training a smaller model to match a larger one
- You want to reduce latency by avoiding complex prompts
- You need the model to adopt a specific style or persona
Don't Fine-Tune When:
- Prompt engineering or few-shot examples solve the problem
- RAG can provide the needed knowledge
- You have less than 50 training examples
- The task changes frequently (fine-tuned models are static)
Decision Framework
Need specific knowledge? → RAG
Need specific behavior? → Fine-tuning
Need both? → Fine-tuning + RAG
Can solve with prompting? → Don't fine-tuneHow Fine-Tuning Works
Pre-trained Model (GPT-4o-mini)
│
▼
┌───────────────────┐
│ Fine-Tuning with │
│ Your Data │
│ (100-10K examples)│
└───────────────────┘
│
▼
Custom Model (ft:gpt-4o-mini:your-org:your-model)The model's weights are adjusted slightly based on your training data, making it better at your specific task while retaining its general capabilities.
Fine-Tuning with OpenAI
Step 1: Prepare Training Data
Format: JSONL file with conversation examples:
{"messages": [{"role": "system", "content": "You are a customer support agent for BIGXSTAR."}, {"role": "user", "content": "How do I reset my password?"}, {"role": "assistant", "content": "To reset your password:\n1. Go to bigxstar.com/login\n2. Click 'Forgot Password'\n3. Enter your email address\n4. Check your inbox for the reset link\n5. Create a new password (minimum 8 characters)\n\nNeed more help? Contact support@bigxstar.com"}]}
{"messages": [{"role": "system", "content": "You are a customer support agent for BIGXSTAR."}, {"role": "user", "content": "What are your pricing plans?"}, {"role": "assistant", "content": "BIGXSTAR offers three plans:\n\n**Free Plan**: Basic features, up to 100 transactions/month\n**Pro Plan (₹499/mo)**: Advanced analytics, unlimited transactions\n**Enterprise (₹999/mo)**: Custom integrations, priority support, dedicated account manager\n\nAll plans include a 14-day free trial. Would you like to know more about any specific plan?"}]}Step 2: Upload and Create Fine-Tuning Job
from openai import OpenAI
client = OpenAI()
# Upload training file
training_file = client.files.create(
file=open("training_data.jsonl", "rb"),
purpose="fine-tune"
)
# Create fine-tuning job
job = client.fine_tuning.jobs.create(
training_file=training_file.id,
model="gpt-4o-mini-2024-07-18",
hyperparameters={
"n_epochs": 3,
"learning_rate_multiplier": 1.8,
"batch_size": 4
}
)
print(f"Job ID: {job.id}")
print(f"Status: {job.status}")Step 3: Monitor Training
# Check status
job = client.fine_tuning.jobs.retrieve(job.id)
print(f"Status: {job.status}")
# List events
events = client.fine_tuning.jobs.list_events(job.id, limit=10)
for event in events.data:
print(f"{event.created_at}: {event.message}")
# Output:
# "Step 100/300: training loss=0.25"
# "Step 200/300: training loss=0.18"
# "Step 300/300: training loss=0.12"
# "Fine-tuning job succeeded"Step 4: Use Your Fine-Tuned Model
# Use just like any other model
response = client.chat.completions.create(
model="ft:gpt-4o-mini-2024-07-18:your-org::abc123",
messages=[
{"role": "system", "content": "You are a customer support agent for BIGXSTAR."},
{"role": "user", "content": "How do I upgrade my plan?"}
],
temperature=0.3
)
print(response.choices[0].message.content)Training Data Guidelines
Quantity
| Quality Level | Examples Needed | Result |
|---|---|---|
| Minimum | 10-50 | Noticeable style change |
| Good | 50-200 | Reliable task performance |
| Great | 200-1000 | Strong domain expertise |
| Excellent | 1000+ | Near-human quality |
Quality Rules
- Consistent format — All examples should follow the same structure
- Diverse inputs — Cover edge cases and variations
- High-quality outputs — Your training data IS the model's behavior
- Balanced classes — Don't over-represent any category
- No errors — Every example should be a "perfect" response
Common Mistakes
❌ Too few examples (< 10)
❌ Inconsistent formatting between examples
❌ Training data contains errors you want to avoid
❌ All examples are similar (no diversity)
❌ Missing edge casesHyperparameters
| Parameter | What It Does | Guidance |
|---|---|---|
n_epochs | How many times to go through training data | Start with 3, increase if underfitting |
learning_rate_multiplier | How much to adjust weights | Lower (0.5) = conservative, Higher (2.0) = aggressive |
batch_size | Examples processed at once | Larger = more stable, smaller = faster learning |
Signs of Overfitting
Training loss keeps decreasing but validation loss increases
→ The model memorizes training data instead of learning patterns
→ Solution: Reduce epochs, add more diverse dataSigns of Underfitting
Training loss stays high
→ The model hasn't learned the patterns
→ Solution: More epochs, higher learning rate, more dataCost of Fine-Tuning
| Model | Training Cost | Inference Cost |
|---|---|---|
| GPT-4o-mini | $3.00 / 1M tokens | $0.30 input / $1.20 output |
| GPT-4o | $25.00 / 1M tokens | $3.75 input / $15.00 output |
Training cost = number of tokens × epochs × per-token price
Example: 200 examples × 500 tokens × 3 epochs = 300K tokens ≈ $0.90 for GPT-4o-mini
Practical Use Cases
1. Consistent JSON Output
Train the model to always output in your specific schema:
{"category": "billing", "priority": "high", "summary": "...", "next_action": "..."}2. Brand Voice
Train on your company's actual support responses to match tone and style.
3. Domain Classification
Train a classifier for your specific categories (support ticket routing, content moderation).
4. Code Generation in Your Stack
Train on your codebase's patterns, naming conventions, and frameworks.
Fine-Tuning vs Alternatives
| Approach | Effort | Cost | Best For |
|---|---|---|---|
| Better prompts | Low | Low | Most tasks |
| Few-shot examples | Low | Medium (more tokens) | Format compliance |
| RAG | Medium | Medium | Knowledge-heavy tasks |
| Fine-tuning | High | High (upfront) | Style, format, efficiency |
Key Takeaways
- Try prompting first — Fine-tuning is a last resort, not a first step
- Quality > Quantity — 100 excellent examples beat 1000 mediocre ones
- Consistent format in training data produces consistent outputs
- Monitor training metrics — Watch for overfitting and underfitting
- Fine-tuning + RAG is the most powerful combination for domain-specific applications
Next, we'll explore LoRA and parameter-efficient fine-tuning for open-source models.