Suggested Title

FAPO: When Claude Code Learns to Optimize Its Own Pipelines


You built an LLM pipeline. Retrieval, reasoning, formatting, final answer. Six steps chained together. It scores 40% on your eval set. Now what?

You start tweaking prompts. You change a word here, add an instruction there. Run the full eval. Wait 20 minutes. Accuracy goes up 2%. Or down 3%. You try again. You inspect a few failure cases by hand. One looks like a retrieval problem. Another looks like the reasoning step ignored the evidence. A third has perfect intermediate steps but the formatter mangled the output.

This is manual prompt engineering for multi-step pipelines. It doesn't scale. And nobody really has the time for it.

Cisco Foundation AI just released FAPO — Fully Automated Prompt Optimization — to fix this. It uses Claude Code as an autonomous agent that iteratively optimizes LLM pipelines. Not just prompts. Chain parameters. Chain structure too. The code is on GitHub (81 stars, Apache 2.0). The paper is on arXiv (2606.19605). And the results are genuinely impressive.

What FAPO Actually Does

FAPO treats your LLM pipeline as an inspectable workflow. Every step gets recorded. Every intermediate output gets stored. When the pipeline produces a wrong answer, FAPO doesn't just look at the final output and guess what went wrong. It traces back through the chain to find exactly which step failed and why.

Then it fixes it.

The system runs a closed optimization loop with six stages:

  1. Evaluate. Run the pipeline on your training split. Collect per-case scores and step-level outputs.
  2. Attribute. Classify each failure by root cause. Was it the prompt? Missing evidence from upstream? A structural problem in the chain itself?
  3. Propose. Generate a new variant targeting the dominant failure cluster.
  4. Review. An independent agent checks the proposal for scope compliance, data leakage, and scorer compatibility.
  5. Compare. Run the new variant. If it beats the previous best, keep it. Otherwise, discard it.
  6. Iterate. Keep going until you hit target accuracy or run out of budget.

The key insight is the order of operations. FAPO always tries prompt edits first. Tweaks wording, adds instructions, adjusts formatting guidance. Only when prompt-level optimization plateaus and the failure attribution shows a structural bottleneck does it escalate to chain parameters or chain structure changes.

This is a big deal. Most prompt optimizers treat the pipeline as fixed. They optimize the words inside a prompt and leave the architecture alone. FAPO can rewrite the architecture.

Step-Level Failure Attribution

This is the heart of the system. When a multi-step pipeline produces a wrong answer, you need to know where it went wrong. FAPO records the output of every step in the chain, not just the final answer.

The step-attribution subagent analyzes these intermediate outputs after each evaluation run. It classifies failures into categories:

  • Prompt-addressable failures. Verbose answers. Format errors. Missing instructions. The prompt itself is the problem.
  • Upstream evidence failures. Retrieval didn't find the right documents. The reasoning step didn't use the evidence it had. The prompt is fine — the input is broken.
  • Structural failures. The chain is missing a step. There's no constraint enforcement. The retrieval step needs to be extended. No prompt change will fix this.

This classification drives the optimization strategy. If 70% of failures are prompt-addressable, FAPO focuses on prompt edits. If the dominant failure cluster is missing evidence from retrieval, no amount of prompt tweaking will help. FAPO escalates to structural changes — adding a retrieval step, introducing deterministic constraint enforcement, extending the chain.

The attribution report is the decision engine. It connects what's broken to what FAPO is allowed to fix.

The Three Optimization Levels

FAPO operates at three levels, tried in order:

Level 1: Prompt text. Rewrite the prompt. Add instructions. Change formatting guidance. Adjust tone and structure. This is where most optimizers live. FAPO exhausts this level first.

Level 2: Chain parameters. Adjust settings like temperature, retrieval depth, number of samples. Still not touching the pipeline structure. Just tuning the knobs.

Level 3: Chain structure. Add steps. Remove steps. Change the order. Introduce new nodes. This is where FAPO separates from GEPA and other prompt-only optimizers.

The escalation is governed by a scope contract. The tenant playbook defines what's allowed to change. Some tenants permit all three levels. Some restrict to prompt-only. The CTIBench security experiments, for example, were constrained to prompt edits only — and FAPO still improved accuracy by +4.0 pp on GPT-5, +7.1 pp on Foundation-Sec-8B-Instruct, and +2.0 pp on Foundation-Sec-8B-Reasoning.

