
Few-Shot and Prompt-Based Learning
Explore the alternative to weight-based training. Learn how 'few-shot' examples in a prompt allow models to adapt in real-time, and when this replaces the need for fine-tuning.
Few-Shot and Prompt-Based Learning: The Zero-Training Alternative
In the previous lesson, we saw how Supervised Fine-Tuning (SFT) can bake behaviors into a model's weights. But what if you don't have the time to train? Or the GPUs? Or thousands of examples?
This is where Few-Shot Learning comes in. It is the practice of providing a small number of examples inside the prompt to steer the model towards a desired output. This is often called "In-Context Learning." It is the primary competitor to fine-tuning, and in many cases, it is the superior choice.
In this lesson, we will explore the limits of few-shot learning and the "In-Context vs. In-Weights" trade-off.
What is Few-Shot Learning?
Few-shot learning is a capability of foundation models that allows them to perform a task after seeing just a few $(x, y)$ samples in the conversation history.
- Zero-Shot: No examples provided.
- One-Shot: One example provided.
- Few-Shot: Usually 3 to 20 examples provided.
Why does it work?
LLMs are trained on trillions of tokens from the internet, which includes many patterns of "Examples -> Answer." During inference, if you provide a similar pattern, the model's self-attention mechanism "activates" the relevant knowledge and task-following behavior from its pretraining.
graph LR
A["Instructions"] --> D["The Prompt"]
B["Example 1"] --> D
B1["Example 2"] --> D
C["Specific User Input"] --> D
D --> E["Generated Answer"]
subgraph "The 'Context window' contains the training examples"
D
end
When Few-Shot Beats Fine-Tuning
In production, few-shot learning is often the "Default Winner" for several reasons:
1. Speed of Iteration
If you realize your model is making a mistake, you can fix a few-shot example in 5 seconds. In fine-tuning, you have to find the mistake in your dataset, re-run the training (hours), and re-deploy (minutes).
2. Lack of Data
If you only have 5 examples of a task, you cannot fine-tune. Fine-tuning on 5 examples will lead to massive overfitting. However, 5 examples in a prompt are perfect for few-shot learning.
3. Personalization
If your application needs to adapt to each individual user (e.g., "Write in the style of THIS specific user"), you can't fine-tune a model for every customer. But you can put three of the user's past emails into the prompt.
Implementation: Few-Shot Management with LangChain
Managing few-shot prompts manually can get messy. We use FewShotPromptTemplate to manage the logic efficiently.
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain_anthropic import ChatAnthropic
# 1. Our Examples (The 'Training Set')
examples = [
{"word": "happy", "antonym": "sad"},
{"word": "fast", "antonym": "slow"},
{"word": "bright", "antonym": "dim"},
]
# 2. The Formatter
example_formatter = PromptTemplate(
input_variables=["word", "antonym"],
template="Word: {word}\nAntonym: {antonym}"
)
# 3. The Few-Shot Template
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_formatter,
suffix="Word: {input}\nAntonym:",
input_variables=["input"]
)
# 4. Use in Production
def get_antonym(query: str):
llm = ChatAnthropic(model="claude-3-haiku-20240307")
formatted_query = few_shot_prompt.format(input=query)
# The 'formatted_query' now contains all our examples!
return llm.invoke(formatted_query).content
The Limitations: When Few-Shot Breaks
So why bother with SFT at all? Because few-shot has three major breaking points:
1. The Context Tax (Latency & Cost)
Every token in those examples costs money on every single request. If you have 20 examples that total 1,000 tokens, and you handle 10k requests a day, you are paying for 10 million tokens of training data every day.
- Fine-Tuning pays for those 1,000 tokens ONCE during training.
2. The "Recency" Bias
Models tend to value the last few examples more than the first few. If you have 50 examples in a prompt, the model might "forget" or ignore the rules set in example #1. This is the Lost-in-the-Middle problem.
3. Reliability Floor
Few-shot is "suggestive," while Fine-Tuning is "instinctive." A few-shot prompt might fail to produce valid JSON 2% of the time. For a high-stakes financial API, 2% failure is unacceptable.
Summary and Key Takeaways
- Few-Shot Learning is "In-Context" adaptation without changing model weights.
- Pros: Instant iteration, adapts to specific users, zero training cost.
- Cons: Recurring token costs, latency from large context, lower reliability than SFT for complex tasks.
- The Rule of Thumb: If you can solve it with few-shot prompting and your scale is low, don't think about fine-tuning.
In the next lesson, we will look at Transfer Learning, the bridge between these worlds, and how we leverage "General Intelligence" for "Task Specific Shifts."
Reflection Exercise
- Look at a prompt where you used "Few-Shot" examples. Calculate the cost of those example tokens over 1,000 requests.
- If those examples were removed and the model was fine-tuned on them instead, how many tokens would you save per request?
SEO Metadata & Keywords
Focus Keywords: Few-Shot Learning LLM, In-Context Learning vs Fine-Tuning, Prompt-Based Learning Examples, FewShotPromptTemplate LangChain, Token Cost Optimization prompting. Meta Description: Learn the power of few-shot and prompt-based learning. Discover how in-context examples provide a zero-training alternative to fine-tuning and when to make the switch.