The Fine-Tuning Engineer decides when fine-tuning is the right approach and then executes it correctly - from dataset curation through evaluation. The most important decision is whether to fine-tune at all: most production LLM problems are better solved with prompt engineering, few-shot examples, or RAG. Fine-tuning is warranted when you need a consistent output style, domain-specific vocabulary, or latency/cost reduction by distilling a large model's behaviour into a smaller one.
Fine-Tune vs Prompt: Decision Framework
| Signal | Recommendation |
|---|---|
| Need consistent tone/style | Fine-tune - style is hard to enforce reliably in prompts |
| Domain vocabulary (legal, medical) | Fine-tune - specialized tokens improve accuracy |
| New knowledge / facts | RAG - fine-tuning does not reliably inject facts |
| Cost/latency reduction | Fine-tune small model to match large model behaviour |
| Fewer than 1,000 examples | Few-shot prompting - not enough data to fine-tune safely |
| Output format compliance | Prompt first; fine-tune if pass rate < 90% |
Dataset Construction
# JSONL format for instruction fine-tuning (standard across most platforms)
# Each line: {"messages": [{role, content}, ...]}
import json
def build_example(instruction: str, input_text: str, output: str) -> dict:
messages = [
{"role": "system", "content": instruction},
{"role": "user", "content": input_text},
{"role": "assistant", "content": output},
]
return {"messages": messages}
# Quality rules:
# - Minimum 500 examples for style/format tasks, 5000+ for domain knowledge
# - Diversity: cover all input variations, not just the easy cases
# - Consistency: same instruction wording throughout; variation causes regression
# - No leakage: validation set examples must not appear in training set
# - Human review: spot-check 10% of generated examples before training
LoRA Configuration (HuggingFace PEFT)
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM, TrainingArguments
from trl import SFTTrainer
lora_config = LoraConfig(
r=16, # Rank - higher = more parameters, more memory
lora_alpha=32, # Scaling factor (alpha/r controls effective learning rate)
target_modules=["q_proj", "v_proj"], # Attention layers only for efficiency
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
)
training_args = TrainingArguments(
output_dir="./output",
num_train_epochs=3,
per_device_train_batch_size=4,
gradient_accumulation_steps=4, # Effective batch = 16
learning_rate=2e-4,
warmup_ratio=0.03,
lr_scheduler_type="cosine",
fp16=True,
evaluation_strategy="steps",
eval_steps=100,
save_strategy="steps",
save_steps=100,
load_best_model_at_end=True,
)
Evaluation After Fine-Tuning
- Run the same eval suite used before fine-tuning - verify pass rate improved on target tasks
- Run a general capability eval (MMLU subset) - fine-tuning must not degrade general reasoning
- Check for overfitting: training loss drops but validation loss rises → reduce epochs or increase dropout
- A/B test against the base model on real production traffic before fully switching over