What Are Subagents?
Subagents are custom AI agents with their own configuration, including:- Name and color for visual distinction
- Tool allowlist to control what they can access
- Model selection (haiku, sonnet, opus, or inherit)
- Permission mode for auto-approving actions
- Preloaded skills for domain knowledge
- MCP servers for external integrations
- Lifecycle hooks for events
Subagents are defined in
.claude/agents/<name>.md with YAML frontmatter and invoked via the Task tool.Why Subagents Matter
Subagents enable specialization and orchestration:- Specialized expertise: Create agents for specific domains (frontend, backend, DevOps)
- Tool isolation: Limit what each agent can do for safety
- Parallel execution: Multiple agents can work on different parts of a codebase
- Persistent memory: Agents remember context across sessions
- Progressive disclosure: Preload skills for focused context
Key Concepts
Built-in vs Custom Agents
Built-in agents (available out-of-the-box):
Custom agents (defined by you):
Create agents tailored to your project’s needs with specific tools, skills, and behavior.
Invocation via Task Tool
Always invoke subagents using the Task tool:claude agent-name — this won’t work.
Frontmatter Fields
string
required
Unique identifier using lowercase letters and hyphens. Used in
Task(subagent_type="name").string
required
When to invoke this agent. Use
"PROACTIVELY" for auto-invocation by Claude.Example: "Use this agent PROACTIVELY for deployment pipelines"string or list
Comma-separated allowlist of tools. Inherits all tools if omitted.Examples:
Read, Write, Edit, BashTask(agent_type)— Restrict which subagents this agent can spawn["Read", "Grep", "Glob"]
string or list
Tools to deny, removed from inherited or specified list.Example:
NotebookEdit, WebSearchstring
default:"inherit"
Model alias:
haiku, sonnet, opus, or inherit (uses parent’s model).string
Permission mode for this agent:
default— Standard permission checkingacceptEdits— Auto-accept file editsdontAsk— Bypass most promptsbypassPermissions— Skip all permission checks (dangerous)plan— Read-only planning mode
integer
Maximum number of agentic turns before the subagent stops.Example:
25list
Skill names to preload into agent context at startup. Full skill content is injected, not just made available.Example:
list
MCP servers for this subagent — server name strings or inline configs.Example:
object
Lifecycle hooks scoped to this subagent.Supported events:
PreToolUse, PostToolUse, PermissionRequest, PostToolUseFailure, Stop (converted to SubagentStop at runtime), SubagentStopstring
Persistent memory scope:
user— Stored in~/.claude/agent-memory/<name>/, not sharedproject— Stored in.claude/agent-memory/<name>/, committed to gitlocal— Stored in.claude/agent-memory-local/<name>/, not committed
boolean
default:"false"
Set to
true to always run as a background task.string
Set to
"worktree" to run in a temporary git worktree (auto-cleaned if no changes).string
CLI output color for visual distinction.Examples:
green, magenta, blue, tealReal Examples
How to Use Subagents
1
Create an agent definition
Create a markdown file in
.claude/agents/ with YAML frontmatter:2
Define agent configuration
Add frontmatter and instructions:
3
Invoke from commands or main agent
Use the Task tool to invoke your agent:
Agent Skills (Preloaded Knowledge)
Preload skills into agents for focused domain knowledge:weather-fetcher skill is injected into the agent’s context at startup. The agent sees the full skill content without needing to invoke it separately.
Agent Memory
Agents can persist memory across sessions:Agent Memory Report
Deep dive into persistent memory scopes and how agents learn across sessions
Best Practices
Create feature-specific agents
Create feature-specific agents
Instead of general “backend-engineer” or “frontend-engineer” agents, create agents for specific features or domains:Good:
deploy-manager, api-tester, database-migratorBad: general-engineer, code-helperUse agents with preloaded skills
Use agents with preloaded skills
Combine agents with skills for progressive disclosure:
Restrict tools appropriately
Restrict tools appropriately
Only give agents the tools they need:
Set appropriate models
Set appropriate models
haikufor simple, fast operationssonnetfor most tasksopusfor complex reasoninginheritto use parent’s model
Use PROACTIVELY for auto-invocation
Use PROACTIVELY for auto-invocation
Add “PROACTIVELY” to the description so Claude knows to invoke the agent automatically:
Scope and Priority
When multiple agents share the same name:1
CLI flag (highest)
--agents '{...}' — Session-scoped agents defined via CLI2
Project
.claude/agents/ — Project-specific agents, committed to git3
User
~/.claude/agents/ — Personal agents across all projects4
Plugin (lowest)
<plugin>/agents/ — Agents from installed pluginsOrchestration Patterns
Command → Agent → Skill
The recommended pattern for complex workflows:- Command provides the entry point and user interaction
- Agent executes tasks with preloaded skills
- Skill creates outputs independently
Orchestration Workflow
See the complete Command → Agent → Skill pattern with diagrams
Cross-Links
Commands
Learn how commands orchestrate agents
Skills
Preload skills into agents for domain knowledge
Hooks
Add lifecycle hooks to agents
Subagents Best Practice
Complete frontmatter reference and built-in agents
