Get the FREE Ultimate OpenClaw Setup Guide →

sub-agents

npx machina-cli add skill shinpr/sub-agents-skills/sub-agents --openclaw
Files (1)
SKILL.md
5.1 KB

Sub-Agents - External CLI AI Task Delegation

Spawns external CLI AIs (claude, cursor-agent, codex, gemini) as isolated sub-agents with dedicated context.

Resources

Script Path: Use absolute path {SKILL_DIR}/scripts/run_subagent.py where {SKILL_DIR} is the directory containing this SKILL.md file.

CLI-Specific Notes

Check the corresponding reference for your environment:

Interpreting User Requests

Extract parameters from user's natural language request:

ParameterSource
--agentAgent name from user request (see selection rules below)
--promptTask instruction part (excluding agent specification)
--cwdCurrent working directory (absolute path)

Agent Selection Rules (when user doesn't specify agent name):

  1. Run --list to get available agents
  2. 0 agents: Inform user no agents available, show setup instructions (see Agent Definition Format)
  3. 1 agent: Auto-select without asking
  4. 2+ agents: Show list with descriptions, ask user to choose

Example: "Run code-reviewer on src/" → --agent code-reviewer --prompt "Review src/" --cwd $(pwd)

Important: Permission and Timeout

This script executes external CLIs that require elevated permissions.

Before first execution:

  1. Request elevated permissions via your CLI's tool parameters
  2. Set tool timeout to match --timeout argument (default: 600000ms)

For Codex CLI (most common permission issues): See references/codex.md for exact JSON parameter format.

Workflow

Step 0: Read CLI-Specific Setup (if applicable)

If you are running on Codex, read references/codex.md first.

Step 1: List Available Agents

Always list agents first to discover available definitions:

scripts/run_subagent.py --list

Output:

{"agents": [{"name": "code-reviewer", "description": "Reviews code..."}], "agents_dir": "/path/.agents"}

If agents list is empty:

  1. Create {cwd}/.agents/ directory
  2. Add agent definition file (see Agent Definition Format)
  3. Re-run --list to verify

Step 2: Execute Agent

scripts/run_subagent.py \
  --agent <name> \
  --prompt "<task>" \
  --cwd <absolute-path>

Step 3: Handle Response

Parse JSON output and check status field:

{"result": "...", "exit_code": 0, "status": "success", "cli": "claude"}

By status:

statusMeaningAction
successTask completedUse result directly
partialTimeout but has outputReview partial result, may need retry
errorExecution failedCheck error field and exit_code, fix and retry

By exit_code (when status is error):

exit_codeMeaningResolution
0Success-
124TimeoutIncrease --timeout or simplify task
127CLI not foundInstall required CLI (claude, codex, etc.)
1General errorCheck error field in response

Parameters

ParameterRequiredDescription
--list-List available agents (no other params needed)
--agentYes*Agent definition name from --list
--promptYes*Task description to delegate
--cwdYes*Working directory (absolute path)
--timeoutNoTimeout ms (default: 600000)
--cliNoForce CLI: claude, cursor-agent, codex, gemini

*Required when not using --list

Agent Definition Location

PrioritySourcePath
1Environment variable$SUB_AGENTS_DIR
2Default{cwd}/.agents/

To customize: export SUB_AGENTS_DIR=/custom/path

Agent Definition Format

Place .md files in .agents/ directory:

---
run-agent: claude
---

# Agent Name

Brief description of agent's purpose.

## Task
What this agent does.

## Output Format
How results should be structured.

Critical: The run-agent frontmatter determines which CLI executes the agent.

CLI Selection Priority

  1. --cli argument (explicit override)
  2. Agent definition run-agent frontmatter
  3. Auto-detect caller environment
  4. Default: codex

Common Mistakes

MistakeResultFix
Skip --list before executionAgent not found errorAlways run --list first
Use relative path for --cwdValidation failsUse absolute path
Ignore status field in responseUndetected errorsAlways check status before using result
Very long promptsMay exceed CLI limitsBreak into smaller tasks

Source

git clone https://github.com/shinpr/sub-agents-skills/blob/main/skills/sub-agents/SKILL.mdView on GitHub

Overview

Sub-Agents lets you run agent definitions as isolated sub-CLIs (claude, cursor-agent, codex, gemini). It uses run_subagent.py to spawn external AIs, with dedicated context, permission handling, and timeout control. You can list agents, pick one, and feed a natural language prompt for delegation.

How This Skill Works

The tool discovers agent definitions with a --list call, then executes a chosen agent using run_subagent.py with --agent, --prompt, and --cwd. Output is parsed as JSON with fields like status, exit_code, and result, guiding follow-up actions.

When to Use It

  • When a user names an agent to run
  • When you want to delegate a task to a specific sub-agent definition
  • When you need isolated contexts for external CLIs
  • When you must respect permissions and timeouts for external AIs
  • When you want to verify available agents before delegation

Quick Start

  1. Step 1: List available agents with scripts/run_subagent.py --list
  2. Step 2: Run a chosen agent with --agent, --prompt, and --cwd
  3. Step 3: Read the JSON response, handle status, and retry if needed

Best Practices

  • Always run --list before execution to see available agents
  • Ensure --agent, --prompt, and --cwd are provided and valid
  • Set --timeout to match task complexity and external CLI responsiveness
  • Parse the JSON response and check status and exit_code
  • Prefer concise, task-focused prompts tailored to the selected agent

Example Use Cases

  • Run code-reviewer on src/
  • Run data-cleaner on dataset.csv
  • Ask code-generator agent to refactor a function
  • Run test-generator agent for a module
  • Execute CI-checks via a designated agent

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers ↗