Subagent(子 Agent)是 Claude Code 中专门处理特定类型任务的 AI 助手。每个 Subagent 在独立的上下文窗口中运行,拥有自定义的系统提示词、特定的工具访问权限和独立的权限设置。本文将带你了解内置 Subagent 的功能,并学会创建自己的专属 Subagent。
Subagent 的核心价值
Subagent 帮你解决以下问题:
- 保护主上下文:将探索和实现任务从主对话中隔离出去
- 强制约束:限制 Subagent 可使用的工具,防止越权操作
- 跨项目复用:User 级别的 Subagent 在所有项目中可用
- 专业化行为:针对特定领域使用专注的系统提示词
- 控制成本:将简单任务路由到更快、更便宜的模型(如 Haiku)
如果你需要多个 Agent 并行工作并互相通信,请参考 Agent Teams 功能。Subagent 在单个会话内工作;Agent Teams 协调跨会话的工作。
内置 Subagent
Claude Code 内置了几个核心 Subagent,Claude 会根据任务特点自动调用:
Explore(探索者)
专为搜索和分析代码库优化的快速只读 Agent:
- 模型:Haiku(快速、低延迟)
- 工具:只读工具(禁止 Write 和 Edit)
- 用途:文件发现、代码搜索、代码库探索
当 Claude 需要搜索或理解代码库但不做修改时,会委托给 Explore 处理,从而保持主对话上下文的整洁。
调用时,Claude 会指定彻底程度:quick(快速定向查找)、medium(平衡探索)、very thorough(全面分析)。
Plan(规划者)
在 Plan 模式下用于收集上下文的研究 Agent:
- 模型:继承主对话模型
- 工具:只读工具
- 用途:规划前的代码库研究
当你在 Plan 模式下工作时,Claude 会委托 Plan Subagent 进行代码库研究(Subagent 无法再创建 Subagent,避免无限嵌套)。
General-purpose(通用型)
适合需要探索和操作相结合的复杂多步骤任务:
- 模型:继承主对话模型
- 工具:所有工具
- 用途:复杂研究、多步骤操作、代码修改
其他内置 Agent
| Agent | 模型 | 使用时机 |
|---|---|---|
| Bash | 继承 | 在独立上下文中运行终端命令 |
| statusline-setup | Sonnet | 运行 /statusline 配置状态栏时 |
| Claude Code Guide | Haiku | 询问 Claude Code 功能问题时 |
快速上手:创建你的第一个 Subagent
使用 /agents 命令创建
Subagent 定义在包含 YAML frontmatter 的 Markdown 文件中。
步骤 1:打开 Subagent 界面
/agents步骤 2:选择 Create new agent → User-level(保存到 ~/.claude/agents/,所有项目可用)
步骤 3:选择 Generate with Claude,描述你的 Subagent:
A code improvement agent that scans files and suggests improvements
for readability, performance, and best practices. It should explain
each issue, show the current code, and provide an improved version.步骤 4:选择工具权限(只读审查者选 Read-only tools)
步骤 5:选择模型(Sonnet 适合代码分析)
步骤 6:选择颜色(方便在 UI 中识别)
步骤 7:保存并测试
Use the code-improver agent to suggest improvements in this project手动创建 Subagent 文件
Subagent 文件格式:
---
name: security-reviewer
description: Reviews code changes for security vulnerabilities. Use when reviewing
authentication code, API endpoints, data validation, or sensitive operations.
model: claude-sonnet-4-6
tools:
- Read
- Bash
permissions:
deny:
- Bash(rm *)
- Write(**)
---
# Security Code Reviewer
You are a security-focused code reviewer. When analyzing code:
1. Check for SQL injection vulnerabilities
2. Verify input validation and sanitization
3. Review authentication and authorization logic
4. Check for hardcoded secrets or sensitive data
5. Assess dependency security risks
For each issue found:
- Describe the vulnerability type
- Show the vulnerable code
- Explain the risk
- Provide a secure alternative
Always reference OWASP guidelines when applicable.保存路径:
- 用户级(全项目):
~/.claude/agents/security-reviewer.md - 项目级(当前项目):
.claude/agents/security-reviewer.md
通过 CLI 标志定义
claude --agent "security-reviewer:.claude/agents/security-reviewer.md" \
"Review the auth module for security issues"实用 Subagent 配置示例
文档生成器
---
name: doc-writer
description: Generates API documentation, README files, and inline code comments.
Use when writing or updating documentation for functions, modules, or APIs.
model: claude-haiku-4-5
tools:
- Read
- Write
---
You generate clear, comprehensive documentation. For each function or module:
1. Write a concise one-line summary
2. Document all parameters with types and descriptions
3. Document return values
4. Include usage examples
5. Note any edge cases or important behaviors
Use JSDoc format for JavaScript/TypeScript, docstrings for Python.测试生成器
---
name: test-writer
description: Creates unit tests and integration tests for existing code.
Use when adding test coverage to functions, classes, or API endpoints.
model: claude-sonnet-4-6
tools:
- Read
- Write
- Bash
---
You write thorough test suites. For each component to test:
1. Identify all code paths and edge cases
2. Write unit tests with descriptive names
3. Add integration tests where appropriate
4. Include both happy path and error cases
5. Ensure >80% code coverage
Use the project's existing test framework and conventions.Subagent vs. Agent Teams
选择 Subagent 还是 Agent Teams?关键区别:
| Subagent | Agent Teams | |
|---|---|---|
| 上下文 | 独立上下文窗口,结果返回调用者 | 独立上下文窗口,完全独立 |
| 通信 | 只向主 Agent 报告结果 | 成员间可直接通信 |
| 协调 | 主 Agent 管理所有工作 | 共享任务列表,自我协调 |
| 适用场景 | 只关心结果的聚焦任务 | 需要讨论和协作的复杂工作 |
| Token 成本 | 较低:结果汇总返回 | 较高:每个成员完整上下文 |
选择 Subagent:任务相对独立,只需要返回结果 选择 Agent Teams:任务需要多个 Agent 讨论和协作
最佳实践
- 描述要精确:Claude 根据 description 决定何时使用 Subagent,描述越精确,使用越准确
- 工具最小化:只赋予 Subagent 完成任务所需的工具权限
- 选对模型:简单搜索用 Haiku,复杂分析用 Sonnet,顶级任务用 Opus
- User vs Project 级别:通用 Subagent(安全审查、文档生成)放用户级;项目特定的放项目级
- 测试 Subagent:创建后立即测试,确保行为符合预期
总结
Claude Code 的 Subagent 系统提供了强大的任务专业化和上下文管理能力。从内置的 Explore、Plan、General-purpose Subagent 开始,逐步创建符合团队需求的自定义 Subagent,能显著提升开发效率和代码质量。
来源:Claude Code 官方文档 - Sub-agents 原文作者:Anthropic Team