Fine-tuning vs Prompting

AI & Machine Learning Foundations beginner Building with AI 11 min read

Overview

When building AI-powered features, one of the first architectural decisions an engineer faces is: should we fine-tune a model on our specific data, or should we rely on prompting (in-context learning) to get the behavior we need? This choice has cascading implications for cost, latency, maintainability, and the quality of your production system.

Fine-tuning permanently adjusts a model’s internal weights by training it on task-specific data, creating a specialized version of the model. Prompting, on the other hand, provides instructions and examples in the input context at inference time, leveraging the model’s existing capabilities without changing any parameters. Both approaches have legitimate use cases, and modern production systems often combine them.

This decision is not purely technical — it involves tradeoffs around team expertise, data availability, iteration speed, and infrastructure budget. Understanding these tradeoffs is critical for AI engineers making build-vs-configure decisions in real systems.

How It Works

Prompting (In-Context Learning)

Prompting relies entirely on the content of the input context window to shape model behavior. The model’s weights are never modified. Instead, you provide:

// Example: Prompting for sentiment classification
System: You are a sentiment classifier. Respond with exactly
one word: "positive", "negative", or "neutral".

Examples:
Input: "This hotel was absolutely wonderful!"
Output: positive

Input: "The room was dirty and the staff was rude."
Output: negative

Input: "Classify the following review:"
User: "The location was great but the breakfast was mediocre."
In-Context Learning

The model adapts its behavior based solely on the tokens in the current context window. No gradient updates occur. The model is “learning” from the examples and instructions you provide at inference time, but this learning is ephemeral — it vanishes once the conversation ends.

Fine-tuning

Fine-tuning permanently modifies the model’s weights through additional training on task-specific data. The model learns patterns, formats, and behaviors that become part of its parameters.

There are several approaches to fine-tuning, each with different compute and memory requirements:

# LoRA conceptual model:
# Original: output = W * input
# LoRA:     output = (W + A @ B) * input
# Where W is frozen (billions of params)
# A is (d x r) and B is (r x d), r << d
# Only A and B are trained (thousands of params)
Catastrophic Forgetting

When a model is fine-tuned too aggressively on a narrow dataset, it can lose previously learned general knowledge. The model becomes highly specialized but performs poorly on anything outside the fine-tuning domain. LoRA and other PEFT methods mitigate this by keeping most weights frozen.

The Decision Framework

The practical rule in the industry is: exhaust prompting approaches before resorting to fine-tuning. Here is a structured decision framework:

Choose Prompting When:

Choose Fine-tuning When:

Practical Applications

The Hybrid Approach

Most production systems use both techniques together. A common pattern is:

  1. Fine-tune a model on your domain data to learn your vocabulary, output format, and baseline task behavior.
  2. Prompt the fine-tuned model at inference time with specific instructions, retrieved context (RAG), and any per-request customization.

This hybrid approach gives you the reliability of fine-tuning (consistent format, domain knowledge) plus the flexibility of prompting (dynamic context, per-request instructions).

Cost Comparison

Consider a real-world scenario: classifying 100,000 customer support tickets per month.

The fine-tuned approach is 5x cheaper and 10-30x faster, but requires upfront investment in training data curation and model training infrastructure.

RAG as a Third Option

Retrieval-Augmented Generation (RAG) represents a middle ground. Instead of fine-tuning the model on your data, you store your data externally and retrieve relevant chunks at inference time, injecting them into the prompt. RAG preserves the general model’s capabilities while grounding it in your specific knowledge base, without the cost and complexity of fine-tuning.

Decision Hierarchy

The recommended approach in production: (1) Start with prompting. (2) Add RAG if the model needs domain knowledge. (3) Fine-tune only if prompting + RAG can’t achieve the required quality, consistency, or cost targets. Each step adds complexity, so justify the escalation with measured results.

When Fine-tuning Goes Wrong

Common pitfalls to watch for:

Key Takeaways

Further Reading