Sub topic 3
RAG, fine-tuning, and long context: choosing between them
This chapter assumes you know how an LLM is prompted, have read the pipeline overview, and understand that a model has a fixed knowledge cutoff and a limited context window. Here you will compare three ways to give a model knowledge it lacks and learn how to pick one.
Three ways to add knowledge
- RAG retrieves relevant passages at query time and places them in the prompt.
- Fine-tuning continues training the model on your examples, so the change lives in the model's weights.
- Long context skips retrieval and puts the whole document set into one very large prompt.
Each one changes a different thing. RAG changes the input per query. Fine-tuning changes the model. Long context changes how much input you can send.
What each approach does well
| Need | RAG | Fine-tuning | Long context |
|---|---|---|---|
| Facts that change often | Re-index the changed documents | Retrain | Replace the text in the prompt |
| Citing sources | Natural, since you know which passages were used | Hard, since facts are blended into weights | Possible, but the model must locate the passage itself |
| Per-user access control | Filter before retrieval | Not possible per user | Only include what the user may see |
| Consistent tone or output format | Weak | Strong | Weak |
| Corpus larger than the window | Works | Works, but unreliable for facts | Does not fit |
| Cost per query | Moderate | Low after training | High, since every token is processed each time |
Fine-tuning is best for teaching behavior: a style, a schema, a classification task. It is a poor way to store facts. The model may learn them partially, cannot show where they came from, and may state them with false confidence.
Long context is attractive for small corpora because it needs no index. It has costs. You pay for every token on every query, latency grows with prompt length, and models can use information in the middle of a long prompt less reliably than information at the edges.
A decision flow
Combining approaches
The options are not exclusive. A common design fine-tunes a model to follow a citation format and refuse when context is missing, then feeds it retrieved passages. Another uses long context to hold many retrieved passages, so retrieval can be generous with k.
Common mistakes
- Fine-tuning to inject facts, then finding that answers are outdated or invented.
- Using RAG when the real problem is output format, which retrieval cannot fix.
- Assuming a large window means the model uses all of it well. Test with your own questions.
Exercise
A support team needs answers from a product manual that is updated weekly, with links to the source section. Which approach fits best, and why is fine-tuning a poor primary choice?
Show answer
RAG fits best. You re-index only the changed sections each week, and the retrieved passages tell you which section to cite. Fine-tuning would require retraining after each update, would store facts in weights where they can be wrong or stale, and cannot point to a source.
Exercise
You have 40 pages of internal policy that rarely changes and receive about ten questions per day. Name one reason to try long context first and one signal that would make you switch to RAG.
Show answer
Long context needs no chunking, embeddings, or index, so it is simpler to build and debug at this size. Switch to RAG if the corpus grows beyond the window, if per-query cost or latency becomes too high, or if different users need different access to documents.