Claude Code as the Orchestrator

Here's where it gets interesting. FAPO uses Claude Code as its optimization engine. Not the task model being optimized — that's separate. Claude Code is the agent that reads the playbook, writes the scope contract, proposes variants, runs evaluations, and records results.

Three core agents drive the system:

  • Optimization agent. Reads the tenant playbook, drives the loop, proposes changes.
  • Step-attribution subagent. Analyzes failures after each eval run. Uses rule-based heuristics and LLM analysis to classify failure types.
  • Variant-reviewer subagent. Independent check on every proposed change. Verifies scope compliance, placeholder integrity, data leakage prevention, and scorer compatibility.

The separation matters. The agent proposing changes is not the agent reviewing them. That's a guardrail against the optimizer going off the rails — literally. Every variant gets checked before it runs.

FAPO also provides Claude Code with custom commands and repository instructions. These tell Claude Code how to run evaluations, how to recover from failures, and how to navigate the tenant workspace. It's a full agentic system, not just a prompt template.

Tenant Architecture

FAPO organizes work around tenants. A tenant is a self-contained optimization project. It holds everything specific to one task: prompts, datasets, chain definitions, scorers, configs, tests, and optimization history.

The shared engine lives under src/hephaestus/. It handles the generic work — loading cases, rendering prompts, calling provider adapters, running LangGraph chains, validating scores, writing run artifacts. Each tenant workspace under tenants/<tenant_id>/ holds the task-specific material.

Tenants are isolated from each other. Assumptions from one task don't leak into another. This matters in enterprise environments where different business units have different requirements, different scoring methods, different deployment configurations.

Each tenant has a playbook — the most important policy document. It describes the task, the layout, and the constraints. It can override FAPO capabilities. If the playbook says "prompt edits only," FAPO respects that.

FAPO vs. GEPA: The Numbers

GEPA (Generalized Evolutionary Prompt Architecture) is the current state-of-the-art for automated prompt optimization in multi-step pipelines. It uses evolutionary search with genetic operators. FAPO builds on GEPA but expands the action space to include chain parameters and structure.

The comparison ran across six benchmarks and three task models (GPT-4.1-mini, GPT-5.4-mini, Gemma 3-12B). Claude Opus 4.6 served as both FAPO's orchestrator and GEPA's reflector model. Both systems started from identical baseline pipelines and prompts.

The results, averaged across the three task models:

Benchmark Baseline GEPA FAPO Gain
HoVer* 35.9 48.5 83.8 +35.3 pp
IFBench* 35.7 48.5 80.7 +32.2 pp
LiveBench-Math 51.0 52.6 62.0 +9.4 pp
HotpotQA 50.9 61.8 68.3 +6.5 pp
Papillon 73.6 90.7 94.9 +4.2 pp
AIME 16.7 16.0 12.9 -3.1 pp

\*HoVer and IFBench involved structural changes.

FAPO wins 15 of 18 model-benchmark comparisons. The mean gain is +14.1 pp. On the six HoVer and IFBench comparisons where FAPO escalated to structural changes, it wins all six with a mean gain of +33.8 pp.

AIME is the only benchmark where GEPA leads. The gap (-3.1 pp) falls within sampling noise — smaller than the standard deviation across stochastic trials. Not a meaningful regression.

The headline finding: structural optimization matters. The biggest gains came from escalating beyond prompts to pipeline changes when attribution identified retrieval bottlenecks or format constraint failures. No prompt rewrite fixes a missing retrieval step.

The HoVer Story

HoVer is a multi-hop fact verification benchmark. The baseline pipeline retrieves documents, extracts supporting facts, reasons over evidence, and produces a verdict. It starts at 35.9% accuracy.

GEPA optimizes the prompts. Gets to 48.5%. That's a solid 12.6 pp improvement from prompt engineering alone.

FAPO starts with prompt edits too. But when prompt optimization plateaus, the attribution report shows the dominant failure cluster is missing evidence — retrieval isn't finding the right documents for multi-hop questions. No prompt change fixes that.

