Skip to content

Chapter 8: Subagents


What are Subagents?

Subagents are specialized agents in Claude Code, each designed to handle specific types of tasks. Like experts in a team, each has their specialty.

Core Advantages:

  • 🎯 Specialized: Optimized for specific tasks
  • Efficient: Faster execution
  • 💰 Economical: Some subagents use cheaper models
  • 🔀 Parallel: Can run multiple subagents simultaneously

Available Subagent Types

1. Explore Agent

Purpose: Quickly explore codebase, find files and code

Features:

  • Uses Haiku model (fast and cheap)
  • Suitable for search and browsing tasks
  • No modification operations

Example:

8.2 可用子代理类型

bash
You: explore codebase and find all API endpoints

Claude: I'll use the Explore Agent to find API endpoints.
[Launching Explore Agent...]

Found 23 API endpoints across 5 files:
- GET /api/users (src/routes/users.js:12)
- POST /api/users (src/routes/users.js:45)
...

2. Plan Agent

Purpose: Create implementation plans

Features:

  • Analyze requirements
  • Create steps
  • Identify key files
  • Consider architectural trade-offs

Example:

bash
You: plan how to add user authentication

Claude: I'll use the Plan Agent to create an implementation plan.
[Launching Plan Agent...]

Implementation Plan:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. Create auth middleware (src/middleware/auth.js)
2. Add JWT generation utility (src/utils/jwt.js)
3. Update user routes with authentication
4. Create login/register endpoints
5. Add tests for auth flow

Estimated files to modify: 6
Estimated new files: 3
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Proceed with implementation? (yes/no)

3. Bash Agent

Purpose: Handle command-line tasks

Features:

  • Execute Git operations
  • Run builds and tests
  • Manage dependencies
  • System commands

Example:

bash
You: install dependencies and run tests

Claude: I'll use the Bash Agent for this.
[Launching Bash Agent...]

$ npm install
✓ Installed 125 packages

$ npm test
✓ 45 tests passed

4. Feature Dev Agents

Includes three subagents:

  • Code Explorer: Deep analysis of existing features
  • Code Architect: Design feature architecture
  • Code Reviewer: Review code quality

Example:

bash
You: develop a comment system feature

Claude: I'll use Feature Dev agents for this.

[Code Explorer analyzing existing patterns...]
✓ Analyzed comment patterns in codebase

[Code Architect designing architecture...]
✓ Created architecture blueprint

[Implementation...]
✓ Feature implemented

[Code Reviewer checking quality...]
✓ Code review passed

Using Subagents

Automatic Selection

Claude Code intelligently selects appropriate subagents:

8.3 使用子代理

bash
# This automatically uses Explore Agent
You: find all test files

# This automatically uses Plan Agent
You: plan adding payment feature

# This automatically uses Bash Agent
You: commit code changes

Manual Specification

Explicitly specify which subagent to use:

bash
# Use Explore Agent
You: /explore search all TODO comments

# Use Plan Agent
You: /plan implement user permission system

# Use Bash Agent
You: /bash run database migration

Parallel Execution

Run multiple subagents simultaneously:

bash
You: run in parallel: explore codebase structure and run tests

Claude: I'll run these tasks in parallel.

[Explore Agent] Analyzing codebase structure...
[Bash Agent] Running tests...

✓ Explore Agent completed: Found 45 modules
✓ Bash Agent completed: 120 tests passed

Practical Examples

Case 1: Large Codebase Analysis

8.4 实战案例

bash
You: analyze architecture of this large project

Claude: This is a large codebase. I'll use specialized agents.

[Explore Agent: quick scan]
- 234 files found
- 12 main modules identified
- Using: React, Node.js, PostgreSQL

[Analysis complete in 30 seconds]

Summary:
- Frontend: React with Redux (src/client/)
- Backend: Express API (src/server/)
- Database: PostgreSQL with Sequelize
- Tests: Jest (85% coverage)

Case 2: Feature Development Workflow

bash
You: develop a complete blog comment feature

Claude: I'll use Feature Dev agents for comprehensive development.

[Step 1: Code Explorer]
Analyzing existing comment patterns...
✓ Found similar implementation in forum module

[Step 2: Code Architect]
Designing comment system architecture...
✓ Created blueprint:
  - Comment model
  - API endpoints
  - Frontend components
  - Notification service

[Step 3: Implementation]
Creating files...
✓ models/Comment.js
✓ routes/comments.js
✓ components/CommentList.jsx
✓ services/notification.js

[Step 4: Code Reviewer]
Reviewing implementation...
✓ No security vulnerabilities
✓ Follows project conventions
⚠ Suggestion: Add rate limiting

[Step 5: Tests]
Generating tests...
✓ 18 tests created and passing

Case 3: Parallel Task Processing

bash
You: complete simultaneously: 1)run tests 2)generate docs 3)analyze performance

Claude: Running 3 tasks in parallel...

[Bash Agent] Running tests...
[General Agent] Generating documentation...
[Explore Agent] Analyzing performance...

Results:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[1] Tests: 95/95 passed ✓
[2] Documentation: api-docs.md created ✓
[3] Performance: 3 bottlenecks found ⚠
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Performance issues:
1. Database N+1 query in users.js:45
2. Large bundle size in dashboard.js
3. Unoptimized images in /assets

Choosing the Right Subagent

Task TypeRecommended SubagentReason
Search files/codeExplore AgentFast, cheap, focused on search
Create plansPlan AgentStructured thinking, detailed planning
Git operationsBash AgentSpecialized in command-line
Complete feature devFeature Dev AgentsFull workflow support
Code reviewCode ReviewerProfessional review capability
Simple tasksGeneral AgentGeneral purpose

Decision Flow:

Does task need codebase exploration?
  ├─ Yes → Use Explore Agent
  └─ No → Does task need planning?
      ├─ Yes → Use Plan Agent
      └─ No → Is task command-line operation?
          ├─ Yes → Use Bash Agent
          └─ No → Use General Agent

Summary

In this chapter, we learned:

  • ✅ Subagent concepts and advantages
  • ✅ Available subagent types and purposes
  • ✅ How to use subagents (auto/manual/parallel)
  • ✅ Practical case demonstrations
  • ✅ How to choose appropriate subagents

Key Takeaways:

  • Subagents improve efficiency and reduce costs
  • Can execute multiple tasks in parallel
  • Claude intelligently selects subagents
  • Can manually specify specific subagents

Next Step: Chapter 9 introduces the plugin system!


Content is for learning and research only.