Fine-Tuning & Training

Fine-Tuning Fundamentals

Learn when and how to fine-tune LLMs — customizing models for specific tasks, domains, and output styles.

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-tune

How 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:

jsonl
{"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

python
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

python
# 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

python
# 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 LevelExamples NeededResult
Minimum10-50Noticeable style change
Good50-200Reliable task performance
Great200-1000Strong domain expertise
Excellent1000+Near-human quality

Quality Rules

  1. Consistent format — All examples should follow the same structure
  2. Diverse inputs — Cover edge cases and variations
  3. High-quality outputs — Your training data IS the model's behavior
  4. Balanced classes — Don't over-represent any category
  5. 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 cases

Hyperparameters

ParameterWhat It DoesGuidance
n_epochsHow many times to go through training dataStart with 3, increase if underfitting
learning_rate_multiplierHow much to adjust weightsLower (0.5) = conservative, Higher (2.0) = aggressive
batch_sizeExamples processed at onceLarger = 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 data

Signs of Underfitting

Training loss stays high
→ The model hasn't learned the patterns
→ Solution: More epochs, higher learning rate, more data

Cost of Fine-Tuning

ModelTraining CostInference 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:

json
{"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

ApproachEffortCostBest For
Better promptsLowLowMost tasks
Few-shot examplesLowMedium (more tokens)Format compliance
RAGMediumMediumKnowledge-heavy tasks
Fine-tuningHighHigh (upfront)Style, format, efficiency

Key Takeaways

  1. Try prompting first — Fine-tuning is a last resort, not a first step
  2. Quality > Quantity — 100 excellent examples beat 1000 mediocre ones
  3. Consistent format in training data produces consistent outputs
  4. Monitor training metrics — Watch for overfitting and underfitting
  5. 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.