Lesson 24.3 · 3 of 3 · 5 min read · Reviewed 24 Nov 2026
RAG vs long context vs fine-tuning
By the end, you can
- Compare RAG, long context and fine-tuning.
- Choose the right approach for a knowledge problem.
Three ways to give a model knowledge
| RAG | Long context | Fine-tuning | |
|---|---|---|---|
| How | Retrieve passages per question | Put whole documents in the prompt | Train on examples |
| Size of knowledge | Very large | Must fit the window | Any, but blurs facts |
| Freshness | Re-index changed docs | Swap the document | Retrain |
| Citations | Natural | Possible | Hard |
| Setup | Index, search, tuning | Simple | Data and training work |
| Cost per question | Small prompts | Large prompts; cache helps | Small prompts |
Rules of thumb
- Fits in the window and used together? Long context, with prompt cachingPrompt caching: Reusing the processed form of a repeated prompt prefix across requests, which cuts cost and latency. for repeated use.
- Too large, or only small parts are relevant per question? RAGRetrieval-augmented generation (RAG): Retrieving relevant documents at request time and adding them to the prompt so the answer is grounded in them..
- Need a new style, format or skillSkill: A folder of instructions, scripts and resources that an agent loads when relevant to a task. rather than facts? Consider fine-tuningFine-tuning: Continuing to train a model on your own examples to change its behaviour or specialise it. (chapter 29), or first try prompting and examples.
Mixing
They combine well. A support assistant might keep the core policy (short) always in context, retrieve from a large knowledge base per question, and use a model tuned for the house tone.
Long context caveats
Very long prompts cost more per call and can make details in the middle easier to miss. Put the question after the documents and ask for quotes to ground answers (chapter 59).
Key points
- RAG fetches relevant passages at question time: best for large, changing knowledge and citations.
- Long context puts whole documents in the prompt: simplest when the material fits and is used together.
- Fine-tuning changes model behaviour and style; it's a poor way to add facts that change.
Record a knowledge strategy decision
Turn the rules of thumb for RAG, long context and fine-tuning into a small function, and apply it to four real-looking cases.
You will have: A choose() function and a printed decision record with the reason for each case.
Steps
- Read the four cases: size in tokens, how often it changes, whether citations are needed, and whether the need is facts or style.
- Write choose(): style needs point to fine-tuning or prompting; material that fits and is used together points to long context; large or changing knowledge points to RAG.
- Add the per-question input token ratio between long context and RAG for the large case.
- Print each case with its choice and a one-line reason.
Done when
- The 60-page contract chooses long context.
- The 40,000-item catalogue chooses RAG.
- The house tone case chooses prompting or fine-tuning.
Open in the Playground No API key needed.
Starter code (Python)
WINDOW = 200_000
CASES = [
{"name": "60-page contract review", "tokens": 45_000, "changes": "never", "cite": True, "need": "facts", "used_together": True},
{"name": "40,000-item catalogue", "tokens": 9_000_000, "changes": "daily", "cite": True, "need": "facts", "used_together": False},
{"name": "House tone for replies", "tokens": 0, "changes": "rarely", "cite": False, "need": "style", "used_together": False},
{"name": "Short core policy", "tokens": 6_000, "changes": "monthly", "cite": True, "need": "facts", "used_together": True},
]
def choose(case):
# TODO: return (choice, reason) using the rules of thumb from the lesson
return ("?", "TODO")
for c in CASES:
print(c["name"], "->", choose(c))
Show a solution
WINDOW = 200_000
CASES = [
{"name": "60-page contract review", "tokens": 45_000, "changes": "never", "cite": True, "need": "facts", "used_together": True},
{"name": "40,000-item catalogue", "tokens": 9_000_000, "changes": "daily", "cite": True, "need": "facts", "used_together": False},
{"name": "House tone for replies", "tokens": 0, "changes": "rarely", "cite": False, "need": "style", "used_together": False},
{"name": "Short core policy", "tokens": 6_000, "changes": "monthly", "cite": True, "need": "facts", "used_together": True},
]
def choose(case):
if case["need"] == "style":
return ("prompting, then fine-tuning", "a style or format, not facts; try examples first")
if case["tokens"] <= WINDOW * 0.5 and case["used_together"]:
return ("long context", "fits the window and is used together; cache it for repeated questions")
return ("RAG", "too large or only small parts are relevant per question; re-index on change and cite passages")
print("DECISION RECORD")
for c in CASES:
choice, why = choose(c)
print(f"- {c['name']}: {choice} ({why})")
rag_tokens = 4_000
print(f"catalogue: long context would need {CASES[1]['tokens'] // rag_tokens:,}x the input of a 4,000-token RAG prompt, and does not fit")
Finished the chapter? Take the chapter check.