Recommended resources
The freely available resources this lesson will direct you to.
- RAG vs Fine-Tuning vs Prompt Engineering: Optimizing AI Modelswww.youtube.com
- Pretraining vs Fine-tuning vs In-context Learning of LLM (GPT-x) EXPLAINED | Ultimate Guide ($)www.youtube.com
- To fine-tune or not to fine-tune - AI at Metaai.meta.com
- Pre-training vs Fine-Tuning vs In-Context Learning ...www.entrypointai.com
Hello! Welcome to your next lesson in our journey through Large Language Models.
In our last session, we built a practical Retrieval-Augmented Generation (RAG) pipeline. You learned how to provide an LLM with external, up-to-date knowledge at the moment of a query, without changing the model itself. This technique, where we guide the model's response by providing information in its input context, is a powerful form of In-Context Learning (ICL).
Today, we will take a step back to understand the broader landscape of model adaptation. Our goal is to explain the difference between fine-tuning and in-context learning, the two primary methods for customizing LLMs. Understanding the distinction, including the mechanisms, trade-offs, and ideal use cases for each, is crucial for any AI engineer looking to tailor models for specific tasks.
Two Paths for Model Adaptation
When a pre-trained LLM doesn't meet your specific needs out of the box, you have two main strategies to adapt it:
- In-Context Learning (ICL): Change the input to the model to guide its output.
- Fine-Tuning: Change the model itself by updating its internal parameters (weights).
Let's explore each of these in detail.

