๐Ÿฆ Subagent

November 28, 2025 ยท View on GitHub

๐Ÿ  Home โ€บ ๐Ÿ”ง Implementation โ€บ ๐Ÿ“ฆ Components โ€บ ๐Ÿฆ Subagent

โ† Components โ”โ”โ—โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿฆด Slash Command โ†’


๐Ÿฆ Subagent

A Subagent is an independent worker spawned by the ๐Ÿ” Main Agent via the Task tool (๐Ÿชบ spawn action) to handle specific, isolated tasks.


Key Characteristics

PropertyValue
InvocationTask tool with subagent_type parameter (๐Ÿชบ spawn)
Location.claude/agents/*.md
AutonomyFull - executes independently
SpawningโŒ Cannot spawn other subagents
ContextIsolated from main conversation
PermissionsControlled via permissionMode frontmatter

File Structure

# .claude/agents/code-reviewer.md

---
name: code-reviewer
description: Reviews code for quality, security, and best practices
tools: Read, Write, Grep, Glob
model: sonnet
permissionMode: acceptEdits
skills: test-driven-development, code-review
---

You are a code review specialist. Your task is to...

Note: tools and skills are comma-separated strings, not YAML lists.


Frontmatter Reference

FieldRequiredDescription
nameYesUnique identifier (lowercase, hyphens)
descriptionYesNatural language description for discovery
toolsNoComma-separated tool list. Omit to inherit all tools
modelNosonnet, opus, haiku, or inherit (default: configured subagent model)
permissionModeNoControls permission handling (see below)
skillsNoComma-separated skill names to auto-load

Permission Modes

ModeBehaviorUse Case
defaultAsks permission for each toolRead-only, validation
acceptEditsAuto-approves Write/EditGeneration after ๐Ÿง™ user confirmation
bypassPermissionsAll tools auto-approvedTrusted autonomous workflows
planRead-only planning modeResearch without modifications
ignoreSkip permission prompts entirelyBatch processing

Best Practice: Use acceptEdits after ๐Ÿง™ Wizard confirmation to enable autonomous generation without repeated permission prompts.


Usage Examples

Basic Invocation

# ๐Ÿ” Main Agent ๐Ÿชบ spawns ๐Ÿฆ subagent via Task tool
Task(
    subagent_type="code-reviewer",
    prompt="Review the authentication module in src/auth/ for security vulnerabilities. Focus on: 1) Input validation 2) Session management 3) Password handling",
    description="Security review of auth module"
)

With Model Override

Task(
    subagent_type="code-reviewer",
    prompt="Quick syntax check of utils.py",
    model="haiku",  # Use faster model for simple tasks
    description="Quick syntax review"
)

Resumable Invocation

# First call - returns agentId
result = Task(
    subagent_type="research-analyst",
    prompt="Research the current state of WebSocket libraries in Python",
    description="WebSocket library research"
)
# result.agentId = "agent-abc123"

# Later - resume with context
Task(
    subagent_type="research-analyst",
    prompt="Now compare the top 3 libraries you found and recommend one",
    resume="agent-abc123",  # Continue previous conversation
    description="WebSocket library comparison"
)

Mermaid Representation

%%{init: {'theme': 'base', 'themeVariables': {'lineColor': '#64748b'}}}%%
sequenceDiagram
    participant U as ๐Ÿ™‹โ€โ™€๏ธ User
    participant MA as ๐Ÿ” Main Agent
    participant SA as ๐Ÿฆ Subagent
    participant T as ๐Ÿ”ง Tools

    U->>MA: "Review my code"
    MA->>SA: ๐Ÿชบ Task(subagent_type="code-reviewer")
    SA->>T: Read, Grep, Glob
    T-->>SA: Results
    SA-->>MA: ๐Ÿฆ๐Ÿ“ค Review Report
    MA-->>U: ๐Ÿ’โ€โ™€๏ธ๐Ÿ“ค "Here's the review..."

Built-in Subagents

SubagentModelToolsPurpose
General-purposeSonnetAll toolsComplex multi-step tasks
PlanSonnetRead, Glob, Grep, BashResearch (read-only)
ExploreHaikuGlob, Grep, Read, BashFast codebase searching

Explore Thoroughness: quick โ†’ medium โ†’ very thorough


Resumable Subagents

Subagents can be resumed to continue previous conversations, maintaining full context.

How It Works

%%{init: {'theme': 'base', 'themeVariables': {'lineColor': '#64748b'}}}%%
sequenceDiagram
    participant MA as ๐Ÿ” Main Agent
    participant SA as ๐Ÿฆ Subagent
    participant FS as ๐Ÿ’พ File System

    MA->>SA: Task(prompt="Research X")
    SA->>SA: Work on task...
    SA-->>MA: Return result + agentId
    SA->>FS: Save transcript (agent-{id}.jsonl)

    Note over MA,FS: Later...

    MA->>SA: Task(resume="abc123", prompt="Continue with Y")
    FS-->>SA: Load previous transcript
    SA->>SA: Resume with full context
    SA-->>MA: Return continued result

End-to-End Example: Research Project

Session 1: Initial Research

# Start a research task
result1 = Task(
    subagent_type="research-analyst",
    prompt="""Research the current state of Python async web frameworks.

    Investigate:
    1. FastAPI - features, performance, ecosystem
    2. Starlette - relationship to FastAPI
    3. AIOHTTP - comparison points
    4. Litestar - newer alternative

    Create a comparison matrix and initial recommendation.""",
    description="Async framework research"
)

# Result includes agentId for later resumption
# result1.agentId = "agent-research-abc123"
# Transcript saved to: agent-research-abc123.jsonl

Session 2: Continue with Deeper Analysis

# Resume the same subagent with its full context
result2 = Task(
    subagent_type="research-analyst",
    prompt="""Based on your previous research, now:

    1. Deep dive into FastAPI's dependency injection system
    2. Compare its approach to Flask/Django
    3. Provide code examples showing the pattern

    Build on what you learned in the previous analysis.""",
    resume="agent-research-abc123",  # Continue previous conversation
    description="Deep dive into FastAPI DI"
)

# The subagent remembers all previous research context

Session 3: Final Recommendation

# Continue to final recommendation
result3 = Task(
    subagent_type="research-analyst",
    prompt="""Now provide final recommendation:

    1. Which framework for our e-commerce API?
    2. Migration path from current Flask app
    3. Team training requirements
    4. Timeline estimate

    Use all your research to justify the recommendation.""",
    resume="agent-research-abc123",
    description="Final framework recommendation"
)

Transcript Storage

project/
โ”œโ”€โ”€ agent-research-abc123.jsonl    # Research analyst transcript
โ”œโ”€โ”€ agent-reviewer-def456.jsonl    # Code reviewer transcript
โ””โ”€โ”€ .claude/
    โ””โ”€โ”€ agents/
        โ””โ”€โ”€ research-analyst.md    # Agent definition

Best Practices

PracticeReason
Use descriptive promptsContext carries forward, be specific
Resume same subagent typeDifferent types have different capabilities
Check agentId existsTranscript might be cleaned up
Build on previous workReference "your previous analysis"

Critical Rule

๐Ÿฆ Subagents cannot spawn other subagents.

All delegation must go through the ๐Ÿ” Main Agent.


โ† Components โ”โ”โ—โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿฆด Slash Command โ†’