Optimizing Claude Skills from Subagents to Scripts

John Lindquist
InstructorJohn Lindquist

Social Share Links

Tweet

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.)

[00:00] I created a skill called Good Morning, and when I run Good Morning it's going to spin up two sub-agents. And this is the language to focus on because you can instruct Cloud Code to create sub-agents for you. You don't actually have to pre-define sub-agents. And the reason we're going to do this is we're going to ask it to do two tasks in parallel so that this goes faster. We'll check for issues and we'll check for pull requests.

[00:25] Then we'll summarize and then we'll work on a plan to start out our day. So opening Cloud I'll go ahead and disconnect from the IDE first so that it's not referencing our skill. Otherwise again it would know I had this file open and I need to prove that it's reading the skill based on description not based on the plugin feeding in context. So I'll say good morning, hit enter, and we're being prompted for our skill. We'll go ahead and always allow this, and now you'll see it's attempted to run two commands in parallel to get the issues and to get the pull requests.

[00:58] I'm gonna go ahead and allow this one, and allow this one, and then continue to allow them as they come through. I'm essentially allowing it to learn and come up with the best way to call these tools so we can take that information and then use it to set up scripts later. So if we scroll up a bit we'll see our summary of the issues and PRs. So it highlighted these and also just said that these are still open. And then it switched over to a planning mode and it's asking where we should start.

[01:26] Let's say let's start with the high priority issue and focus on the critical bug fixes. And then it'll go ahead and put together this plan for us on how to tackle that issue. Now this is just a mock issue that I created but I'll continue to allow all of these to run. And now it's plan is essentially saying this is a mock issue you want to continue this as a learning exercise, move to the PR, or disclose the issue, and you can see how this is progressing. But what I want to do is bring in our skill.

[01:54] So I'll select the good morning skill and I'll dictate 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. So now we'll let Clod essentially improve itself by looking at our existing patterns and realigning this skill with what we've already set up. So this allows us to create skills, run through them, and you'll see it's creating a script for listing priority issues. I'm going to allow all edits to allow it to create scripts and this is allowing us to define what we want originally in plain English and then after a test run make it much more scripted which makes it much more efficient as far as speed and cost goes. So Now it's updating our skill.

[02:46] You'll see now it's running a triage script, which is going to look at both the issues and the pull requests as mentioned here. But it also says, if you need to, you have access to only do issues or only pull requests. Then it gives you specific planning questions so that if we allow this edit, as it mentions in the final summary, we don't need sub-agents anymore. We get consistent results, it's easier to maintain, and it's essentially wins all around. So if I exit out, start up Clot again, I'll say good morning, and this time once it runs the skill it'll be much faster, much more efficient, with much more expected results.

[03:24] And I'm going to give it permission to always run the triage script, and we're back into a very similar system that we saw last time. Obviously if you don't like this planning mode or if your agents also want to grab issues from linear or other task tracking software or grab in documentation or on and on and on, you can tell it to customize and adapt that as well. But the key takeaway here is you can just spike out a skill, and this works for commands as well, that has all of the steps that you want it to achieve. You can instruct it with English to spawn sub-agents so you're not waiting for a ton of tasks to finish, and it can do them all in isolation, and then refactor those into scripts as you go along, and then essentially ask Claude to run a self-improvement pass to optimize and improve your skills.