Sub topic 13
Combining Prompting, RAG, and Fine-Tuning
What this chapter assumes
You have read the earlier chapters on diagnosing knowledge gaps versus behavior gaps, building an evaluation set, and the three levers on their own. You can read Python and call a chat-style API. You know what an embedding and a context window are.
This chapter covers how the three levers work together in one system. It also covers the mistakes that appear when they are stacked.
Why combine them
Each lever fixes a different kind of failure.
- Prompting tells the model what to do on this request.
- RAG (retrieval-augmented generation) supplies facts the model does not have.
- Fine-tuning changes how the model behaves by default.
A real task often has more than one failure. A support assistant may need current policy documents (a knowledge gap) and a strict reply format that prompts only follow 80% of the time (a behavior gap). RAG fixes the first and fine-tuning fixes the second. Neither one fixes the other's problem.
Prompting is part of every combination. Even a fine-tuned model that uses RAG still receives a prompt, and that prompt has to carry the retrieved text.
Each lever owns a layer
Treat the combined system as layers. Each layer has one job, and a problem is fixed in the layer that owns it.
| Layer | Owns | Changes when | Cost to change |
|---|---|---|---|
| Prompt | Task wording, rules, output format, examples | Requirements change | Minutes, no deployment of a model |
| Retrieval | Facts, citations, freshness | Documents change | Re-index the changed documents |
| Fine-tuned weights | Style, format habits, domain behavior | The behavior target changes | New training run and new evaluation |
The "cost to change" column explains the order of work. Put things in the cheapest layer that can hold them. A fact that changes monthly belongs in retrieval, not in the weights. A one-off rule belongs in the prompt, not in training data.
How a request flows
At request time the layers run in a fixed order. Retrieval runs first because its output goes into the prompt. The prompt is then sent to the model, which may be fine-tuned.
Fine-tuning happens before this flow, offline. The index is updated on its own schedule. The prompt template is code you can edit at any time.
A combined request in code
The code below builds the prompt for a combined system. The key design choice is that one function builds the messages for both serving and training. This keeps the two identical.
import json
SYSTEM = (
"You are a support assistant for Acme. "
"Answer using only the context. "
"If the answer is not in the context, reply: I don't know. "
"Reply in two sentences or fewer."
)
def build_user_turn(question, passages):
if passages:
context = "\n\n".join(
f"[{i}] {p}" for i, p in enumerate(passages, start=1)
)
else:
context = "(no passages found)"
return f"Context:\n{context}\n\nQuestion: {question}"
def build_messages(question, passages):
return [
{"role": "system", "content": SYSTEM},
{"role": "user", "content": build_user_turn(question, passages)},
]
def training_record(question, passages, ideal_answer):
messages = build_messages(question, passages)
messages.append({"role": "assistant", "content": ideal_answer})
return json.dumps({"messages": messages})
if __name__ == "__main__":
passages = ["Refunds are issued within 5 business days of approval."]
question = "How long do refunds take?"
print(json.dumps(build_messages(question, passages), indent=2))
print(
training_record(
question,
passages,
"Refunds are issued within 5 business days of approval [1].",
)
)Three things in this code matter.
- The same
build_messagesproduces serving prompts and training examples. The fine-tuned model sees the same prompt shape at inference that it saw in training. - The training examples contain retrieved passages. The model learns to read and use context. It does not learn to recite facts.
- The "no passages found" case is explicit. Include training examples where the correct answer is "I don't know", or the model will learn to always answer.
Add one lever at a time
Combining everything at once makes failures impossible to attribute. Build up in steps, and run your evaluation set after each step.
- Prompt only. Record the baseline score.
- Prompt plus RAG. Check whether knowledge failures dropped.
- Look at the remaining failures. If they are all behavior failures (wrong format, wrong tone, ignored rules), consider fine-tuning.
- Prompt plus RAG plus fine-tuning. Compare against step 2, not against step 1.
The comparison in step 4 is the one people skip. Fine-tuning must beat the best non-tuned system, which is often a longer or better prompt. If the gain is small, the training run and its ongoing maintenance are not worth it.
Score each layer separately where you can. For a RAG system, check retrieval on its own: for each evaluation question, was the passage that contains the answer in the top results? If retrieval fails, no amount of prompting or tuning on the generation side will help.
Common combination patterns
- Fine-tune for format, RAG for facts. The model learns a house style or a strict JSON shape. Retrieval supplies the content. This is the most common useful pairing.
- Fine-tune to use context well. Train on examples where the answer comes from the supplied passages, including refusals when the passages do not help. This makes a small model more faithful to retrieved text.
- Fine-tune a smaller model, prompt it lightly. Training moves instructions out of the prompt and into the weights. Shorter prompts reduce latency and per-request cost at high volume.
- Prompt and RAG only. Many systems need nothing more. Stopping here is a valid outcome of the decision process.
Common errors
Training facts into the weights
Fine-tuning on a document set does not reliably teach the model its facts. The model may recall them partly, answer confidently, and be wrong. The facts also go stale the day a document changes. If the failure is "the model doesn't know X", use retrieval.
Mismatch between training and serving prompts
If training examples use a short prompt and production uses a long template with retrieved passages, the model sees an input shape it was never tuned on. Behavior gains can shrink or disappear. Generate training data with the same code that builds production prompts, as shown above.
Noisy retrieval hurting the tuned model
A model tuned to trust its context will trust bad context. Irrelevant or contradictory passages produce confident wrong answers. Include some training examples with distractor passages, and measure retrieval quality separately.
Conflicting instructions across layers
The system prompt says "answer in two sentences". The tuned weights favor long answers. A retrieved document contains the text "ignore previous instructions". Decide which layer wins, keep the rules in one place where possible, and treat retrieved text as data in the prompt, not as instructions.
Evaluating only the combined system
If you only measure the end result, you cannot tell whether a regression came from the index, the prompt, or the model. Keep per-layer checks: retrieval hit rate, format validity, and answer correctness given the correct passage.
Where to look when something breaks
| Symptom | Likely layer | First check |
|---|---|---|
| Answer is out of date | Retrieval | Is the new document indexed? |
| Right passage exists but is not used | Retrieval ranking or prompt | Is it in the top results? Is it in the prompt? |
| Passage is in the prompt but ignored | Prompt or weights | Does the prompt tell the model to rely on it? |
| Output format is wrong sometimes | Weights or prompt | Does a stricter prompt fix it, or does it need tuning data? |
| Quality dropped after a model upgrade | Weights | Does the fine-tune need retraining on the new base? |
A tuned model is tied to its base model. When the provider retires the base, you must re-run training and re-evaluate. Prompt and retrieval layers usually carry over with little change. This is part of the maintenance cost you should weigh before adding fine-tuning.
Exercises
Exercise
A team fine-tuned a model on its product manual so it would "know" the product. Answers about last month's release are wrong. Which layer should have held that knowledge, and why?
Show answer
Retrieval. Release details change often, and weights are slow and costly to update. Index the manual, retrieve the relevant passages at query time, and update the index when the manual changes. Fine-tuning can still be used for tone or format.
Exercise
You fine-tune on examples with the short prompt "Summarize this ticket." In production, you send a long template with rules and retrieved passages. Format accuracy is lower than in your tests. What is the likely cause and the fix?
Show answer
The training prompts and the serving prompts differ, so the model is seeing an input shape it was not tuned on. Build training examples with the same function that builds production prompts, including retrieved passages, and retrain.
Exercise
Your evaluation shows the correct passage is in the top 3 retrieved results for 95% of questions, but the end-to-end accuracy is 70%. Mostly the model ignores the passage or answers in the wrong format. Which lever do you try next, and what do you compare the result against?
Show answer
Try prompt changes first, such as clearer rules to rely on the context and a stricter format. If failures remain and are mostly behavior failures, try fine-tuning on examples that include retrieved passages. Compare against the best prompt-plus-RAG system, not against the original baseline.