So FAPO escalates. It extends the retrieval chain. Adds more retrieval steps. Introduces better evidence aggregation. The pipeline that comes out the other end hits 83.8% accuracy.

That's a 35.3 pp gain over GEPA. From a system that recognized its own prompt-only optimization was insufficient and rewrote its own architecture.

The IFBench Story

IFBench tests instruction following. The baseline scores 35.7%. GEPA gets it to 48.5% through prompt optimization.

FAPO's attribution shows failures clustering around format constraint violations. The prompts say "follow these constraints" but the model doesn't always comply. Prompt rewrites help a little. But the real fix is deterministic — enforce the constraints in code rather than asking the model to self-enforce.

FAPO adds a constraint enforcement step to the chain. Accuracy jumps to 80.7%. A +32.2 pp gain over GEPA.

This is the pattern. Prompt-only optimization hits a ceiling. Attribution identifies the bottleneck. Structural changes break through the ceiling.

Prompt-Only Still Works

On the four benchmarks where FAPO didn't escalate to structural changes (LiveBench-Math, HotpotQA, Papillon, AIME), FAPO wins 9 of 12 model-benchmark comparisons through prompt optimization alone.

This means FAPO's prompt optimization is also better than GEPA's, even when both are operating in the same prompt-only space. The win comes from the attribution loop. FAPO doesn't just generate prompt variants blindly. It looks at failure patterns, identifies the dominant cluster, and targets the prompt change at that specific failure type.

GEPA uses evolutionary search. It generates variants, evaluates them, and keeps the best. FAPO does something more surgical. It diagnoses first, then prescribes.

Model-Specific Strategies

One finding from the paper: optimal prompting approaches vary across models, even for identical tasks. The optimization agent discovers different strategies for GPT-4.1-mini, GPT-5.4-mini, and Gemma 3-12B.

This makes sense. Different models have different strengths, different failure modes, different formatting preferences. A prompt that works great for GPT-4.1-mini might not be optimal for Gemma 3-12B. FAPO discovers this automatically because it runs the attribution loop per model and adapts its strategy accordingly.

Manual prompt engineers usually write one prompt and use it across all models. FAPO writes a different optimized prompt for each.

Security Tasks

FAPO was also evaluated on CTIBench-RCM, a security task that maps CVEs (vulnerability reports) to CWEs (weakness classifications). This experiment was constrained to prompt-only optimization, following the Foundation-Sec evaluation protocol.

Results:

  • GPT-5: +4.0 pp
  • Foundation-Sec-8B-Instruct: +7.1 pp
  • Foundation-Sec-8B-Reasoning: +2.0 pp

These are smaller gains than the structural change benchmarks. But they show FAPO's prompt-only optimization generalizes to security classification tasks, not just QA and math.

Guardrails

FAPO has several built-in protections against common optimization pitfalls:

  • No test set leakage. The optimizer sees only training-split cases. Validation and test sets expose aggregate scores only.
  • Immutable variants. Every variant is a new file. Nothing gets overwritten. You can always roll back.
  • Independent review. Every proposed change goes through the variant-reviewer subagent before it runs. Checks for scope compliance, placeholder integrity, data leakage, and scorer compatibility.
  • Scope contracts. The tenant playbook defines what can and cannot change. The optimizer respects those boundaries.
  • Bounded optimization. The system has a variant budget. It stops when the budget is exhausted or target accuracy is reached.

These guardrails matter. Autonomous optimization without guardrails is a recipe for overfitting and data leakage. FAPO builds the guardrails into the system architecture.

Practical Implications

If you're building multi-step LLM pipelines, FAPO changes the optimization workflow:

Stop hand-tuning prompts. Describe your task, provide a dataset, and let FAPO iterate. The system will find failure patterns you'd miss manually and propose fixes you might not think of.

Let the pipeline evolve. Your initial chain structure is a starting point, not a constraint. FAPO can extend retrieval, add constraint enforcement, introduce new reasoning steps. The best pipeline for your task might not be the one you designed by hand.

Use attribution as a diagnostic. Even if you don't run the full optimization loop, FAPO's step-level failure attribution is valuable. Knowing that 60% of your failures come from retrieval rather than reasoning changes how you spend your engineering time.

