Skip to main content
skill CX Codex

dspy-optimize-anything — Claude Code skill: how to install

Guides AI agents through GEPA reflective evolutionary search for code, prompts, configs, and text artifacts using diagnostic feedback.

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

What dspy-optimize-anything does

This skill equips the agent with instructions for running gepa.optimize_anything in Python. It applies reflective evolutionary search to artifacts formatted as text, ranging from code kernels to system configurations. Without gepa>=0.1.1,<0.2 installed in your environment, the optimization pipeline will fail immediately on import.

Search progress relies on an evaluator that returns both a numeric score and Actionable Side Information (ASI). The underlying proposer uses error traces, runtime telemetry, or images passed through gepa.Image to plan code modifications. The framework supports single-task discovery, multi-task tuning, dataset generalization, and seedless generation.

What it helps with

  • discover algorithms and code implementations via evolutionary search
  • tune agent architectures against train and validation datasets
  • optimize vector graphics with multimodal feedback from vision models
  • generate code without a seed candidate from natural language objectives

Example prompts for the agent

  • Write a gepa.optimize_anything script to optimize a bin-packing heuristic with runtime feedback.
  • Set up an evaluator returning ASI error logs and run GEPAConfig with a limit of 100 metric calls.
  • Create a seedless optimization script to generate a string reversing function.

How to install dspy-optimize-anything

1Skills CLI (Vercel)

npx skills add OmidZamani/dspy-skills --skill dspy-optimize-anything -a claude-code

2Manually: put the files in place

~/.claude/skills/dspy-optimize-anything/SKILL.md

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

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about dspy-optimize-anything

How to install dspy-optimize-anything in Claude Code?

Run npx skills add OmidZamani/dspy-skills --skill dspy-optimize-anything -a claude-code. The command places the skill definition at ~/.claude/skills/dspy-optimize-anything/SKILL.md.

What does dspy-optimize-anything do?

It guides the agent on structuring text artifact optimization runs using the GEPA library. The model uses these instructions to write custom evaluators with diagnostic feedback and configure search engines.

What dependencies are needed to run the scripts?

You need gepa>=0.1.1,<0.2 installed in your Python environment, aligned with DSPy 3.2.1 compatibility.

Source text

SKILL.md · MIT
---
name: dspy-optimize-anything
version: "1.0.0"
dspy-compatibility: "3.2.1"
gepa-compatibility: "0.1.1"
tags: ["optimizer"]
requires-extras: ["gepa>=0.1.1,<0.2"]
description: Use for GEPA optimize_anything on text artifacts such as code, prompts, agent architectures, configs, and non-DSPy optimization targets.
allowed-tools:
  - Read
  - Write
  - Glob
  - Grep
---

# GEPA optimize_anything

## Goal

Optimize any artifact representable as text — code, prompts, agent architectures, vector graphics, configurations — using a single declarative API powered by GEPA's reflective evolutionary search.

## When to Use

- **Beyond prompt optimization** — optimizing code, configs, SVGs, scheduling policies, etc.
- **Single hard problems** — circle packing, kernel generation, algorithm discovery
- **Batch related problems** — CUDA kernels, code generation tasks with cross-transfer
- **Generalization** — agent skills, policies, or prompts that must transfer to unseen inputs
- When you can **express quality as a score** and provide **diagnostic feedback** (ASI)

## Inputs

| Input | Type | Description |
|-------|------|-------------|
| `seed_candidate` | `str \| dict[str, str] \| None` | Starting artifact text, or `None` for seedless mode |
| `evaluator` | `Callable` | Returns score (higher=better), optionally with ASI dict |
| `dataset` | `list \| None` | Training examples (for multi-task and generalization modes) |
| `valset` | `list \| None` | Validation set (for generalization mode) |
| `objective` | `str \| None` | Natural language description of what to optimize for |
| `background` | `str \| None` | Domain knowledge and constraints |
| `config` | `GEPAConfig \| None` | Engine, reflection, and tracking settings |

## Outputs

| Output | Type | Description |
|--------|------|-------------|
| `result.best_candidate` | `str \| dict` | Best optimized artifact |

## Workflow

### Phase 1: Install

```bash
pip install -U "gepa>=0.1.1,<0.2"
```

### Phase 2: Define Evaluator with ASI

The evaluator scores a candidate and returns Actionable Side Information (ASI) — diagnostic feedback that guides the LLM proposer during reflection.

**Simple evaluator (score only):**

```python
import gepa.optimize_anything as oa
from gepa.optimize_anything import EngineConfig, GEPAConfig

config = GEPAConfig(engine=EngineConfig(max_metric_calls=100))

def evaluate(candidate: str) -> float:
    score, diagnostic = run_my_system(candidate)
    oa.log(f"Error: {diagnostic}")  # captured as ASI
    return score
```

**Rich evaluator (score + structured ASI):**

