Skip to main content
skill CX Codex

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.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add OmidZamani/dspy-skills --skill dspy-better-together -a claude-code
Open on GitHub

What 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 -> p chain for my DSPy program and show the best candidate
  • Add optimizer_compile_args with max_metric_calls 150 for the p step
  • 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-code

2Manually: put the files in place

~/.claude/skills/dspy-better-together/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/dspy-better-together/

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

Similar skills

All skills for AI and ML →

From the same set7

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor