Guides using DSPy's BootstrapFinetune to generate training data from teacher model traces and fine-tune student weights. Use for model distillation (e.g., GPT-4 to smaller model), reducing inference costs, or matching expensive model quality with a fine-tuned small model.
Ships in dspy-api-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Guide the user through using DSPy's BootstrapFinetune optimizer to automatically generate training data from successful reasoning traces and fine-tune a language model's weights. This is the heaviest optimization DSPy offers -- it changes the model itself, not just the prompt.
Ask these three questions before generating any BootstrapFinetune code:
/ai-improving-accuracy (BootstrapFewShot or MIPROv2) — not enough successful traces for effective fine-tuning.dspy.BootstrapFinetune is an optimizer that tunes LM weights rather than prompts. It works in two phases:
The result is a version of your program backed by a fine-tuned model that has internalized the reasoning patterns from the bootstrapped traces.
Training examples ──> Run program ──> Keep passing traces ──> Fine-tune model weights
Use it when:
Do not use it when:
/ai-improving-accuracy with MIPROv2 or BootstrapFewShot insteadimport dspy
lm = dspy.LM("openai/gpt-4o-mini") # or "anthropic/claude-sonnet-4-5-20250929", etc.
dspy.configure(lm=lm)
# 1. Define your program
class Classify(dspy.Signature):
"""Classify the support ticket category."""
text: str = dspy.InputField()
category: str = dspy.OutputField()
program = dspy.ChainOfThought(Classify)
# 2. Prepare labeled data (500+ examples)
trainset = [
dspy.Example(text="Can't log in", category="auth").with_inputs("text"),
dspy.Example(text="Charge me twice", category="billing").with_inputs("text"),
# ... 500+ examples
]
# 3. Define a metric
def metric(example, prediction, trace=None):
return prediction.category.lower() == example.category.lower()
# 4. Fine-tune
optimizer = dspy.BootstrapFinetune(metric=metric, num_threads=24)
finetuned = optimizer.compile(program, trainset=trainset)
# 5. Use the fine-tuned program
result = finetuned(text="My payment failed")
print(result.category)
After compile finishes, finetuned is a copy of your program that uses the newly fine-tuned model. Every module in the program that was backed by a fine-tunable LM gets updated.
The most powerful pattern: use an expensive, high-quality model (the teacher) to generate traces, then fine-tune a cheap model (the student) on those traces. This is model distillation.
# --- Teacher: expensive model, high quality ---
teacher_lm = dspy.LM("openai/gpt-4o") # or any strong model
dspy.configure(lm=teacher_lm)
teacher = dspy.ChainOfThought(Classify)
# Optionally optimize the teacher's prompts first for even better traces
prompt_optimizer = dspy.MIPROv2(metric=metric, auto="medium")
teacher_optimized = prompt_optimizer.compile(teacher, trainset=trainset)
# --- Student: cheap model, fine-tuned on teacher's traces ---
student_lm = dspy.LM("openai/gpt-4o-mini") # or any fine-tunable model
dspy.configure(lm=student_lm)
student = dspy.ChainOfThought(Classify)
ft_optimizer = dspy.BootstrapFinetune(metric=metric, num_threads=24)
student_finetuned = ft_optimizer.compile(
student,
trainset=trainset,
teacher=teacher_optimized, # Teacher generates the traces
)
How it works with a teacher:
The student learns to mimic the teacher's reasoning at a fraction of the inference cost.
BootstrapFinetune fine-tunes whatever LM is configured when you call compile. To control which model gets fine-tuned:
# Fine-tune GPT-4o-mini
student_lm = dspy.LM("openai/gpt-4o-mini") # or any fine-tunable model
dspy.configure(lm=student_lm)
finetuned = optimizer.compile(student, trainset=trainset)
# Fine-tune an open-source model via Together AI
student_lm = dspy.LM("together_ai/meta-llama/Llama-3-70b-chat-hf") # or any fine-tunable model
dspy.configure(lm=student_lm)
finetuned = optimizer.compile(student, trainset=trainset)
The model must support fine-tuning through its provider's API. Common options:
| Provider | Fine-tunable models | Notes |
|---|---|---|
| OpenAI | gpt-4o-mini, gpt-4o | Easiest setup, DSPy handles the API calls |
| Together AI | Llama, Mistral, etc. | Open-source models, competitive pricing |
| Local | Any HuggingFace model | Full control, needs GPU(s) |
dspy.BootstrapFinetune(
metric=None, # Scoring function: (example, prediction, trace) -> bool/float
multitask=True, # Share training data across predictors
train_kwargs=None, # Fine-tuning hyperparams (e.g., {"n_epochs": 2})
adapter=None, # Adapter for formatting training data (usually leave as None)
exclude_demos=False, # Clear few-shot demos after fine-tuning
num_threads=None, # Parallel threads for bootstrapping
)
| Parameter | Type | Default | Description |
|---|---|---|---|
metric | Callable | None | None | Scores each trace during bootstrapping. Only passing traces become training data. |
multitask | bool | True | When True, shares training data across predictors. When False, each predictor gets its own fine-tuning data. |
train_kwargs | dict | None | None | Fine-tuning hyperparameters passed to the provider (e.g., {"n_epochs": 2}). Can be LM-specific: {lm: {"n_epochs": 3}}. |
adapter | Adapter | dict[LM, Adapter] | None | None | Adapter for formatting training data. Can be per-LM. Leave as None unless you need custom formatting. |
exclude_demos | bool | False | If True, clears few-shot demos after fine-tuning (the model has internalized them). |
num_threads | int | None | None | Threads for bootstrapping. Must be >= the number of fine-tuning jobs. 24 is a good starting point. |
The compile method accepts:
optimizer.compile(
student, # Your dspy.Module to fine-tune
trainset, # List of dspy.Example with labeled data
teacher=None, # Optional: a teacher program for distillation
)
| Parameter | Type | Default | Description |
|---|---|---|---|
student | dspy.Module | required | The program whose backing LM will be fine-tuned |
trainset | list[dspy.Example] | required | Labeled training data (500+ recommended) |
teacher | dspy.Module | None | None | If provided, the teacher generates traces instead of the student. Use for distillation. |
BootstrapFinetune is the most expensive optimizer in DSPy. Budget for three cost stages:
Every training example gets run through your program (or the teacher). With 1000 examples and a ChainOfThought module, that's 1000+ LM calls just for bootstrapping.
The model provider charges for training. Costs depend on the number of successful traces and their length.
Fine-tuned models may cost slightly more per token than base models (OpenAI charges ~1.5x for fine-tuned inference). But if you distilled from GPT-4o to GPT-4o-mini, the net savings are still 10-30x.
| Factor | Prompt optimization (MIPROv2) | BootstrapFinetune |
|---|---|---|
| What it changes | Prompt instructions + few-shot examples | Model weights |
| Data needed | ~200 examples | ~500+ examples |
| Cost | Low (just LM calls for optimization) | High (LM calls + fine-tuning fees) |
| Time | Minutes | Hours |
| Quality ceiling | Good, but limited by what prompts can do | Higher -- model learns domain patterns |
| Portability | Optimized prompts work with any model | Weights are locked to one model |
| Iteration speed | Fast -- re-optimize in minutes | Slow -- re-train takes hours |
| Best for | Early development, quick iteration | Production, maximum quality, cost reduction via distillation |
Recommended progression:
dspy.BootstrapFewShot (quick, ~50 examples)dspy.MIPROv2 (better, ~200 examples)dspy.BootstrapFinetune when prompt optimization plateaus (500+ examples)dspy.BetterTogether for absolute maximum quality (combines prompt + weight optimization)# Save the fine-tuned program
finetuned.save("finetuned_classify.json")
# Load later for production
from my_module import MyProgram
production = MyProgram()
production.load("finetuned_classify.json")
result = production(text="New ticket text...")
The saved file stores the fine-tuned model identifier (e.g., ft:gpt-4o-mini-2024-07-18:org::abc123) so loading automatically points to the right model.
If only a small fraction of training examples produce passing traces, the fine-tuning data will be thin.
Fixes:
Fixes:
Fixes:
dspy.BetterTogether to combine prompt and weight optimizationBootstrapFewShot and MIPROv2 first — they are 10-100x cheaper and often close the gap enough. Fine-tune only when prompt optimization plateaus.dspy.configure(lm=student_lm) before calling compile. BootstrapFinetune fine-tunes whatever LM is configured at compile time. If the teacher LM is still configured, the optimizer fine-tunes the expensive model instead of the cheap student. Always switch to the student LM before calling compile.num_threads too low for multi-predictor programs. num_threads must be >= the number of fine-tuning jobs (one per unique LM across all predictors). If a program has 3 predictors all using the same LM, that is 1 job. If each uses a different LM, that is 3 jobs. BootstrapFinetune raises a ValueError if threads are insufficient.exclude_demos=True after fine-tuning. Once the model weights have internalized the reasoning patterns, few-shot demos in the prompt are redundant and waste tokens. Set exclude_demos=True to remove them automatically, reducing prompt length and inference cost.Install any skill:
npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
/ai-improving-accuracy/ai-fine-tuning/dspy-better-together/ai-cutting-costs/ai-do if you do not have it — it routes any AI problem to the right skill and is the fastest way to work: npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill ai-doWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
39 skills in this plugin
/dspy-adaptersUse when you need to customize how DSPy formats prompts for a specific provider — switching from chat to completion format, forcing JSON output, or debugging prompt rendering issues. Common scenarios - debugging why your prompt looks wrong when sent to the model, switching from OpenAI to Anthropic and the formatting breaks, forcing the model to return valid JSON instead of markdown, working with completion-style models that do not support chat format, customizing system messages, or handling models that choke on structured output instructions. Related - ai-switching-models, ai-following-rules, ai-parsing-data. Also used for prompt template rendering, how DSPy builds the prompt, custom system message in DSPy, JSON mode not working, model ignores format instructions, switch from chat to completion API, dspy.ChatAdapter, dspy.JSONAdapter, prompt formatting issues, debug what DSPy sends to the model, dspy.XMLAdapter, XML output format./dspy-assertionsLegacy reference for dspy.Assert and dspy.Suggest, both removed in DSPy 3.x - use dspy.Refine or dspy.BestOfN instead. Use when maintaining a legacy DSPy 2.x codebase that uses dspy.Assert or dspy.Suggest, or migrating from Assert/Suggest to DSPy 3.x. Also used for dspy.Assert, dspy.Suggest, runtime validation for LLM output, retry on bad output, backtracking on constraint violation, guard rails in DSPy, migrate from Assert, migrate from Suggest, ImportError cannot import Assert, ImportError cannot import Suggest./dspy-api-skills:dspy-bootstrap-finetunefrom dspy-api-skills
npx claudepluginhub lebsral/dspy-programming-not-prompting-lms-skills --plugin dspy-api-skillsOther plugins' skills for the same job