RAG or fine-tuning? Decide what you are actually changing
Part 1 of 7 in the RAG in Production series: engineer-level, on Azure, mapped to Microsoft exam AI-103.
TL;DR for managers. RAG (retrieval-augmented generation) lets a model look up your documents at the moment someone asks a question. Fine-tuning retrains the model so it behaves differently. If the problem is "the AI doesn't know our policies, products or prices", you want RAG. If the problem is "it knows enough but answers in the wrong format, tone or at too high a cost", fine-tuning is worth testing. Updating RAG means re-indexing a document. Updating a fine-tuned model means another training run.
"Should we fine-tune a model on our documents?" is one of the most common questions in enterprise AI projects, and in most cases I think the answer is no. The question to ask first is what you are trying to change: what the model knows, or how it behaves.
Knowledge problems and behaviour problems
A knowledge problem: the model has never seen your expenses policy, your product catalogue or last Tuesday's price change. No amount of prompting fixes that, because the information is not in the model. RAG solves it by searching your content at question time and putting the relevant passages into the prompt.
A behaviour problem: the model has the information it needs but returns free text when your portal expects JSON, writes in the wrong register, calls tools inconsistently, or needs a large expensive model to get the task right. Microsoft's own guidance lists style and tone, output formats and schemas, tool use, and distilling a large model into a smaller one as the good cases for fine-tuning, and says to measure a baseline without fine-tuning first.
Side by side
| RAG | Fine-tuning | |
|---|---|---|
| Changes | What the model can see for this question | The model's weights, so its default behaviour |
| Fresh information | Re-index the changed document; live in minutes | New training data, new training run, new deployment |
| Citations | Yes: you know which chunks were used | No: facts in weights have no source to point to |
| Per-user permissions | Filter results by the user's groups at query time | Not possible: every user gets the same weights |
| Removing a fact (policy change, erasure request) | Delete or update the document | Retrain without it |
| Typical failure | Retrieval misses the right passage | Confident answers from stale or half-learned facts |
| Main cost | Search service, embeddings, larger prompts | Training runs, hosting the custom model, data preparation |
Why facts don't belong in weights
Fine-tuning on a few hundred policy documents can make a model sound as if it knows them. It cannot tell you which document an answer came from, it will blend versions when a policy changes, and it gives the intern the same access as the HR director. In regulated UK sectors, the permissions point alone usually settles it: security trimming is a filter in a search index and has no equivalent inside a model.
Worked example: an HR policy assistant
Take a hypothetical 2,000-person UK company that wants staff to ask HR questions in Teams.
- "How many days of holiday can I carry over?" This is knowledge. The answer is in the HR handbook and changes each year. RAG with citations.
- "Managers should only see the disciplinary procedure for their reports." This is permissions. A security filter on the search index (group IDs stored on each chunk). Fine-tuning cannot do this.
- "Answers must come back as JSON for the HR portal, in our plain-English house style." This is behaviour. Try a system prompt and structured outputs first. If that still fails at volume, or you want to move from a large model to a cheaper small one, fine-tune on a few hundred good examples.
The finished system often uses both: RAG supplies the facts, and a small fine-tuned model formats them.
What minimal RAG looks like on Azure
This is the whole pattern in about 30 lines of Python, using Azure AI Search (Microsoft's documentation now calls it Foundry IQ (Azure AI Search)) and an Azure OpenAI deployment in Microsoft Foundry. It uses keyless authentication, which is also an AI-103 exam skill.
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from azure.search.documents import SearchClient
from azure.search.documents.models import VectorizableTextQuery
from openai import AzureOpenAI
cred = DefaultAzureCredential() # keyless: managed identity or az login
search = SearchClient(SEARCH_ENDPOINT, "hr-policies", cred)
llm = AzureOpenAI(
azure_endpoint=AOAI_ENDPOINT, api_version="2024-10-21",
azure_ad_token_provider=get_bearer_token_provider(cred, "https://cognitiveservices.azure.com/.default"))
SYSTEM = ("Answer only from the sources below. Cite the source title in square brackets. "
"If the sources do not contain the answer, say you don't know.")
def answer(question: str) -> str:
hits = search.search(
search_text=question, # BM25 keyword leg
vector_queries=[VectorizableTextQuery( # vector leg (index has a vectorizer)
text=question, k_nearest_neighbors=50, fields="content_vector")],
query_type="semantic", semantic_configuration_name="default", # re-rank the top 50
top=5, select=["title", "content"])
sources = "\n\n".join(f"[{h['title']}]\n{h['content']}" for h in hits)
reply = llm.chat.completions.create(
model="gpt-4o-mini", # your deployment name
messages=[{"role": "system", "content": SYSTEM},
{"role": "user", "content": f"Sources:\n{sources}\n\nQuestion: {question}"}])
return reply.choices[0].message.content
Three details in this code matter more than they look, and the rest of this series is about them. The query runs a keyword search and a vector search together (part 3), Azure merges the two lists with Reciprocal Rank Fusion (part 4), and the semantic ranker re-orders the top 50 before we keep 5 (part 5).
And fine-tuning, for comparison
Fine-tuning data for chat models is JSONL: one complete example conversation per line. Notice that what you teach is the shape of the answer. The facts still come from retrieval at run time.
{"messages": [
{"role": "system", "content": "You write HR answers as JSON for the staff portal."},
{"role": "user", "content": "Can I carry over unused holiday?"},
{"role": "assistant", "content": "{\"answer\": \"Yes, up to 5 days ...\", \"policy\": \"HR-012\", \"tone\": \"plain\"}"}
]}
My order of operations
- Prompt engineering and structured outputs. Cheap, fast to change, and the baseline you measure everything else against.
- RAG when the model is missing knowledge, especially knowledge that changes or needs permissions.
- Fine-tuning when behaviour is still wrong after 1 and 2, or when you want a smaller model to match a larger one on one narrow task.
If someone proposes fine-tuning first, ask what the baseline score is. If there isn't one, start there.
Learning this for AI-103? Microsoft's AI-103 exam (Azure AI Apps and Agents Developer Associate) asks you to implement RAG, configure semantic, hybrid and vector search for grounding, and monitor relevance and grounding quality. Our live online AI-103 course builds these pipelines hands-on in Microsoft Foundry and Azure AI Search, on UK time, with mock assessments before you book the exam.
The RAG in Production series
- Part 1: RAG or fine-tuning? (you are here)
- Part 2: The RAG retrieval funnel
- Part 3: Stage 1: ANN + BM25 hybrid search
- Part 4: Stage 2: Reciprocal Rank Fusion
- Part 5: Stage 3: re-ranking with cross-encoders
- Part 6: Scaling: 10,000 vs 10 million documents
- Part 7: Evaluating RAG: retrieval vs generation