```python
def evaluate(candidate: str) -> tuple[float, dict]:
    result = execute_code(candidate)
    return result.score, {
        "Error": result.stderr,
        "Output": result.stdout,
        "Runtime": f"{result.time_ms:.1f}ms",
    }
```

ASI can include open-ended text, structured data, multi-objectives (via `scores`), or images (via `gepa.Image`) for vision-capable LLMs.

### Phase 3: Choose Optimization Mode

**Mode 1 — Single-Task Search:** Solve one hard problem. No dataset needed.

```python
result = oa.optimize_anything(
    seed_candidate="<your initial artifact>",
    evaluator=evaluate,
    config=config,
)
```

**Mode 2 — Multi-Task Search:** Solve a batch of related problems with cross-transfer.

```python
result = oa.optimize_anything(
    seed_candidate="<your initial artifact>",
    evaluator=evaluate,
    dataset=tasks,
    config=config,
)
```

**Mode 3 — Generalization:** Build a skill/prompt/policy that transfers to unseen problems.

```python
result = oa.optimize_anything(
    seed_candidate="<your initial artifact>",
    evaluator=evaluate,
    dataset=train,
    valset=val,
    config=config,
)
```

**Seedless mode:** Describe what you need instead of providing a seed.

```python
result = oa.optimize_anything(
    evaluator=evaluate,
    objective="Generate a Python function `reverse()` that reverses a string.",
    config=config,
)
```

### Phase 4: Use Results

```python
print(result.best_candidate)
```

## Production Example

```python
import gepa.optimize_anything as oa
from gepa import Image
from gepa.optimize_anything import EngineConfig, GEPAConfig
import logging

logger = logging.getLogger(__name__)

# ---------- SVG optimization with VLM feedback ----------

GOAL = "a pelican riding a bicycle"
VLM = "vertex_ai/gemini-3-flash-preview"

VISUAL_ASPECTS = [
    {"id": "overall",     "criteria": f"Rate overall quality of this SVG ({GOAL}). SCORE: X/10"},
    {"id": "anatomy",     "criteria": "Rate pelican accuracy: beak, pouch, plumage. SCORE: X/10"},
    {"id": "bicycle",     "criteria": "Rate bicycle: wheels, frame, handlebars, pedals. SCORE: X/10"},
    {"id": "composition", "criteria": "Rate how convincingly the pelican rides the bicycle. SCORE: X/10"},
]

def evaluate(candidate, example):
    """Render SVG, score with a VLM, return (score, ASI)."""
    image = render_image(candidate["svg_code"])  # via cairosvg
    score, feedback = get_vlm_score_feedback(VLM, image, example["criteria"])

    return score, {
        "RenderedSVG": Image(base64_data=image, media_type="image/png"),
        "Feedback": feedback,
    }

result = oa.optimize_anything(
    seed_candidate={"svg_code": "<svg>...</svg>"},
    evaluator=evaluate,
    dataset=VISUAL_ASPECTS,
    background=f"Optimize SVG source code depicting '{GOAL}'. "
               "Improve anatomy, composition, and visual quality.",
    config=GEPAConfig(engine=EngineConfig(max_metric_calls=100)),
)

logger.info(f"Best SVG:\n{result.best_candidate['svg_code']}")


# ---------- Code optimization (single-task) ----------

def evaluate_solver(candidate: str) -> tuple[float, dict]:
    """Evaluate a Python solver for a mathematical optimization problem."""
    import subprocess, json

    proc = subprocess.run(
        ["python", "-c", candidate],
        capture_output=True, text=True, timeout=30,
    )

    if proc.returncode != 0:
        oa.log(f"Runtime error: {proc.stderr}")
        return 0.0, {"Error": proc.stderr}

    try:
        output = json.loads(proc.stdout)
        return output["score"], {
            "Output": output.get("solution"),
            "Runtime": f"{output.get('time_ms', 0):.1f}ms",
        }
    except (json.JSONDecodeError, KeyError) as e:
        oa.log(f"Parse error: {e}")
        return 0.0, {"Error": str(e), "Stdout": proc.stdout}

result = oa.optimize_anything(
    evaluator=evaluate_solver,
    objective="Write a Python solver for the bin packing problem that "
              "minimizes the number of bins. Output JSON with 'score' and 'solution'.",
    background="Use first-fit-decreasing as a starting heuristic. "
               "Higher score = fewer bins used.",
    config=GEPAConfig(engine=EngineConfig(max_metric_calls=100)),
)

print(result.best_candidate)


# ---------- Agent architecture generalization ----------

def evaluate_agent(candidate: str, example: dict) -> tuple[float, dict]:
    """Run an agent architecture on a task and score it."""
    exec_globals = {}
    exec(candidate, exec_globals)
    agent_fn = exec_globals.get("solve")

    if agent_fn is None:
        return 0.0, {"Error": "No `solve` function defined"}

    try:
        prediction = agent_fn(example["input"])
        correct = prediction == example["expected"]
        score = 1.0 if correct else 0.0
        feedback = "Correct" if correct else (
            f"Expected '{example['expected']}', got '{prediction}'"
        )
        return score, {"Prediction": prediction, "Feedback": feedback}
    except Exception as e:
        return 0.0, {"Error": str(e)}

result = oa.optimize_anything(
    seed_candidate="def solve(input):\n    return input",
    evaluator=evaluate_agent,
    dataset=train_tasks,
    valset=val_tasks,
    background="Discover a Python agent function `solve(input)` that "
               "generalizes across unseen reasoning tasks.",
    config=GEPAConfig(engine=EngineConfig(max_metric_calls=100)),
)

print(result.best_candidate)
```

