dspy-better-together — Claude Code skill: DSPy optimizer chains
A skill that walks you through chaining DSPy prompt and weight optimizers, scoring intermediate programs and keeping the best one.
npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a claude-codeWhat dspy-better-together does
This skill covers dspy.BetterTogether, which runs optimizers one after another, scores the programs produced along the way and hands back the strongest candidate. You pick a chain with a string such as "p -> w -> p", where p stands for a prompt optimizer and w for a weight optimizer. Names in the chain come from constructor keyword arguments, so passing mipro=... and gepa=... makes "mipro -> gepa" a valid strategy.
What you get inside: a worked code example and a table that maps strategies to situations. A plain "p -> w" pass fits a first attempt; "p -> w -> p" reworks prompts once weights have been tuned; "w -> p" suits cases where the fine-tuning data is already solid. Per-optimizer settings go through optimizer_compile_args. Leave student out of that dictionary — BetterTogether tracks the current program itself.
DSPy 3.2.1 or newer in the stable 3.2.x line is required. Every predictor needs its LM assigned by hand with student.set_lm(lm); skip that and the chain will not behave the way you expect. Keep a validation set around, or let BetterTogether hold out part of the trainset. Before adding BootstrapFinetune to a chain, confirm the model provider actually supports fine-tuning. The compiled program exposes candidate_programs with a score and strategy per candidate, plus flag_compilation_error_occurred to tell you whether a step failed before finishing.
What it helps with
- chain prompt and weight optimizers in one DSPy run
- compare intermediate programs by metric and keep the best
- re-optimize prompts after fine-tuning weights
- pass separate compile arguments to one optimizer in a chain
- detect whether a compile step failed before completion
Example prompts for the agent
- Build a
p -> w -> pchain for my DSPy program and show the best candidate - Add
optimizer_compile_argswithmax_metric_calls150 for thepstep - Check whether the provider supports fine-tuning before I add
BootstrapFinetune
How to install dspy-better-together
1Skills CLI (Vercel)
npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a claude-code2Manually: put the files in place
~/.claude/skills/dspy-better-together/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/dspy-better-together/
1Skills CLI (Vercel)
npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a codex2Manually: put the files in place
.agents/skills/dspy-better-together/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a cursor2Manually: put the files in place
.cursor/skills/dspy-better-together/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install OmidZamani/dspy-skills dspy-better-together2Manually: put the files in place
.github/skills/dspy-better-together/SKILL.md1Skills CLI (Vercel)
npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a gemini-cli2Platform CLI
gemini skills install https://github.com/OmidZamani/dspy-skills --path skills/dspy-better-togetherManually: put the files in place
.devin/skills/dspy-better-together/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about dspy-better-together
How do I install dspy-better-together in Claude Code?
Run npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a claude-code. The skill then lives at ~/.claude/skills/dspy-better-together/SKILL.md.
What does dspy-better-together do?
It shows how to sequence optimizers with dspy.BetterTogether, evaluate the programs each step produces and return the best candidate.
Which chain strategies are available?
"p -> w", "p -> w -> p" and "w -> p", plus custom chains such as "mipro -> gepa" when those are the constructor argument names.
Source text
SKILL.md · MIT
---
name: dspy-better-together
version: "1.0.0"
dspy-compatibility: "3.2.1"
tags: ["optimizer"]
requires-extras: []
description: Use for BetterTogether, prompt plus weight optimization, fine-tuning sequences, and strategy chains like p -> w -> p.
allowed-tools:
- Read
- Write
- Glob
- Grep
---
# DSPy BetterTogether
## Goal
Sequence prompt and weight optimizers, evaluate intermediate programs, and return the best candidate.
## Prerequisites
- Use DSPy `3.2.1` or later in the stable `3.2.x` series.
- Assign an LM directly to every predictor with `student.set_lm(lm)`.
- Keep a validation set, or allow `BetterTogether` to hold out part of the trainset.
- Confirm the LM provider supports fine-tuning before including `BootstrapFinetune`.
## Basic Pattern
```python
import dspy
lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)
student = dspy.ChainOfThought("question -> answer")
student.set_lm(lm)
def metric(example, pred, trace=None):
return float(example.answer.lower() == pred.answer.lower())
optimizer = dspy.BetterTogether(
metric=metric,
p=dspy.GEPA(
metric=lambda gold, pred, trace=None, pred_name=None, pred_trace=None:
dspy.Prediction(score=metric(gold, pred), feedback="Check answer correctness."),
reflection_lm=dspy.LM("openai/gpt-4o"),
auto="light",
),
w=dspy.BootstrapFinetune(metric=metric),
)
compiled = optimizer.compile(
student,
trainset=trainset,
valset=valset,
strategy="p -> w -> p",
)
```
## Strategy Choices
| Strategy | Use it when |
|----------|-------------|
| `"p -> w"` | Start with a simple prompt-then-weight pass |
| `"p -> w -> p"` | Re-optimize prompts after fine-tuning |
| `"w -> p"` | Fine-tuning data is already strong |
| Custom chains | Comparing prompt optimizers or conducting controlled experiments |
Optimizer names come from constructor keyword arguments. For example, `mipro=...` and `gepa=...` make `"mipro -> gepa"` valid.
## Per-Optimizer Compile Arguments
Pass optimizer-specific arguments through `optimizer_compile_args`:
```python
compiled = optimizer.compile(
student,
trainset=trainset,
valset=valset,
strategy="p -> w",
optimizer_compile_args={
"p": {"max_metric_calls": 150},
},
)
```
Do not pass `student` inside `optimizer_compile_args`; `BetterTogether` manages the current program.
## Inspect Results
The returned program exposes:
- `candidate_programs`: evaluated candidates with score and strategy
- `flag_compilation_error_occurred`: whether a step failed before completion
## Related Skills
- Pick optimizers: [dspy-optimizer-selection](../dspy-optimizer-selection/SKILL.md)
- Fine-tune weights: [dspy-finetune-bootstrap](../dspy-finetune-bootstrap/SKILL.md)
- Reflect with GEPA: [dspy-gepa-reflective](../dspy-gepa-reflective/SKILL.md)
## Official Documentation
- **BetterTogether API**: https://dspy.ai/api/optimizers/BetterTogether/
- **Optimizer guide**: https://dspy.ai/learn/optimization/optimizers/
© OmidZamani · MIT · OmidZamani/dspy-skills