Chapter 2.10 — RAG vs fine-tuning vs prompt engineering¶
🎯 Objective¶
End the false dichotomy of "fine-tune or RAG" and offer an operational criterion for choosing between prompt engineering, RAG, fine-tuning, structured outputs, policy-as-code, distillation and model routing.
🧠 Guiding principle¶
The right question is rarely "fine-tune or RAG?". The right question is:
What is missing from the model so that it does what I need reliably?
The answer to that question points to the mechanism, and the mechanism points to the technique.
| What is missing | The right mechanism |
|---|---|
| Mutable knowledge (FAQ, docs, internal data) | RAG |
| Rigid output format (JSON/Pydantic) | Structured outputs + schema |
| Critical business rules | Policy-as-code + deterministic validation |
| Consistent style / voice / format | Light fine-tuning (LoRA) or stable few-shot |
| Reducing the cost of a large model for a specific task | Distillation |
| Choosing a model by query complexity | Model routing |
| Repetitive task with a labeled dataset and tight latency | Traditional ML or fine-tuning |
| Reordering results | Reranker |
| Ensuring an action only happens with permission | Authorization (IAM/OAuth) + policy |
📏 Long context vs. RAG¶
Windows of hundreds of thousands of tokens reopened the question "why not put everything in the prompt?". The published evidence gives no silver bullet to either side:
- With enough resources, long context beats RAG on average, but costs much more per query. A router that lets the model itself decide whether to answer from the retrieved passages or ask for the whole context (Self-Route) kept long-context performance at a much lower cost.
- It depends on the task. In a benchmark with 2,326 cases and 11 models (LaRA), RAG tied with long context on single-passage location tasks and was better at identifying hallucination; long context was better at reasoning and comparison. The result varied with model size, long-text capability, context length and chunk characteristics.
- Position matters. Even in long-context models, performance drops when the relevant information sits in the middle of the context (lost in the middle). Filling the window is not the same as the model using what is in it.
- A practical cutoff rule. One engineering source suggests: below ~200k tokens, put the whole corpus in the prompt (with prompt caching) and only build RAG above that. It is a heuristic, not a law; cost per query and how often the corpus changes also weigh.
Defensible stance: treat long context and RAG as complementary, route by question type, and measure cost per success on your golden set, not just accuracy (Ch. 2.11, 6.1).
🚨 Frequent anti-patterns¶
- Fine-tuning as an updatable, auditable factual store. Training can incorporate knowledge, but does not guarantee faithful recall, updates, or citations. RAG/tools is usually the first option for mutable facts and governed access; domain adaptation and stable knowledge can justify continued pretraining or SFT with independent evaluation and regression analysis.
- Prompt as a security policy. A prompt is a probabilistic instruction. A critical policy goes in code.
- RAG without retrieval evaluation. A new index is promoted based on "intuition"; recall drops 20%; nobody notices until the incident.
- Swapping a large model for a "different" large model without first examining routing, RAG, prompt and structured outputs. There is almost always a cheaper gain before the upgrade.
🧪 Decision heuristic¶
- First of all, structured outputs + RAG + a good prompt. This solves most cases.
- Routing between small and large models when load variability justifies it. Reduces cost without affecting average quality.
- Fine-tuning when an evaluated hypothesis justifies changing weights for behavior, adaptation, or cost/latency. Compare with a suitable baseline (prompt/RAG when relevant), with data and capacity to train and maintain it. RAG need not be exhausted for unrelated problems.
- Distillation when the large model is the dominant cost, there is a well-defined specific task, and there is a budget for continuous eval of the student against the teacher.
- Multi-agent / complex agent only when workflow is already exhausted.
📚 References¶
- Li et al. — Retrieval Augmented Generation or Long-Context LLMs? A Comprehensive Study and Hybrid Approach (Self-Route; EMNLP 2024 Industry): https://arxiv.org/abs/2407.16833
- Li et al. — LaRA: Benchmarking Retrieval-Augmented Generation and Long-Context LLMs (ICML 2025): https://arxiv.org/abs/2502.09977
- Li, Cao, Ma & Sun — Long Context vs. RAG for LLMs: An Evaluation and Revisits (2024): https://arxiv.org/abs/2501.01880
- Liu et al. — Lost in the Middle: How Language Models Use Long Contexts (TACL 2023): https://arxiv.org/abs/2307.03172
- Anthropic — Introducing Contextual Retrieval (~200k-token rule of thumb; 2024): https://www.anthropic.com/news/contextual-retrieval