1. In-Context Learning: Guiding the Model at Inference Time
In-context learning is the process of steering an LLM's behavior by providing it with instructions, examples, or supplementary information directly within the prompt. The key characteristic is that no model weights are updated. The model's core knowledge and parameters remain unchanged; you are simply leveraging its existing capabilities more effectively.
The simplest form of ICL is prompt engineering, which you've likely done when using tools like ChatGPT. More advanced forms include RAG, where the context is automatically retrieved from a knowledge base.
To understand the core idea, let's watch a brief explanation.
RAG vs Fine-Tuning vs Prompt Engineering: Optimizing AI Models
This video from IBM Technology explains prompt engineering, which is the essence of in-context learning. It highlights how you can change a model's output simply by changing the prompt, without any backend changes.
Watch the segment from 08:44 to 11:33. Focus on how a well-engineered prompt 'activates' existing capabilities of the model without adding new knowledge or requiring additional training.
As the video explains, ICL works by directing the model's attention to relevant patterns it learned during its initial training. You are providing a context that helps it generate a more accurate or better-formatted response.
Key Characteristics of In-Context Learning:
- No Training Required: It's an inference-time technique. The model's parameters are frozen.
- Flexibility & Speed: You get immediate feedback by simply changing the prompt. This makes it ideal for rapid prototyping and experimentation.
- Doesn't Teach New Skills: ICL is limited to leveraging what the model already knows. It cannot fundamentally change the model's style or teach it a new, complex skill that isn't present in its pre-training data.
- Limited by Context Window: The amount of information you can provide is constrained by the model's context window size.
2. Fine-Tuning: Teaching the Model a New Specialization
Fine-tuning is the process of taking a pre-trained model and continuing to train it on a smaller, curated, task-specific dataset. This process updates the model's weights using backpropagation, effectively "baking" new knowledge, skills, or behaviors into the model itself.
Think of pre-training as general education (learning grammar, facts, reasoning) and fine-tuning as specialized on-the-job training (learning legal jargon, a company's specific tone, or how to summarize medical records).
This next video segment explains the fine-tuning process.
RAG vs Fine-Tuning vs Prompt Engineering: Optimizing AI Models
Let's return to the IBM Technology video to understand fine-tuning. This section explains how fine-tuning uses a specialized dataset to make small adjustments to the model's weights.
Watch the segment from 05:17 to 08:44. Pay attention to how fine-tuning modifies the model's internal parameters and the types of problems it is well-suited to solve.
Fine-tuning is a powerful method for specialization. By training the model on thousands of high-quality examples (e.g., pairs of {"prompt": "question", "completion": "desired_answer"}), you adapt its internal representations to excel at that specific task.
Key Characteristics of Fine-Tuning:
- Updates Model Weights: This is a training process that modifies the model, resulting in a new model version.
- Requires a Dataset: You need a labeled dataset that is representative of the task you want the model to perform.
- Resource Intensive: It requires computational resources (GPUs), time, and expertise.
- "Bakes In" Behavior: It's excellent for instilling a specific style, tone, format, or improving reliability on a narrow task.
- Risk of Catastrophic Forgetting: When specializing a model too much, it can sometimes lose some of its general capabilities.
Head-to-Head Comparison
To make the differences crystal clear, let's turn to a textual resource that directly compares these two approaches.
Pre-training vs Fine-Tuning vs In-Context Learning of Large ...
The article "Pre-training vs Fine-Tuning vs In-Context Learning of Large ..." by Entrypoint AI provides a very clear, point-by-point comparison.
First, quickly read the definition of in-context learning under the heading "What is in-context learning of LLMs?". Then, carefully study the five bullet points that follow, which directly compare it with fine-tuning.
Based on what you've learned, here is a summary of the core differences:
| Feature | In-Context Learning (ICL) | Fine-Tuning |
|---|---|---|
| What It Modifies | The prompt given to the model at inference time. | The model's weights through an additional training phase. |
| When It Happens | Inference Time: On-the-fly, for each query. | Training Time: An offline process to create a new model. |
| Core Purpose | To provide temporary knowledge, context, or examples. | To teach a permanent new skill, style, or specialization. |
| Data Requirement | A few examples in the prompt (few-shot) or a retrieved document (RAG). | A curated, labeled dataset of hundreds or thousands of examples. |
| Resource Cost | Low upfront cost, but can increase inference cost (longer prompts). | High upfront cost (compute for training), but can lead to cheaper/faster inference. |
| Flexibility | High. Easy to change tasks by changing the prompt. | Low. The model is specialized for a specific task. |
| Permanence | Temporary. The guidance is gone after the query is complete. | Permanent. The new behavior is "baked into" the model. |
When to Use Which? A Decision Framework
Now for the most important practical question: how do you choose? The answer depends entirely on your goal.

Here’s a more detailed breakdown of when each approach shines, based on common scenarios discussed in the Meta AI blog post, "To fine-tune or not to fine-tune":
-
If you need to teach the model new, factual knowledge (especially if it changes often):
- Start with RAG (ICL). Fine-tuning is not effective for "injecting" facts. A RAG system can be updated instantly by adding new documents to its knowledge base.
-
If you need to change the model's style, tone, or output format consistently:
- Use Fine-Tuning. If you always want the model to respond in a specific JSON format or adopt the persona of a witty pirate, fine-tuning on examples of this behavior is far more reliable and efficient than putting a long instruction in every prompt.
-
If the model is consistently failing on specific, complex tasks or edge cases:
- Use Fine-Tuning. If prompt engineering isn't enough to make the model reliably follow complex instructions (e.g., a multi-step reasoning task), fine-tuning on examples of that task can teach it the desired skill.
-
If your prompts are becoming very long and complex with many examples (few-shot learning):
- Consider Fine-Tuning. This is a strong signal that the behavior you want is too complex for ICL alone. Fine-tuning can "bake in" the logic from those examples, allowing you to use shorter, simpler prompts at inference time, saving costs.
-
If you want to use a smaller, faster, cheaper model:
- Use Fine-Tuning. You can fine-tune a smaller model (like Llama 3 8B) on the outputs of a larger, more capable model (like GPT-4o). This process, called "distillation," can create a smaller model that performs almost as well on a specific task, leading to significant cost and latency reductions.
Conclusion
You now have a solid framework for understanding and choosing between the two primary methods of LLM adaptation.
Key Takeaways:
- In-Context Learning (ICL) guides the model at inference time by providing context in the prompt. It's fast, flexible, and great for providing knowledge (like in RAG), but doesn't change the model's core abilities.
- Fine-Tuning is a training process that updates a model's weights to specialize it for a task. It's powerful for teaching new skills, styles, and improving reliability, but requires data, compute, and expertise.
- The choice between them is strategic: RAG (ICL) is for knowledge, while fine-tuning is for skill and behavior. They are not mutually exclusive and are often used together for the best results.
Preview of the Next Lesson
Fine-tuning sounds resource-intensive, and traditionally it has been. However, modern techniques have made it dramatically more accessible. In our next lesson, we will explore this exciting development by discussing Parameter-Efficient Fine-Tuning (PEFT) and diving into LoRA, one of the most popular and effective PEFT methods used today.