Library

Volume 2 · RAG Architecture

Notebook
Sign in

Section 24

RAG vs. Fine-Tuning vs. Long Context

489 words · about 2 min

24.1 Decision Matrix

Factor RAG Fine-Tuning Long Context
Knowledge freshness Real-time Training-time cutoff Real-time (if included in context)
Citations/Traceability Excellent None Good
Cost per query Medium (retrieval + LLM) Low (no retrieval) High (long input = many tokens)
Setup cost Medium High Low
Access control Excellent (filter at retrieval) Impossible (baked into weights) Manual (include/exclude docs per call)
Scale (# documents) Millions+ Limited by training data Limited by context window
Hallucination risk Low (grounded) Medium Low (if answer is present in context)
Latency Higher (retrieval step) Lowest Higher (long input processing)
Implementation complexity Medium High Low

24.2 Decision Framework

                Need custom knowledge?
                      │
                ┌─────┴─────┐
               Yes          No → Use base model
                │
          Knowledge changes frequently?
                │
          ┌─────┴─────┐
         Yes          No
          │            │
         RAG     Is it about behavior/style?
                      │
                ┌─────┴─────┐
               Yes          No → RAG (still — don't fine-tune for facts)
                │
          Fine-tune (LoRA) + RAG for knowledge

24.3 When to Use Each

  • RAG: Default choice for most enterprise Q&A and knowledge systems — knowledge changes frequently, citations matter, access control matters, data volume exceeds what training could realistically encode.
  • Fine-Tuning: When you need to teach reasoning patterns, style, tone, or output format that prompting alone can’t reliably achieve.
  • Long Context: When you have a small, bounded document set (<50 documents) and need to reason across ALL of them simultaneously in a single call, and chunking/retrieval would lose necessary cross-document connections.
  • RAG + Fine-Tuning (hybrid, best practice): Fine-tune a smaller model to reason like a domain expert (teach domain-specific reasoning patterns and output format), then use RAG to supply up-to-date factual grounding. Example: fine-tune an 8B model on domain-specific reasoning examples, then RAG from the latest source-of-truth documents — giving domain expertise (fine-tuning) + factual accuracy (RAG) + cost efficiency (small model).
  • RAG + Long Context: Retrieve the top-N chunks via RAG, then stuff them into a long-context window for comprehensive cross-chunk analysis — combines RAG’s scalability with long-context’s ability to reason across many retrieved pieces at once.

Section 24 — Interview Q&A

Q: Explain the trade-offs between fine-tuning an LLM vs. using RAG. When would you choose each, and when would you use both? A: RAG is best when knowledge changes frequently (news, product catalogs, support tickets, payer rules), when citations/traceability are required (legal, compliance, healthcare), when data is too large or diverse to encode via training, or when per-user access control over visible data matters. Fine-tuning is best when you need to teach a specific style/tone/format, domain-specific reasoning patterns that prompting can’t reliably induce, when retrieval latency is unacceptable for the use case, or when you want to reduce per-query cost by replacing a large model + long prompt with a smaller specialized one. The hybrid approach — fine-tune a smaller model on domain reasoning patterns, then use RAG for factual grounding — captures the strengths of both: domain expertise from fine-tuning, up-to-date factual accuracy from RAG, and cost efficiency from using a smaller base model.