Key Takeaways
- RAG (Retrieval‑Augmented Generation) enhances a model’s input by pulling in external knowledge at inference time; the model’s weights stay unchanged.
- Fine‑tuning changes the model itself by updating its weights on a task‑specific dataset, shaping its behaviour (tone, format, consistency).
- RAG excels at providing up‑to‑date, traceable, and private knowledge; it does not alter the model’s inherent style or reasoning.
- Fine‑tuning is ideal for teaching a model a consistent output style, adhering to strict formats, and reducing reliance on long system prompts, but it is unreliable for injecting factual knowledge.
- The two techniques address different layers of an AI application—knowledge vs behaviour—and can be combined: fine‑tune for behaviour, use RAG for knowledge.
- In production systems (e.g., a customer‑support assistant), the most common pattern is to call a fine‑tuned model while also injecting retrieved context via RAG.
Introduction and Motivation
The author notes that, after covering many RAG‑related topics—chunking, hybrid search, reranking, contextual retrieval, and a three‑part series on evaluating retrieval quality—they have not yet explored fine‑tuning in depth. A quick online search for “RAG vs fine‑tuning” reveals a plethora of articles that frame the two as competitors, declaring one superior based on cost or performance. The author argues that this framing is “fundamentally misleading” because RAG and fine‑tuning solve different problems at different layers of an AI application. Understanding what each actually does is the prerequisite for making a sound decision.
What is RAG and How It Works
RAG, or Retrieval‑Augmented Generation, “is a technique that enhances an LLM’s response by retrieving relevant external information at inference time and injecting it into the prompt. The model itself is not changed in any way. What changes is what it sees as input.” The pipeline consists of three steps: (1) converting external documents into vector embeddings and storing them in a vector database; (2) embedding the user query and retrieving the most semantically similar chunks; and (3) passing those chunks together with the query to the LLM so it can generate a grounded response.
A minimal example using the OpenAI API illustrates the process:
python
from openai import OpenAI
import numpy as np
client = OpenAI(api_key="your_api_key")
documents = [
"pialgorithms is an AI-powered document management platform.",
"pialgorithms allows teams to search, extract, and automate document workflows.",
"pialgorithms was founded in Athens, Greece."
]
def embed(texts):
response = client.embeddings.create(model="text-embedding-3-small", input=texts)
return [r.embedding for r in response.data]
doc_embeddings = embed(documents)
query = "Where is pialgorithms based?"
query_embedding = embed([query])[0]
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
similarities = [cosine_similarity(query_embedding, doc_emb) for doc_emb in doc_embeddings]
best_match = documents[np.argmax(similarities)]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": f"Answer the user’s question using only the following context:\n\n{best_match}"},
{"role": "user", "content": query}
]
)
print(response.choices[0].message.content)
→ pialgorithms is based in Athens, Greece.
As the author points out, “the model has no idea what pialgorithms is from its training, but because we retrieved the right document chunk and injected it into the prompt, the model is able to answer accurately. The knowledge comes from outside the model, at the moment of the query, and the model itself is untouched.”
Strengths and Limitations of RAG
RAG shines in scenarios where the model needs access to information it never saw during training. It is particularly effective for:
- Answering questions about documents, knowledge bases, or data unknown to the model.
- Keeping the system up to date without retraining, since the knowledge base can be refreshed independently.
- Providing traceable, citable answers because the source chunk is known.
- Handling private or proprietary data safely, without ever embedding that data in the model.
Conversely, RAG does not modify the model’s intrinsic behaviour. “If your model tends to be verbose, RAG won’t make it more concise. If it struggles with a particular output format, RAG will not fix that.” Thus, RAG cannot teach the model a new tone, style, or reasoning pattern.
What is Fine‑Tuning and How It Works
Fine‑tuning is defined as “the process of taking a pre‑trained model and continuing to train it on a new, task‑specific dataset, updating its weights in the process. … while RAG changes the inputs of the model, fine‑tuning changes the model itself.” A base model such as GPT‑4o‑mini, already trained on a massive general corpus, undergoes an additional training loop on curated input‑output pairs. The resulting model receives a new identifier (e.g., ft:gpt-4o-mini-2024-07-18:your-org:your-suffix:abc123) and can be invoked exactly like any other model.
A concise OpenAI API example demonstrates the workflow:
python
from openai import OpenAI
import json
client = OpenAI(api_key="your_api_key")
training_examples = [
{
"messages": [
{"role": "system", "content": "You are a concise technical assistant. Always respond in one sentence."},
{"role": "user", "content": "What is a vector database?"},
{"role": "assistant", "content": "A vector database stores and retrieves data as high-dimensional numerical vectors, enabling fast semantic similarity search."}
]
},
…additional examples…
]
with open("training_data.jsonl", "w") as f:
for ex in training_examples:
f.write(json.dumps(ex) + "\n")
with open("training_data.jsonl", "rb") as f:
training_file = client.files.create(file=f, purpose="fine-tune")
fine_tune_job = client.fine_tuning.jobs.create(
training_file=training_file.id,
model="gpt-4o-mini-2024-07-18"
)
print(fine_tune_job.id) # → ft:gpt-4o-mini-2024-07-18:your-org:your-suffix:abc123
Once the job finishes, the fine‑tuned model “has already internalised the behaviour we trained it on: in our example, it will now consistently respond in one concise sentence, without us having to instruct it to do so in every system prompt.”
Strengths and Limitations of Fine‑Tuning
Fine‑tuning excels at shaping the model’s behaviour:
- Teaching a consistent output format, tone, or style (e.g., always answering in a single sentence).
- Improving performance on a narrow, specific task (e.g., generating valid JSON, summarising in three bullet points).
- Reducing the need for lengthy, repetitive system prompts by encoding those instructions directly into the model.
- Adapting the model to domain‑specific jargon so it understands and uses the right vocabulary.
However, fine‑tuning is not a reliable method for injecting factual knowledge. The author cautions: “Unlike what one might intuitively assume, fine‑tuning a model on your company’s documents won’t make the model ‘learn’ that information and be able to answer questions about it reliably. It may indeed result in the model memorizing specific facts … but this memorization is brittle and unreliable. The most probable outcome would be a model hallucinating about topics appearing in the training examples, rather than a model accurately recalling specific details.” Consequently, fine‑tuning does not provide up‑to‑date knowledge, traceable citations, or safe handling of private data.
Complementary Nature: Different Layers
Having clarified what each technique does, the author reframes the debate: “RAG and fine‑tuning operate at different layers of an AI application. RAG operates at the knowledge layer, meaning it controls what information the model has access to. On the flip side, fine‑tuning operates at the behaviour layer, meaning it defines the way the model processes the provided information and generates responses.” Because these layers are independent, a developer can choose RAG, fine‑tuning, or both, depending on the problem at hand.
A practical decision framework emerges: fine‑tune for behaviour, use RAG for knowledge. If the model’s failure stems from not knowing something, RAG is the remedy. If the failure is due to how the model responds—its tone, format, or consistency—then fine‑tuning addresses it. When both knowledge and behaviour are deficient, combining the two yields the best outcome.
When to Use Both: Practical Example
Consider building a customer‑support assistant for a software product that must:
- Always reply in a specific brand‑consistent tone and format.
- Have accurate, up‑to‑date knowledge of the product’s documentation.
For the first requirement, fine‑tuning is ideal: train the model on examples of ideal support responses so it learns the right tone, level of detail, and output structure. For the second, RAG retrieves the most relevant documentation chunk at inference time and injects it into the prompt, grounding the answer in the source material.
The combined call looks like this:
python
fine‑tuned model for tone/format + RAG context
response = client.chat.completions.create(
model="ft:gpt-4o-mini-2024-07-18:your-org:support-style:abc123", # fine‑tuned for behaviour
messages=[
{
"role": "system",
"content": f"You are a helpful support assistant for pialgorithms. "
f"Use only the following documentation to answer:\n\n{retrieved_context}" # RAG context
},
{
"role": "user",
"content": user_question
}
]
)
Here, “fine‑tuning makes the model know how to appropriately respond, and RAG tells it what to say.” The author concludes that the question is not “fine‑tuning vs RAG” but rather “fine‑tuning and RAG complement one another and do different things.”
Conclusion and Takeaway
The author’s final reflection underscores the usefulness of shifting the conversation from “which technique is better?” to “what problem am I actually trying to solve?” RAG addresses knowledge gaps; fine‑tuning corrects behavioural shortcomings. If a model lacks both up‑to‑date information and the desired response style, employing both techniques—fine‑tuning for behaviour and RAG for knowledge—delivers a robust, production‑ready solution. This layered perspective dispels the myth of competition and guides developers toward informed, effective choices in LLM‑based applications.
RAG vs Fine-Tuning Explained: What They Actually Do and When to Use Each

