Back to Skills

creating-subagents

majiayu000
Updated Today
58
9
58
View on GitHub
Metaaiapiautomationdesign

About

This skill provides a framework for creating specialized Claude Code subagents that handle domain-specific tasks with custom configurations and tools. Developers should use it when building autonomous agents for particular workflows that require separate context, restricted tools, or tailored system prompts. It covers agent definition, frontmatter options, and spawning patterns for task delegation.

Quick Install

Claude Code

Recommended
Plugin CommandRecommended
/plugin add https://github.com/majiayu000/claude-skill-registry
Git CloneAlternative
git clone https://github.com/majiayu000/claude-skill-registry.git ~/.claude/skills/creating-subagents

Copy and paste this command in Claude Code to install this skill

Documentation

Creating Subagents

Build specialized AI agents for Claude Code that handle domain-specific tasks with custom configurations, tools, and system prompts.

Quick Reference

ElementRequirement
Location.claude/agents/ (project) or ~/.claude/agents/ (user)
Filenameagent-name.md
NameLowercase, hyphens only (e.g., code-reviewer)
Required Fieldsname, description
Optional Fieldstools, model, permissionMode, skills, hooks

What Are Subagents?

Subagents are specialized AI assistants that Claude Code delegates tasks to. They provide:

  • Separate Context - Own context window, prevents main conversation pollution
  • Custom Tools - Restricted or expanded tool access
  • Specialized Prompts - Domain-specific instructions and expertise
  • Flexible Models - Can use different models (opus, sonnet, haiku)

When Claude encounters a task matching a subagent's expertise, it spawns the subagent via the Task tool, which works independently and returns results.

Built-in vs Custom Agents

Built-in Agents

Claude Code includes several built-in agents:

AgentModelModePurpose
ExploreHaikuRead-onlyFast codebase search and analysis
PlanInheritsRead-onlyResearch for plan mode
General-purposeInheritsRead/WriteComplex multi-step tasks
BashInheritsCommandsRunning terminal commands in separate context
statusline-setupSonnetConfigConfiguring status line via /statusline
Claude Code GuideHaikuRead-onlyAnswering questions about Claude Code features

When to Create Custom Agents

Create custom agents when you need:

  • Domain expertise - Security audit, database migration, API design
  • Workflow automation - Test runner, deployment, documentation
  • Tool restrictions - Read-only reviewers, sandboxed explorers
  • Team consistency - Shared agents for common workflows

Agent Definition Structure

Agents are Markdown files with YAML frontmatter:

---
name: agent-name
description: When this agent should be invoked
tools: Tool1, Tool2, Tool3
model: sonnet
permissionMode: default
skills: skill1, skill2
---

System prompt content here.

Detailed instructions for the agent's behavior,
expertise area, and approach to problem-solving.

Frontmatter Fields

FieldRequiredTypeDescription
nameYesstringUnique identifier (lowercase, hyphens)
descriptionYesstringWhen to invoke (include "PROACTIVELY" for auto-use)
toolsNostringComma-separated tool list (allowlist). Omit to inherit all tools
disallowedToolsNostringComma-separated tools to deny (denylist), removed from inherited list
modelNostringsonnet, opus, haiku, or inherit
permissionModeNostringPermission handling mode
skillsNostringSkills to load into subagent's context at startup
hooksNoobjectAgent-scoped hooks (2.1.0+)

Note on skills: The full skill content is injected into the subagent, not just made available for invocation. Subagents don't inherit skills from the parent conversation.

Model Options

ValueDescription
sonnetDefault. Balanced capability and speed
opusMost capable. Use for complex reasoning
haikuFastest. Use for simple, quick tasks
inheritUse main conversation's model

Permission Modes

Subagents inherit permission context from the main conversation but can override the mode.

ModeDescription
defaultStandard permission checking with prompts
acceptEditsAuto-accept file edits
dontAskAuto-deny permission prompts (explicitly allowed tools still work)
bypassPermissionsSkip all permission checks (use with caution)
planPlan mode (read-only exploration)