Model-specific optimization is worth it. FAPO discovers different optimal prompts for different models. If you're deploying the same pipeline across multiple models, consider running FAPO separately for each.

Structural changes > prompt changes for some problems. The HoVer and IFBench results prove this. When your pipeline has a structural deficiency, no amount of prompt engineering will fix it. You need to change the pipeline. FAPO does this automatically.

Getting Started

The FAPO repository is at github.com/cisco-foundation-ai/fully-automated-prompt-optimization. It's Apache 2.0 licensed. You need Claude Code installed and an API key for whatever task model you want to optimize.

The fastest path is to let Claude Code scaffold a tenant for you. Describe your task in plain English. Claude Code generates the prompt template, chain definition, scorer, and config. Drop in your dataset as JSONL. Run a test evaluation. Then invoke the optimization agent.

The system uses LangGraph for pipeline representation, so you need to be comfortable with that ecosystem. The tenant structure is opinionated but flexible. You can customize scorers, chain definitions, and evaluation configs per task.

Limitations

FAPO isn't perfect. The AIME result shows it can regress on some tasks, even if within noise. The system requires Claude Code as its orchestrator, which adds cost. The tenant setup has a learning curve. And the structural changes, while powerful, are bounded by what the scope contract allows.

The paper also doesn't explore optimization of very long chains (10+ steps). Most benchmarks use 3-5 step pipelines. Whether FAPO's attribution scales to deeper chains is an open question.

And like all prompt optimization systems, FAPO is limited by the quality of the evaluation dataset. Garbage in, garbage out. If your eval set doesn't cover your real-world distribution, FAPO will optimize for the wrong thing.

The Bigger Picture

FAPO represents a shift in how we think about prompt engineering. The field has been moving from manual prompt writing to automated prompt optimization for a while. DSPy, GEPA, MIPRO, and others have pushed this forward.

FAPO takes the next step. It moves from prompt-only optimization to pipeline optimization. The insight is simple: in a multi-step pipeline, the prompt is just one component. Sometimes the problem isn't the prompt. Sometimes the problem is the pipeline.

By giving the optimizer access to the full pipeline — prompts, parameters, and structure — and by providing the attribution data to make informed decisions about what to change, FAPO achieves gains that prompt-only systems can't match.

The fact that it uses Claude Code as the orchestrator is also significant. It's a real-world demonstration of an AI agent doing useful autonomous work on a complex codebase. Not generating code snippets. Not answering questions. Running a multi-stage optimization loop with guardrails, review, and decision-making.

This is what agentic AI looks like when it's applied to a real problem with real constraints.


Citations


Word count: ~2,250

Fact-check notes:

  • FAPO wins 15 of 18 model-benchmark comparisons with +14.1 pp mean gain over GEPA — confirmed from both arXiv abstract and Cisco blog post.
  • HoVer gain: +35.3 pp, IFBench gain: +32.2 pp — confirmed from Cisco blog results table.
  • Six HoVer/IFBench comparisons with structural changes: FAPO wins all six, mean gain +33.8 pp — confirmed from arXiv abstract.
  • CTIBench-RCM results: +4.0 pp (GPT-5), +7.1 pp (Foundation-Sec-8B-Instruct), +2.0 pp (Foundation-Sec-8B-Reasoning) — confirmed from arXiv abstract.
  • GitHub: 81 stars, Apache 2.0, created 2026-04-20 — confirmed from search results.
  • Claude Code as orchestrator, Claude Opus 4.6 as orchestrator model — confirmed from paper and blog.
  • Three optimization levels (prompt text, chain parameters, chain structure) — confirmed from paper Section 3.2.
  • Three core agents (optimization, step-attribution, variant-reviewer) — confirmed from paper Section 3.1.
  • AIME result (-3.1 pp) within sampling noise — confirmed from blog post key findings.
  • Authors: Paul Kassianik, Baturay Saglam (Yale/Cisco intern), Huaibo Zhao, Blaine Nelson, Supriti Vijay, Aman Priyanshu, Amin Karbasi — confirmed from arXiv.
  • Task models: GPT-4.1-mini, GPT-5.4-mini, Gemma 3-12B — confirmed from paper and blog.