Sub topic 13
Combining prompting, RAG, and fine-tuning
This chapter assumes you can call a chat-style LLM API, read Python, and know what embeddings and a context window are. It also assumes you have an evaluation set and a baseline, as covered earlier in this tutorial.
The three approaches are not exclusive. They act on different parts of the system, so they stack. The skill is adding a layer only when your evaluation shows a gap that the current layers cannot close.
What each layer contributes
Each layer changes a different thing:
| Layer | What it changes | Typical job in a combined system |
|---|---|---|
| Prompting | The instructions and examples in the request | States the task, the output format, and the refusal rules |
| RAG | The facts available at query time | Supplies private or fresh knowledge |
| Fine-tuning | The model's weights | Makes style, format, or judgment consistent |
Prompting is always present. Even a fine-tuned, retrieval-backed system still needs a prompt to carry the question and the retrieved passages.
A common combined pattern
A support assistant might need current policy facts and a fixed house tone. Retrieval handles the facts. A fine-tuned model handles the tone. The prompt joins them.
The code below builds the prompt for this flow. The system message holds stable rules. The retrieved context and the question go in the user turn.
def combined_messages(question, passages):
context = "\n\n".join(passages)
system = (
"You are a support assistant. "
"Answer using only the context provided. "
"If the answer is not in the context, say you don't know."
)
user = "Context:\n" + context + "\n\nQuestion: " + question
return [
{"role": "system", "content": system},
{"role": "user", "content": user},
]
passages = ["Refunds are issued within 14 days of purchase."]
messages = combined_messages("How long do I have to ask for a refund?", passages)
print(messages[1]["content"])If you fine-tune, train on examples that look like this final prompt, including the context block. Otherwise the model learns a format at training time that differs from the one it sees in production.
How to add layers in order
Add layers one at a time and re-run your evaluation set after each change.
- Run the prompt-only baseline and record the score.
- If failures are caused by missing or stale facts, add RAG. Re-run the evaluation.
- If the remaining failures are about style, format, or consistency, and better prompts and examples have not fixed them, add fine-tuning. Re-run the evaluation.
- Keep a layer only if it improves the score enough to justify its cost.
This order matches cost. Prompt changes are cheapest to try and to undo. Fine-tuning is the most expensive to build and to maintain.
Common errors when combining
- Fine-tuning facts into the model. Facts baked into weights go stale and can't be cited. Keep facts in retrieval and use fine-tuning for behavior.
- Training on prompts without context. If production prompts include retrieved passages, the training examples should too.
- Changing several layers at once. You then can't tell which change helped or hurt.
- Ignoring retrieval quality. A fine-tuned model can't answer well from the wrong passages. Check what was retrieved before blaming the model.
- Never re-evaluating. Each layer ages differently. Documents change, the base model gets replaced, and prompts drift. Re-run the evaluation after each of these.
Exercise
A team's assistant gives wrong answers about this quarter's pricing, and its replies also vary in tone. Which layers would you add, in what order, and how would you decide each one is worth keeping?
Show answer
Start with a prompt-only baseline on the evaluation set. Wrong pricing is a knowledge gap, so add RAG over current pricing documents and re-run the evaluation. If tone still varies after prompt instructions and few-shot examples, add fine-tuning on examples that include the retrieved context, then re-run the evaluation. Keep each layer only if the score improves enough to justify its cost and maintenance.