Important: If the parent uses bypassPermissions, this takes precedence and cannot be overridden by subagents.

Critical Constraint

Subagents cannot spawn other subagents. This prevents infinite nesting. Design your agents to be self-contained.

Hooks in Agent Frontmatter (2.1.0+)

Agents can define hooks scoped to their lifecycle. These hooks are:

  • Lifecycle-scoped - Only active while the agent executes
  • Auto-cleanup - Removed when the agent finishes
  • Portable - Packaged with the agent for distribution
---
name: secure-agent
description: Security-focused agent with audit logging
hooks:
  PreToolUse:
    - matcher: "Bash|Write|Edit"
      hooks:
        - type: command
          command: "$CLAUDE_PROJECT_DIR/.claude/hooks/audit-log.sh"
  Stop:
    - hooks:
        - type: command
          command: "$CLAUDE_PROJECT_DIR/.claude/hooks/agent-complete.sh"
---

Supported hook events in agent frontmatter:

  • PreToolUse - Before tool execution
  • PostToolUse - After tool completion
  • Stop - When agent finishes

Conditional Rules with Hooks

For dynamic control over tool usage, use PreToolUse hooks to validate operations. This is useful when you need to allow some operations of a tool while blocking others.

Example: Read-only database agent

---
name: db-reader
description: Execute read-only database queries
tools: Bash
hooks:
  PreToolUse:
    - matcher: "Bash"
      hooks:
        - type: command
          command: "./scripts/validate-readonly-query.sh"
---

The validation script reads JSON from stdin, extracts the command, and exits with code 2 to block:

#!/bin/bash
# ./scripts/validate-readonly-query.sh

INPUT=$(cat)
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')

# Block SQL write operations (case-insensitive)
if echo "$COMMAND" | grep -iE '\b(INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|TRUNCATE)\b' > /dev/null; then
  echo "Blocked: Only SELECT queries are allowed" >&2
  exit 2
fi

exit 0

Hook exit codes:

  • 0 - Allow operation
  • 2 - Block operation (with message to stderr)

Creating an Agent

Via /agents Command (Recommended)

/agents

Interactive interface to:

  • View all available subagents (built-in, user, project, plugin)
  • Create new subagents with guided setup or Claude generation
  • Edit existing subagent configuration and tool access
  • Delete custom subagents
  • See active subagents when duplicates exist (higher priority wins)

Generate with Claude: Select "Generate with Claude" and describe your subagent. Claude generates the system prompt and configuration. Press e to open in your editor.

Via File Creation

# Project agent
mkdir -p .claude/agents
cat > .claude/agents/test-runner.md << 'EOF'
---
name: test-runner
description: Run tests and fix failures. Use PROACTIVELY after code changes.
tools: Bash, Read, Glob, Grep, Edit
---

You are a test automation expert.

When invoked:
1. Identify test files related to changes
2. Run appropriate test suite
3. Analyze failures
4. Fix issues while preserving test intent
5. Re-run to verify fixes

Test frameworks to check:
- Jest/Vitest for TypeScript/JavaScript
- pytest for Python
- go test for Go
- cargo test for Rust

Always run tests before reporting completion.
EOF

Via CLI Flag

claude --agents '{
  "test-runner": {
    "description": "Run tests and fix failures",
    "prompt": "You are a test automation expert...",
    "tools": ["Bash", "Read", "Glob", "Grep", "Edit"],
    "model": "sonnet"
  }
}'

Using Agents

Automatic Delegation

Claude automatically delegates based on:

  • Task matching description field
  • Current context and available tools
  • Phrases like "PROACTIVELY" or "MUST BE USED"

Explicit Invocation

> Use the code-reviewer agent to check my changes
> Have the security-audit agent scan for vulnerabilities
> Ask the database-migration agent to create the schema

Chaining Agents

> First use test-runner to find failures, then have debugger fix them

Resuming Agents

Agents can be resumed to continue previous work:

> Resume agent abc123 and continue the analysis

Each execution gets a unique agentId stored in agent-{agentId}.jsonl.

Tool Restrictions

Read-Only Agent

tools: Read, Glob, Grep

Execution-Safe Agent

tools: Bash, Read, Glob, Grep

Note: Bash in this context is still full Bash. For truly safe execution, use hooks to validate commands.

Write-Capable Agent

tools: Read, Write, Edit, Bash, Glob, Grep

Inherit All Tools

Omit the tools field entirely to inherit all tools from main thread, including MCP tools.

Workflow: Creating a Custom Agent

Prerequisites

  • Identify the domain/expertise area
  • Determine required tools
  • Plan the system prompt

Steps

  1. Define the agent

    • Choose descriptive name (lowercase, hyphens)
    • Write clear description with trigger phrases
    • Select minimum necessary tools
    • Choose appropriate model
  2. Write system prompt

    • Define role and expertise
    • List step-by-step process
    • Include constraints and best practices
    • Add output format expectations
  3. Configure location

    • Project (.claude/agents/) for team sharing
    • User (~/.claude/agents/) for personal use
  4. Test

    • Invoke explicitly: "Use the X agent to..."
    • Check /agents menu for registration
    • Verify tool restrictions work

Validation

  • Name is lowercase with hyphens
  • Description explains when to use
  • Tools are minimum necessary
  • System prompt is clear and actionable

Priority Order

When agent names conflict, higher priority wins:

PriorityLocationScope
1 (highest)--agents CLI flagCurrent session only
2.claude/agents/Current project
3~/.claude/agents/All your projects
4 (lowest)Plugin's agents/ directoryWhere plugin is enabled

Project agents (.claude/agents/) are ideal for team sharing - check them into version control.

User agents (~/.claude/agents/) are personal agents available in all your projects.

CLI agents exist only for that session - useful for quick testing or automation scripts.

Disabling Specific Subagents

Disable built-in or custom subagents using permission rules or CLI flags.

Via settings.json

{
  "permissions": {
    "deny": ["Task(Explore)", "Task(Plan)"]
  }
}

Via CLI

claude --disallowedTools "Task(Explore)"

Permission Rule Syntax

PatternEffect
Task(Explore)Disable built-in Explore agent
Task(Plan)Disable built-in Plan agent
Task(code-reviewer)Disable custom code-reviewer agent
Task(*)Disable all subagents

Use Cases

  • Security - Prevent delegation to agents with write access
  • Workflow control - Force specific agents for certain tasks
  • Debugging - Isolate main thread behavior without subagents
  • Cost management - Disable expensive opus-based agents

Example: Read-Only Mode

Disable all write-capable agents for safe exploration:

{
  "permissions": {
    "deny": ["Task(general-purpose)", "Task(code-reviewer)"],
    "allow": ["Task(Explore)"]
  }
}

Reference Files

FileContents
DEFINITION.mdComplete frontmatter reference and file structure
EXAMPLES.mdReal-world agent examples
PATTERNS.mdBest practices and composition patterns

GitHub Repository

majiayu000/claude-skill-registry
Path: skills/creating-subagents

Related Skills

content-collections

Meta

This skill provides a production-tested setup for Content Collections, a TypeScript-first tool that transforms Markdown/MDX files into type-safe data collections with Zod validation. Use it when building blogs, documentation sites, or content-heavy Vite + React applications to ensure type safety and automatic content validation. It covers everything from Vite plugin configuration and MDX compilation to deployment optimization and schema validation.

View skill

creating-opencode-plugins

Meta

This skill provides the structure and API specifications for creating OpenCode plugins that hook into 25+ event types like commands, files, and LSP operations. It offers implementation patterns for JavaScript/TypeScript modules that intercept and extend the AI assistant's lifecycle. Use it when you need to build event-driven plugins for monitoring, custom handling, or extending OpenCode's capabilities.

View skill

sglang

Meta

SGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.

View skill

evaluating-llms-harness

Testing

This Claude Skill runs the lm-evaluation-harness to benchmark LLMs across 60+ standardized academic tasks like MMLU and GSM8K. It's designed for developers to compare model quality, track training progress, or report academic results. The tool supports various backends including HuggingFace and vLLM models.

View skill