Handbooks / Generative AI & LLMs / Chapter 5

Fine-Tuning vs Prompting

7 pages · ~5 min✓ Reviewed

Builds on Evaluation and Hallucinations.

Generative AI & LLMsPage 1

The Customization Ladder

1. Three Levers

PromptingRAGFine-tuning
ChangesInstructions at inferenceDocs injected at query timeWeights
FixesTask clarityMissing or changing knowledge, citationsBehavior, style, format, domain skill
LimitContext window, model's skillsNo weight changePoor at fresh facts
StartZero cost, instantIndex + retrieverData + training

2. Which Gap?

  • Always try prompting first
  • Fine-tuning shapes how it answers, not today's facts
Mistake

Fine-tuning to add knowledge: facts go stale and get hallucinated. Use RAG for facts.

3. Interview Point

  • Fine-tune on company docs? No: weak at facts, stale, no citations. Use RAG
  • Combine RAG and fine-tuning? Domain behavior plus fresh facts
  • Failing eval? Missing facts: RAG. Wrong format: fine-tune. Else fix the prompt
Generative AI & LLMsPage 2

Choosing Between Prompt, RAG and Fine-Tune

1. Decision Order

  1. Prompt+ few-shot baseline
  2. Add RAGfacts missing or stale
  3. Fine-tunebehavior still fails, or a smaller model is needed

2. Compare

PromptRAGFine-tune
Data freshnessfixedbestpoor
Style/formatokokbest
Setup effortlowestmediumhighest
Per-query costlong promptsadds contextcan shrink prompts

3. When Each Wins

Fine-tune winsRAG wins
Strict output formatData changes often
ToneSource citations needed
High-volume classification or extractionPer-user access control
Replaces long few-shot, cuts tokensCorpus too big for context
Mistake

Fine-tuning to add fresh facts gives stale, hallucinated answers. Use RAG.

4. Interview Point

  • Weekly policy changes? RAG: update the docs, no retraining; fine-tune only for tone
  • Fine-tune beat prompting? Same held-out eval, prompt-only baseline vs tuned; compare metric, cost, latency
  • Long context replaces RAG? Only for small corpora; cost, latency, no per-user access control or citations at scale
Generative AI & LLMsPage 3

Full Fine-Tuning vs LoRA and PEFT

1. Full FT vs PEFT

  • Full FT updates all weights
  • Needs weights + gradients + Adam states: ~12-16 bytes/param
  • PEFT freezes the base, trains few extra params

2. LoRA and QLoRA

MethodTrains7B memory
Full FTall weights~100 GB+
LoRABA, r 8-64, under 1%far less
QLoRALoRA on 4-bit baseone 16-24 GB GPU
  • LoRA: frozen W + trainable low-rank BA
  • Adapters are MBs, swappable per task or customer
  • Merge into W: no extra latency
  • Full FT: full model copy per task

3. Key knobs

KnobSetting
rank rupdate capacity
alphascaling, often 2r
targetsattention q,v or all linear
LRLoRA 1e-4 to 2e-4, full FT ~1e-5
LR trap

Full-FT LR under-trains the adapter.

4. Interview Point

  • Why low rank? Updates have low intrinsic rank, so BA fits them
  • LoRA vs QLoRA? QLoRA: less memory, small quality loss, slower
  • Full FT still worth it? Big domain shift, new skills, top quality
Generative AI & LLMsPage 4

Data Needs, Costs and Distillation

1. Data

  • Quality beats quantity
  • Style/format with LoRA: 100s–1000s clean, diverse examples
  • New skills or domains: 10k+ examples
  • Instruction/response pairs or chat JSONL, same as the inference template
  • Hold out 10–20% as eval set

2. Cost

ItemNote
Trainingtokens × epochs × model size
LoRAmainly cuts memory; compute drops only modestly
Hiddenlabeling, eval, re-training per base upgrade
Hosted fine-tuneoften pricier per token than base
Merged LoRAself-hosted, no overhead
Multi-adapterone shared base

3. Distillation

  1. Teacherlarge model
  2. Studentsmall model
  3. Resultnear-teacher quality, cheaper
  • Teacher outputs: labels or soft logits
  • Check teacher license terms
  • Best on a narrow task
Remember

Eval set stays real, never teacher-generated.

Generative AI & LLMsPage 5

Choosing Model Size and Common Mistakes

1. Size and Picking

Per 1B paramsfp164-bit
Weights~2 GB~0.5 GB
  1. Strongest modelprove feasibility
  2. Step downone size
  3. Eval checkmetric vs target
  4. Keep last passsmallest sufficient
  • Bigger = better reasoning, fewer failures; costs more latency, cost, memory
  • Memory = weights + KV cache
  • Small fine-tuned model often beats a large prompted one on a narrow task

2. Common Mistakes

  • Fine-tuning to add knowledge: hallucinated, stale facts; use RAG
  • No eval set or baseline: cannot tell if it helped
  • Noisy, duplicated or test-leaking data
Over-training

Causes catastrophic forgetting and overfitting. Fix: fewer epochs, lower LR, lower rank, mix in general data.

3. Interview Point

  • Smallest sufficient model? Step down from the strongest until eval drops below target.
  • Catastrophic forgetting? Tuning erases general skills; mix in general data.
  • Detect overfitting? Train loss keeps falling while held-out eval worsens.
Generative AI & LLMsPage 6

Customization Cheat Sheet

1. Pick the Fix

GapFix
Missing or changing factsRAG
Wrong style, format or behaviorFine-tune
Unclear taskBetter prompt + few-shot
Domain behavior + fresh factsRAG + fine-tune
Order of effort
  1. 1Prompt
  2. 2RAG
  3. 3LoRA / QLoRA
  4. 4Full fine-tune

2. LoRA Defaults

  • r 8-16, alpha 2r, all linear layers
  • LR 1e-4 to 2e-4, 1-3 epochs
  • QLoRA fits a 7B in ~16 GB
python
LoraConfig(r=16, lora_alpha=32,
           target_modules="all-linear")
# lr 2e-4, num_train_epochs=2

3. Data and Cost

  • 100s-1000s clean examples for style
  • 10k+ for new skills
  • Hold out an eval set
  • Keep a prompt-only baseline
Cut cost with

Distillation to a small student, merged adapters, and the smallest model that still meets the eval bar.

Generative AI & LLMsPage 7

Check yourself

A bot is fine-tuned on policy docs. Policies change weekly and it now states old rules. Fix?
  • Use RAG: retrieve current docs at query time.
  • Fine-tuning injects facts poorly and goes stale.
LoRA rank r=8 on a 4096x4096 weight: how many params train?
  • B (4096x8) plus A (8x4096) = 65,536.
  • Full weight is 16.7M, so about 0.4%.
Train loss keeps falling, eval loss rises, general answers worsen. Diagnosis and fix?
  • Overfitting and catastrophic forgetting.
  • Fewer epochs, lower LR or rank, mix in general data.