Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 98/ 160
05. AI System Design

RAG vs Fine-tuning

When to retrieve fresh knowledge versus bake behavior/style into weights.

IntermediateFresh Facts · Behavior3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

RAG lets the AI look things up in your documents when asked; fine-tuning teaches the AI how to behave and write. Use lookup for facts and training for habits.

Think of it like…

Open-book versus trained habits: RAG hands the model the reference book for each question, fine-tuning sends it to training so it writes in the house style and follows the procedure without being told each time. A new employee needs both.

Where you’ve seen it

A support assistant has two problems. First, answers must reflect pricing and policies that change weekly, with citations and per-customer access control: that's RAG. In a test of 300 questions about facts updated after the model's training, a RAG system answers 91% correctly versus 62% for a fine-tuned model without retrieval, and it can cite sources. Second, answers must follow a strict ticket format with a specific tone, which prompt-only reaches 93% of the time with an 800-token style guide and five 240-token examples (2,000 tokens per request). A LoRA fine-tune on 5,000 examples lifts format compliance to 99.5% and lets the team drop those 2,000 prompt tokens: at $2.50 per million input tokens that saves $0.005 a request, $500 a day at 100,000 requests (about $15,000 a month), minus the extra per-token price some providers charge for fine-tuned models and the training and evaluation effort. The final system is hybrid: a fine-tuned small model with a short prompt that reads retrieved policy chunks.

Words you’ll hear

Knowledge vs behaviour
Facts the model must know versus how it should respond and act.
Fresh facts
Information that changes or is private and so belongs in retrieval, not weights.
Parameter-efficient fine-tuning
Training small adapters (such as LoRA) instead of all weights.
Hybrid (RAFT-style)
Fine-tuning a model to use retrieved context well and ignore distractors.

Visual diagram

Loading interactive diagram

Theory & deep dive

What each method is good at

RAG excels at: fresh or frequently updated knowledge (policies, prices, inventory), private or large corpora that don't fit in a prompt or in the model, citations and auditability (show the source passage), access control (retrieve only what the user may see) and cheap updates (re-index a document, no retraining). Its limits: it needs good retrieval (chunking, hybrid search, reranking), adds latency (often 0.3 to 0.5 s) and context tokens (3,000 retrieved tokens cost about $0.0075 per request at $2.50 per million), and the model may ignore or misuse the context. Failures are traceable to retrieval or generation.

Fine-tuning excels at: consistent format and style (JSON schemas, templates, tone), specialised task skills (classification, extraction, domain-specific reasoning patterns), reliable tool calling, domain vocabulary, shorter prompts (instructions and examples absorbed into weights, which cuts per-request tokens and latency) and distilling a large model's behaviour into a small one. Its limits: it is a poor, lossy way to store facts (models memorise unevenly and hallucinate details), knowledge goes stale until you retrain, you can't cite or access-control what's in weights, it needs good labelled data and evaluation, and each model upgrade may require redoing the work. Techniques like LoRA and QLoRA make training cheap, but the data and eval effort dominate.

A quick diagnostic table

Wrong price quoted → knowledge, use RAG. Right information in the wrong format → behaviour, prompt then fine-tune. Correct but verbose and expensive 2,000-token prompt → fine-tune a smaller model. Needs to cite the source → RAG. Different answers per customer permission level → RAG with filters.

Key takeaway: Retrieve for fresh, private, cited facts; fine-tune for behaviour, format and prompt compression.

Decision process, costs and hybrids

Follow a ladder: (1) improve the prompt and examples and measure; (2) add RAG if failures are knowledge gaps; (3) consider fine-tuning if behavioural errors persist, prompts are too long or latency and cost need a smaller model. Always build an eval set that separates the two failure types (a fresh-fact subset to test knowledge, a format and tone subset to test behaviour) so you can see which method moves which metric. Estimate costs honestly: RAG's ongoing costs are indexing, retrieval infrastructure and extra tokens per request; fine-tuning's are data creation, training runs, evaluation, hosting (some providers charge a higher per-token price for custom models) and retraining when requirements or base models change.

