AI

Fine-Tuning Basics

When to train vs prompt — customize behavior, tone, or format on your examples.

Interview tip Default order: prompt engineering → RAG → fine-tune. Fine-tune for style/format or niche domain with lots of examples.

① What you must know (30 sec)

Fine-tuning continues training a base model on your labeled examples so it internalizes style, format, or domain patterns. Default path: prompt engineering → RAG → fine-tune. Fine-tune when you have hundreds of quality examples and need consistent behavior at scale — not when you need up-to-date facts (use RAG).
Analogy: Fine-tuning is hiring someone who already studied your company style guide — versus giving them the guide every morning (prompting) or a searchable wiki (RAG).

② How it works

Fine-tune pipeline
Curate JSONLUpload + trainEval holdoutDeploy model IDMonitor drift
Data — 100–10K+ high-quality input→output pairs in JSONL format
Train — provider fine-tuning job (OpenAI, Anthropic, open models on GPU)
Validate — compare fine-tuned vs base on held-out prompts
Deploy — custom model name in API calls
Maintain — re-train when product, policies, or format change
Garbage in, garbage out — 200 perfect examples beat 2000 noisy ones. Audit labels before uploading.

③ Step-by-step (hands-on)

Step 1 — Confirm fine-tune is needed

Try structured outputs, few-shot, and RAG first. Document failure cases fine-tuning must fix.

Step 2 — Curate training data

JSONL: each line messages array or prompt/completion. Cover edge cases, refusals, and diverse phrasing.

Step 3 — Hold out test set

10–20% never seen in training. Define metrics: exact match, F1, human preference, or LLM judge.

Step 4 — Run training job

Upload via API or dashboard. Note job ID, hyperparameters, and base model version.

Step 5 — Evaluate vs baseline

Same prompts on base model and fine-tuned model. Fine-tune must win clearly to justify cost.

Step 6 — Deploy and monitor

Route traffic to ft model; track quality regressions; schedule re-train quarterly or on policy change.

④ Code / config patterns

Fine-tune whenDon't fine-tune yet
Consistent output format at scaleNeed current facts → RAG
Brand tone across millions of calls< 100 quality examples
Niche classification with labelsPrompt + few-shot works on eval
Reduce prompt length/costCan solve with structured outputs
# OpenAI fine-tune data format (chat)
{"messages": [
  {"role": "system", "content": "You are support bot."},
  {"role": "user", "content": "Where is my order?"},
  {"role": "assistant", "content": "Please share order ID..."}
]}
# After training:
client.chat.completions.create(
    model="ft:gpt-4o-mini:org:custom:abc123",
    messages=[...]
)

⑤ Production & pitfalls

PitfallWhy it hurtsFix
Fine-tuning for factsModel still hallucinates datesRAG for knowledge; fine-tune for behavior
Too few examplesOverfits, poor generalization500+ diverse examples or stay with prompting
Noisy labelsLearns wrong patternsHuman review sample; inter-annotator agreement
No holdout evalFalse confidence from train setBlind test set compared to base model
Skipping version control on dataCannot reproduce modelGit LFS or DVC for JSONL datasets
Ignoring inference costCustom model may cost more per tokenCalculate TCO vs long prompts
Production tips:
  • A/B test fine-tuned vs base in production
  • Rollback to base model via feature flag
  • Document training data provenance for compliance
  • Re-train pipeline triggered on labeled data threshold

⑥ Interview / on-the-job Q&A

QuestionAnswer
Fine-tune vs RAG?Fine-tune: behavior/style. RAG: fresh external facts at query time.
How many examples?Rough guide: 100 minimum viable, 500+ for solid classification, quality > quantity.
What format?Provider-specific JSONL — chat messages or prompt/completion pairs.
How evaluate?Held-out set vs base model on same metrics; human eval for tone tasks.
When re-train?Policy change, product pivot, or measured quality drift.
Open vs API fine-tune?API fine-tune easy; open models (LoRA) need GPU ops but more control.

⑦ Tools & ecosystem

  • API fine-tune: OpenAI, Anthropic (where offered)
  • Open: Hugging Face, Axolotl, LoRA/QLoRA
  • Data: Label Studio, spreadsheets → JSONL scripts
  • Eval: promptfoo, human review panels
fine-tuningtraining-dataloraevaluationjsonl

⑧ Revision checklist

  • Prompting and RAG attempted first
  • Clear success metric defined
  • 500+ curated examples OR strong justification for fewer
  • 10–20% holdout test set locked
  • Labels audited for consistency
  • Fine-tuned model beats base on holdout
  • Training data versioned in git/DVC
  • Rollback to base model planned
  • Inference cost compared to long-prompt baseline