子代理
什么是子代理?
子代理(Subagents) 是 Claude Code 的专用智能体,每个子代理专门处理特定类型的任务。就像一个团队中的专家,各司其职。
核心优势:
- 🎯 专业化:针对特定任务优化
- ⚡ 高效:更快的执行速度
- 💰 经济:某些子代理使用更便宜的模型
- 🔀 并行:可以同时运行多个子代理
可用子代理类型
1. Explore Agent (探索代理)
用途: 快速探索代码库,查找文件和代码
特点:
- 使用 Haiku 模型(快速且便宜)
- 适合搜索和浏览任务
- 不执行修改操作
示例:
bash
You: 探索代码库,找出所有API端点
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(规划代理)
用途: 制定实施计划
特点:
- 分析需求
- 制定步骤
- 识别关键文件
- 考虑架构权衡
示例:
bash
You: 规划如何添加用户认证功能
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(命令行代理)
用途: 专门处理命令行任务
特点:
- 执行 Git 操作
- 运行构建和测试
- 管理依赖
- 系统命令
示例:
bash
You: 安装依赖并运行测试
Claude: I'll use the Bash Agent for this.
[Launching Bash Agent...]
$ npm install
✓ Installed 125 packages
$ npm test
✓ 45 tests passed4. Feature Dev Agents(功能开发代理)
包含三个子代理:
- Code Explorer: 深入分析现有功能
- Code Architect: 设计功能架构
- Code Reviewer: 审查代码质量
示例:
bash
You: 开发一个评论系统功能
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使用子代理
自动选择
Claude Code 会智能选择合适的子代理:
bash
# 这会自动使用 Explore Agent
You: 找出所有测试文件
# 这会自动使用 Plan Agent
You: 规划添加支付功能
# 这会自动使用 Bash Agent
You: 提交代码更改手动指定
可以显式指定使用哪个子代理:
bash
# 使用 Explore Agent
You: /explore 搜索所有 TODO 注释
# 使用 Plan Agent
You: /plan 实现用户权限系统
# 使用 Bash Agent
You: /bash 运行数据库迁移并行执行
同时运行多个子代理:
bash
You: 并行执行:探索代码库结构,同时运行测试
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实战案例
案例 1:大型代码库分析
bash
You: 分析这个大型项目的架构
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)案例 2:功能开发流程
bash
You: 开发一个完整的博客评论功能
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案例 3:并行任务处理
bash
You: 同时完成:1)运行测试 2)生成文档 3)分析性能
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选择合适的子代理
| 任务类型 | 推荐子代理 | 原因 |
|---|---|---|
| 搜索文件/代码 | Explore Agent | 快速、便宜、专注搜索 |
| 制定计划 | Plan Agent | 结构化思考、详细规划 |
| Git操作 | Bash Agent | 专门处理命令行 |
| 完整功能开发 | Feature Dev Agents | 全流程支持 |
| 代码审查 | Code Reviewer | 专业审查能力 |
| 简单任务 | General Agent | 通用处理 |
决策流程:
任务是否需要探索代码库?
├─ 是 → Use Explore Agent
└─ 否 → 任务是否需要规划?
├─ 是 → Use Plan Agent
└─ 否 → 任务是否是命令行操作?
├─ 是 → Use Bash Agent
└─ 否 → Use General Agent小结
在本章中,我们学习了:
- ✅ 子代理的概念和优势
- ✅ 可用的子代理类型及其用途
- ✅ 如何使用子代理(自动/手动/并行)
- ✅ 实战案例演示
- ✅ 如何选择合适的子代理
关键要点:
- 子代理提高效率和降低成本
- 可以并行执行多个任务
- Claude 会智能选择子代理
- 可以手动指定特定子代理