Hybrids are common: fine-tune a model on examples where the correct answer is grounded in provided context (including some irrelevant or distracting passages), so it learns to cite, ignore noise and abstain when the context lacks the answer, often called retrieval-aware or RAFT-style tuning. Another pattern is a fine-tuned small model for classification and routing in front of a large RAG-backed model for answers. Avoid training on facts you expect to change, and avoid retrieving what the model could just do from a short instruction. Revisit the decision as base models improve: longer context windows and better instruction following keep shifting what prompting and RAG can do without training.

Prompt-compression economics

Dropping a 2,000-token style guide and examples saves 2,000 × $2.50 / 1M = $0.005 per request; at 100,000 requests a day that's $500, or $15,000 a month, before subtracting training, evaluation and any per-token premium for the fine-tuned model. If volume is 5,000 requests a day, the saving is $750 a month and the fine-tune may not pay back.

Key takeaway: Prompt first, RAG for knowledge gaps, fine-tune for behaviour at scale, and combine when both matter.

How it works, step by step

Step 1 of 4
Team

1. Baseline with prompting

Write clear instructions and examples and measure on a golden set tagged by failure type.

format 93% · fresh facts 62% (no retrieval)
Key numbers at this step
Cheapest
first step

When to use it & trade-offs

What it is

RAG (retrieval-augmented generation) gives a model knowledge at query time by retrieving relevant documents and placing them in the prompt. Fine-tuning changes the model's weights with training examples to instill behaviour: tone, format, task skills, domain vocabulary and tool-use patterns. They solve different problems. RAG is for facts that change, must be cited or must respect access control; fine-tuning is for how the model behaves, and for compressing long instructions or examples into the model. The best systems often combine them: a fine-tuned model that is good at following the format and using retrieved context.

Why it matters: Teams often reach for fine-tuning to "teach the model our documents" and get a model that confidently misremembers facts, can't cite sources and goes stale the day policies change, or they build elaborate RAG to fix a formatting problem that a small fine-tune would solve. Choosing correctly saves months: ask whether the gap is knowledge (needs retrieval) or behaviour (needs training or better prompting), and measure before committing to either.

When to reach for it

  • Support and knowledge assistants over changing documents
  • Extraction and classification with strict formats
  • Reducing prompt size and cost at high volume

When to avoid or be careful

  • Fine-tuning to inject frequently changing facts
  • Building RAG for a pure formatting problem
  • Skipping a prompt-only baseline

RAG vs fine-tuning

RAG
Best for: Fresh, private or large knowledge with citations

Strengths: Easy updates; traceable; supports access control.

Watch out: Retrieval latency and tokens; quality depends on retrieval.

Fine-tuning
Best for: Style, format, task skills and shorter prompts

Strengths: Consistent behaviour; smaller prompts and models.

Watch out: Poor for facts; stale until retrained; needs data and evals.

Hybrid
Best for: Products needing both grounded facts and reliable behaviour

Strengths: Best of both; better use of context.

Watch out: More complexity to build and evaluate.

Interview signals & numbers to know

When this topic comes up

  • “Should we fine-tune on our docs?”
  • “Answers go stale after policy changes”
  • “The model won't follow our format”
  • LoRA and QLoRA
  • Citations and access control

Benchmarks & rules of thumb

91% vs 62% -
accuracy on fresh facts with RAG vs a fine-tune without retrieval
93% → 99.5% -
format compliance from prompt-only to fine-tuned
$500/day -
saving from removing 2,000 prompt tokens at 100,000 requests

Common mistakes & follow-ups

Common interview mistakes

  • Expecting a fine-tuned model to remember changing facts accurately.
  • Ignoring the per-token premium for hosted fine-tuned models.
  • Using a single aggregate eval that hides which failure type changed.

Likely interviewer follow-ups

  • When would you choose fine-tuning over RAG?
  • Can you combine RAG and fine-tuning?
  • How would you decide with data?
Interview tip: Frame it as knowledge versus behaviour, and show the dollar value of prompt compression. It makes the trade-off concrete.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply RAG vs Fine-tuning with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Multi-tenant Design
Next topic
Context Window Management