evolve-agent (aa evolve agent) (Our Claude skills) (12606)
Build agents that improve their own prompts through automated iteration and testing. Use when you want to optimize agent behavior without manual prompt tuning.
Front matter
name: evolve-agent
description: Build agents that improve their own prompts through automated iteration and testing. Use when you want to optimize agent behavior without manual prompt tuning.
disable-model-invocation: trueevolve-agent Skill
Build autonomous agents that teach themselves. This skill runs your agent through multiple iterations, measures performance, and automatically refines the prompt based on failure patterns. No manual tuning required.
What It Does
The evolve-agent skill handles the full feedback loop:
- Baseline Testing — Run your agent N times against a test task, capture metrics (accuracy, cost, speed)
- Failure Analysis — Collect all failures and have Claude identify patterns
- Prompt Refinement — Generate targeted prompt improvements based on those patterns
- A/B Testing — Run the refined prompt N times, compare metrics to baseline
- Auto-Accept/Revert — If improved, save the new prompt; if worse, revert
- Iteration — Repeat the cycle until metrics plateau or improvement threshold is met
Prerequisites & Dependencies
Before using this skill, verify these dependencies are installed:
Dependency Check
- Node.js 18+:
node --version - npm 9+:
npm --version - @anthropic-ai/sdk v0.27+:
npm list @anthropic-ai/sdk(install withnpm install @anthropic-ai/sdkif missing) - ANTHROPIC_API_KEY environment variable:
echo $ANTHROPIC_API_KEY(must be set)
If any dependency is missing, install it before proceeding.
How It Works
The Core Loop
Baseline Run (N iterations)
↓
Analyze Failures
↓
Generate Refinements
↓
Test Refined Prompt (N iterations)
↓
Compare Metrics
↓
Improved? → Yes → Save & Repeat | No → Revert & ReportMetrics Tracked
Each iteration collects:
- Accuracy — Percentage of correct outputs (you define what "correct" means)
- Cost — Total API tokens used
- Speed — Average time per run (ms)
- Failures — List of failure cases with error details
Prompt Refinement Strategy
Claude analyzes failure patterns and generates refinements like:
- Clearer task framing
- Better error handling instructions
- Additional context or constraints
- Step-by-step reasoning requirements
The refinements are ADDITIVE (never destructive) — they build on your original prompt.
Workflows
Quick Start: Evolve an Existing Agent
Step 1: Define Your Agent and Task
Create a simple test file (test-agent.js):
const Anthropic = require("@anthropic-ai/sdk");
const client = new Anthropic();
// Your agent function
async function myAgent(input) {
const response = await client.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 500,
messages: [
{
role: "user",
content: `Solve this problem: ${input}. Provide your final answer in JSON format: { "answer": "...", "reasoning": "..." }`,
},
],
});
return response.content[0].text;
}
module.exports = { myAgent };Step 2: Define Your Test Task
Create a test file with success criteria:
// test-cases.js
const testCases = [
{ input: "What is 25 + 17?", expectedAnswer: "42" },
{ input: "What is 100 * 5?", expectedAnswer: "500" },
{ input: "What is 1000 / 8?", expectedAnswer: "125" },
];
function evaluateOutput(output, testCase) {
try {
const result = JSON.parse(output);
return result.answer === testCase.expectedAnswer;
} catch (e) {
return false;
}
}
module.exports = { testCases, evaluateOutput };Step 3: Run the Evolution Loop
npx evolve-agent \
--agent test-agent.js \
--task-file test-cases.js \
--iterations 3 \
--runs-per-iteration 5 \
--improvement-threshold 10This runs 3 evolution cycles, testing 5 times per iteration, accepting improvements of 10% or more.
Step 4: Review Results
The skill outputs:
evolution-log.json— Full metrics for each cycleprompts-by-cycle.txt— Evolution of prompts across cyclesfinal-prompt.txt— The best prompt found (or original if no improvement)
Advanced: Custom Metrics
Define custom success criteria:
// custom-eval.js
function evaluateOutput(output, testCase) {
const metrics = {
accuracy: output.includes(testCase.expected) ? 1 : 0,
reasoning_length: output.split(" ").length,
uses_math: output.includes("+") || output.includes("*") ? 1 : 0,
};
// Composite score: accuracy weighted 60%, other factors 40%
return (
metrics.accuracy * 0.6 +
(metrics.reasoning_length > 100 ? 1 : 0) * 0.3 +
metrics.uses_math * 0.1
);
}
module.exports = { evaluateOutput };Then reference this evaluator:
npx evolve-agent \
--agent test-agent.js \
--task-file test-cases.js \
--evaluator custom-eval.js \
--iterations 3Configuration
All options as environment variables or CLI flags:
# CLI flags
npx evolve-agent \
--agent <path> # Required: agent file
--task-file <path> # Required: test cases
--iterations 3 # Number of evolution cycles (default: 3)
--runs-per-iteration 5 # Runs per cycle (default: 5)
--improvement-threshold 10 # Min % improvement to accept (default: 10)
--output-dir ./evolution-results # Where to save outputs (default: current dir)
--model claude-3-5-sonnet-20241022 # Which Claude model (default: sonnet)
--evaluator custom-eval.js # Custom evaluator (optional)Examples
Example 1: Math Problem Solver
Agent: Solves arithmetic and algebra problems Goal: Improve accuracy and reasoning clarity
npx evolve-agent \
--agent math-solver.js \
--task-file math-test-cases.js \
--iterations 5 \
--improvement-threshold 15Output after 3 cycles:
- Baseline accuracy: 60%
- After cycle 1: 70% (+10%)
- After cycle 2: 75% (+5%)
- After cycle 3: 80% (+5%) — STOP (threshold: 15%, not met but cost rising)
The refined prompt now includes: "Show your reasoning step-by-step" and "If unsure, estimate conservatively"
Example 2: Code Generation
Agent: Writes Python functions from descriptions Goal: Reduce syntax errors and improve readability
npx evolve-agent \
--agent code-gen.js \
--task-file code-test-cases.js \
--iterations 4 \
--improvement-threshold 20The skill identifies common failure patterns (missing imports, indentation) and refines the prompt to include a checklist.
Troubleshooting
"Agent file not found"
Ensure the path is correct and the file exports an async function:
async function myAgent(input) { ... }
module.exports = { myAgent };"Evaluation function returned invalid score"
Make sure your evaluator returns a number between 0 and 1 (or a percentage 0-100). Check for exceptions during evaluation:
npx evolve-agent ... --verbose # Shows eval scores for each run"No improvement detected (reverting to baseline)"
This is normal. The agent's prompt may be near-optimal, or the test cases may be too easy. Try:
- Adding harder test cases
- Increasing
--runs-per-iterationfor more consistent metrics - Lowering
--improvement-thresholdto see small improvements
"API rate limit exceeded"
The skill backs off automatically, but you can also:
- Reduce
--runs-per-iteration(uses fewer API calls per cycle) - Reduce
--iterations(fewer total cycles) - Use a smaller model (claude-3-5-haiku is cheaper)
Reference
How to Use This Skill in Claude Code
Install this skill into ~/.claude/skills/evolve-agent/:
git clone https://github.com/<username>/evolve-agent ~/.claude/skills/evolve-agent
cd ~/.claude/skills/evolve-agent
npm installThen invoke it from Claude Code:
/evolve-agent --agent my-agent.js --task-file test-cases.js --iterations 3Or use it in a .claude/skills/ hook to automatically improve agents on a schedule.
Source: written by One Giant Leap.
Related entries
There are more like this in the members library
The Claude Code library holds 13,843 entries. Members can search all of them by meaning and read every one in full.