## Integration with DSPy

`optimize_anything` complements DSPy's built-in optimizers. Use DSPy optimizers (GEPA, MIPROv2, BootstrapFewShot) for DSPy programs, and `optimize_anything` for arbitrary text artifacts outside DSPy:

```python
import dspy
import gepa.optimize_anything as oa
from gepa.optimize_anything import EngineConfig, GEPAConfig

# DSPy program optimization (use dspy.GEPA)
optimizer = dspy.GEPA(
    metric=gepa_metric,
    reflection_lm=dspy.LM("openai/gpt-4o"),
    auto="medium",
)
compiled = optimizer.compile(agent, trainset=trainset)

# Non-DSPy artifact optimization (use optimize_anything)
result = oa.optimize_anything(
    seed_candidate=my_config_yaml,
    evaluator=eval_config,
    background="Optimize Kubernetes scheduling policy for cost.",
    config=GEPAConfig(engine=EngineConfig(max_metric_calls=100)),
)
```

## Best Practices

1. **Rich ASI** — The more diagnostic feedback you provide, the better the proposer can reason about improvements
2. **Use `oa.log()`** — Route prints to the proposer as ASI instead of stdout
3. **Structured returns** — Return `(score, dict)` tuples for multi-faceted diagnostics
4. **Seedless for exploration** — Use `objective=` when the solution space is large and unfamiliar
5. **Background context** — Provide domain knowledge via `background=` to constrain the search
6. **Generalization mode** — Always provide `valset` when the artifact must transfer to unseen inputs
7. **Images as ASI** — Use `gepa.Image` to pass rendered outputs to vision-capable LLMs
8. **Bound every run** — Set `GEPAConfig(engine=EngineConfig(max_metric_calls=...))`

## Limitations

- Requires the `gepa` package (`pip install -U "gepa>=0.1.1,<0.2"`)
- Evaluator must be deterministic or low-variance for stable optimization
- Compute cost scales with number of candidates explored
- Single-task mode does not generalize; use mode 3 with `valset` for transfer
- Currently powered by GEPA backend; API is backend-agnostic for future strategies

## Official Documentation

- **GEPA optimize_anything guide**: https://gepa-ai.github.io/gepa/blog/introducing-optimize-anything/
- **GEPA quick start**: https://gepa-ai.github.io/gepa/guides/quickstart/
- **GEPA GitHub**: https://github.com/gepa-ai/gepa

© OmidZamani · MIT · OmidZamani/dspy-skills

Similar skills

All skills for AI and ML →

From the same set23

dspy-rag-pipeline Builds retrieval-augmented generation pipelines powered by ColBERTv2 that can be systematically optimized. Use when questions require searching across your own document corpus and grounding answers in retrieved facts. CX Codex +1 ★ 124repo stars dspy-production-deployment Prepares DSPy 3.2.1 pipelines for production by configuring safe caching, state-only JSON persistence, async calls, and output streaming. CX Codex +1 ★ 124repo stars dspy-output-refinement-constraints This skill shows how to validate DSPy outputs, refine them with dspy.Refine, and choose among candidates with dspy.BestOfN. Use it when generated results must meet format or content rules. CX Codex +1 ★ 124repo stars dspy-optimizer-selection Helps you pick a DSPy optimizer that fits your data and budget, from LabeledFewShot up to GEPA and BetterTogether. Useful when you don't know where to start compiling. CX Codex +1 ★ 124repo stars dspy-miprov2-optimizer This skill guides MIPROv2 optimization of DSPy programs, searching for instruction and few-shot demo combinations with Bayesian optimization. Use it to tune a program against training data. CX Codex +1 ★ 124repo stars dspy-mcp-tool-integration A skill that walks through wiring an MCP server into DSPy: turning its tools into dspy.Tool and calling them from an async agent. CX Codex +1 ★ 124repo stars dspy-haystack-integration Optimizes Haystack pipeline prompts using DSPy few-shot compilers. Connects Haystack retrievers into DSPy modules for data-driven prompt tuning. CX Codex +1 ★ 124repo stars dspy-gepa-reflective Guides GEPA optimization of DSPy agents using written feedback and execution traces. Useful for ReAct systems with tools and multi-step workflows. CX Codex +1 ★ 124repo stars

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