Lesson

Optimizing Claude Skills from Subagents to Scripts

Evolve your Claude skills from flexible natural language instructions into highly efficient, scripted automations. Learn to have the AI analyze its own command history, generate optimized scripts, and refactor the original skill for faster, more reliable, and cost-effective workflows.

Access
Included
Transcript
Needs source

Building Claude skills in plain English is intuitive and allows the AI agent to use sub-agents to figure out complex tasks. However, this approach can be slow, less reliable, and costly as the agent has to reason through the steps every time.

This lesson demonstrates a powerful "self-improving" workflow. Start by defining a skill with natural language instructions. Run the skill once, allowing the agent to discover the necessary CLI commands. Then, instruct the agent to analyze its own execution history, generate optimized scripts based on those commands, and refactor the original skill to use these new, purpose-built scripts.

Workflow demonstrated in this lesson:

  • Initial Skill Creation: Create a good-morning skill with plain English steps that instructs the AI to use sub-agents for parallel tasks.
  • Test Run & Observation: Run the skill to see how the agent executes the tasks and what specific commands it uses.
  • Script Generation: Prompt the agent to analyze its own actions and create dedicated TypeScript scripts to replicate the discovered commands.
  • Skill Refactoring: Watch as the agent refactors the original SKILL.md file, replacing the abstract sub-agent instructions with a direct call to a single, efficient script.
  • Final Execution: Run the new, improved skill to see a much faster and more consistent result.

Key benefits:

  • Faster Execution: Running a single, pre-made script is much faster than spawning and coordinating sub-agents.
  • Increased Reliability: Scripts provide consistent, predictable results every time, removing the variability of AI reasoning.
  • Lower Cost: Reduces the number of tokens required for the agent to reason about the task on each run.
  • Reusable Components: The generated scripts can be used outside of the skill or in other automations.

Summary

This lesson showcases a powerful workflow for evolving Claude skills from flexible but slow natural language instructions into highly efficient, scripted automations. The process begins by creating a skill using plain English to define a multi-step task, allowing the AI to use sub-agents to figure out the necessary commands. After a test run, the AI is instructed to analyze its own execution history, create optimized TypeScript scripts based on the commands it ran, and then refactor the original skill. The final skill replaces the dynamic sub-agent logic with a direct call to a single, purpose-built script, resulting in faster execution, greater reliability, and lower operational costs.

Prompts

Good morning
@.claude/skills/good-morning/SKILL.md Please read in the scripts from the scripts directory and then create the necessary scripts that we need to make all of the commands that you've run so far so that we can refactor the markdown of this skill that we're running to use. Pre-made scripts so that you don't have to think about it so much next time.

Terminal Commands

claude
/ide

Code Snippets

The initial version of the SKILL.md file uses plain English and sub-agents to perform its task.

---
name: good-morning
description: When the user says "Good morning", run this skill to help them start their day
---

# Good Morning

1. Create 2 subagents using the Haiku model:
    - Check for high priority issues on this github repo based on subject, description, or labels
    - Check for any high priority pull requests based on subject, description, or labels
2. Triage the issues and pull requests in a structured summary.
3. Enter planning mode to help me select which to start on first through a series of questions.

After the self-improvement pass, the skill is refactored to use a dedicated script, making it much more efficient and reliable.

---
name: good-morning
description: When the user says "Good morning", run this skill to help them start their day
---

# Good Morning

1. Run the triage script to check for high-priority issues and pull requests:
    ```bash
    bun run scripts/gh-triage.ts
    ```
2. Parse the JSON output from the triage script to extract the structured data.
3. Present a formatted summary of high-priority items to the user.
4. Enter planning mode to help the user select which item to start on first through a series of questions using the AskUserQuestion tool.

## Available Scripts

The following scripts are available in the `scripts/` directory:
- `gh-list-priority-issues.ts` - Lists high-priority GitHub issues
- `gh-list-priority-prs.ts` - Lists high-priority GitHub pull requests
- `gh-triage.ts` - Combined triage of both issues and PRs with formatted output

## Planning Questions

Ask the user:
1. Which high-priority item they'd like to start with (list all found items as options)
2. What their primary focus is for today's work session (bug fixes, features, docs, etc.)