Skip to main content

Create Custom Subagents

Official guide for creating specialized AI subagents in Claude Code for task-specific workflows and improved context management. Source: https://code.claude.com/docs/en/sub-agents.md Added: 2026-01-29


Overview

Subagents are specialized AI assistants running in their own context window with custom system prompts, specific tool access, and independent permissions. They help:

  • Preserve context by isolating exploration/implementation
  • Enforce constraints by limiting tools
  • Reuse configurations across projects (user-level)
  • Specialize behavior with focused system prompts
  • Control costs by routing to cheaper models (Haiku)

Built-in Subagents

AgentModelToolsPurpose
ExploreHaikuRead-onlyFile discovery, code search, codebase exploration
PlanInheritsRead-onlyCodebase research during plan mode
General-purposeInheritsAllComplex multi-step tasks requiring exploration + action
BashInherits-Running terminal commands in separate context
statusline-setupSonnet-Configure status line via /statusline
Claude Code GuideHaiku-Answer questions about Claude Code features

Creating Subagents

Using /agents Command

Interactive interface for creating, editing, deleting subagents. Can generate with Claude.

Writing Subagent Files

---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Glob, Grep
model: sonnet
---

You are a code reviewer. Analyze code and provide specific,
actionable feedback on quality, security, and best practices.

Scope & Priority

LocationScopePriority
--agents CLI flagCurrent session1 (highest)
.claude/agents/Current project2
~/.claude/agents/All your projects3
Plugin agents/Where plugin enabled4

CLI-Defined Subagents

claude --agents '{
"code-reviewer": {
"description": "Expert code reviewer.",
"prompt": "You are a senior code reviewer.",
"tools": ["Read", "Grep", "Glob", "Bash"],
"model": "sonnet"
}
}'

Frontmatter Reference

FieldRequiredDescription
nameYesUnique identifier (lowercase, hyphens)
descriptionYesWhen Claude should delegate
toolsNoTool allowlist. Inherits all if omitted
disallowedToolsNoTools to deny
modelNosonnet, opus, haiku, or inherit (default)
permissionModeNodefault, acceptEdits, dontAsk, bypassPermissions, plan
skillsNoSkills to preload into context at startup
hooksNoLifecycle hooks scoped to this subagent

Permission Modes

ModeBehavior
defaultStandard permission checking
acceptEditsAuto-accept file edits
dontAskAuto-deny prompts (allowed tools still work)
bypassPermissionsSkip all checks (use with caution)
planRead-only exploration

Foreground vs Background

  • Foreground: Blocks main conversation. Permission prompts pass through to you
  • Background: Runs concurrently. Pre-approved permissions, auto-denies anything not pre-approved. MCP tools unavailable

Press Ctrl+B to background a running task.


Common Patterns

Isolate High-Volume Operations

Use a subagent to run the test suite and report only failing tests

Parallel Research

Research the authentication, database, and API modules in parallel using separate subagents

Chain Subagents

Use code-reviewer to find performance issues, then use optimizer to fix them

Preload Skills

---
name: api-developer
skills:
- api-conventions
- error-handling-patterns
---

Full skill content injected into context (not just made available for invocation).


Hooks in Subagents

In Frontmatter (scoped to subagent)

hooks:
PreToolUse:
- matcher: "Bash"
hooks:
- type: command
command: "./scripts/validate-command.sh"

In settings.json (project-level)

{
"hooks": {
"SubagentStart": [{ "matcher": "db-agent", "hooks": [...] }],
"SubagentStop": [{ "matcher": "db-agent", "hooks": [...] }]
}
}

Disabling Subagents

{
"permissions": {
"deny": ["Task(Explore)", "Task(my-custom-agent)"]
}
}

When to Use

Use subagents when...Use main conversation when...
Task produces verbose outputFrequent back-and-forth needed
Need tool restrictionsMultiple phases share context
Work is self-containedQuick, targeted change
Want separate contextLatency matters

Subagents cannot spawn other subagents. Use skills or chain from main conversation.