匠人学院 JR Academy学AI来匠人
匠人学院 JR Academy学AI来匠人

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png
EN

关于公司

关于我们元宇宙课堂新闻资讯匠人工作成为导师匠人导师联系我们匠人商店J3.Club

匠人资源

工作内推匠人活动1对1私教行业白皮书线上学习平台面试中心分享面试经验Internship会员中心

AI 工具

AI 工具箱考证匠 Cert Master求职匠 Job Hunter牛小匠 UniMate AI

AI 学习方向

全部学习方向AI EngineerContext EngineeringVibe CodingPrompt MasterAI BuilderAI 产品经理Python 入门

AI 应用提效

AI 办公提效AI 数据分析AI 财务AI 内容创作AI 视觉创作前端开发Hermes AgentOpenClaw 本地智能体

大学资源

墨尔本大学昆士兰大学新南威尔士大学悉尼大学莫那什大学阿德莱德大学RMITQUTUTS

少儿 AI 教育

Airbotix 少儿 AI 编程澳洲家长实用资料库NAPLAN 成绩单怎么看My School 学校数据指南悉尼私校学费 2026少儿编程课程与训练营

移民服务

澳洲移民技术移民189/190/491雇主担保482/186/494投资移民188/888英国移民美国移民加拿大移民

企业合作

P3职业孵化器Enterprise (EN)企业培训实习合作招聘合作申请合作

求职代理

岗位代投职位监控LinkedIn代运营LinkedIn人脉代加了解P3项目

匠人支持

FAQsTerms & ConditionsPrivacy PolicyCancellation & Refund PolicySite map

Top Categories

Web全栈班DevOps项目班数据工程全栈班数据分析项目班编程入门班Business Analyst实习算法集训营

求职就业

BA和产品经理实习数据科学实习数据分析实习Marketing实习简历修改面试指导导师指导VIP

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

联系方式

hello@jiangren.com.au0421-672-555

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2026 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572

首页/资源中心/文章详情
JR Academy · Blog职业洞察

Claude Code 从入门到实战 — Subagent 设计模式:什么时候用、怎么写、如何隔离

Subagent 的核心价值是上下文隔离——把大量探索结果关在独立窗口里,主对话只看最终摘要。本章覆盖:何时委托、frontmatter 结构、Prompt 怎么写、worktree 隔离

发布日期2026-09-20
阅读时长4 分钟
作者

快速导航

  • 为什么要用 Subagent
  • Subagent 文件格式
  • 完整 frontmatter 字段
  • 内置 Subagent
  • 怎么写 Subagent 的 Prompt
  • 上下文隔离的好处
  • Worktree 隔离
  • 实战:几个常见的自定义 Subagent
  • 只读代码审查
  • 测试生成器
  • 批量文档更新(worktree 隔离)
  • 用 CLI flag 临时定义 Subagent
  • 禁用特定 Subagent
  • 小结

为什么要用 Subagent

主对话的上下文窗口是有限资源。你让 Claude 扫 200 个文件找一个 bug,那 200 个文件的内容全部堆进同一个窗口——后续每次对话都要携带这些噪音,成本涨、精度降。

Subagent 的价值就一条:把副任务的中间过程关在一个独立窗口里,主对话只收一份摘要。

官方文档对触发时机的描述很直接:

Use one when a side task would flood your main conversation with search results, logs, or file contents you won't reference again.

Subagent 上下文隔离示意

三个具体场景:

  1. 探索型任务:搜代码、读文件、查日志。结果量大但最终只需要一句结论。
  2. 并行独立任务:写测试、格式化代码、更新文档——互不依赖,同时跑省时间。
  3. 工具受限场景:只需要 Read/Grep/Glob,不允许 Write/Edit——把能力限死,防止越权。

Subagent 文件格式

Subagent 定义在 Markdown 文件里,放 .claude/agents/(项目级)或 ~/.claude/agents/(用户级),YAML frontmatter 是配置,正文是系统 Prompt。

---
name: code-reviewer
description: Reviews code for quality and best practices. Use after any code change.
tools: Read, Glob, Grep
model: sonnet
---

You are a senior code reviewer. Analyze the code for:
- Security vulnerabilities (injection, auth bypass, data exposure)
- Performance bottlenecks
- Readability and naming issues

For each issue: show the file + line, the problem, and a concrete fix.

name 是唯一标识,description 是 Claude 决定「要不要委托」的依据——这两个字段是必填项,其余都可选。

完整 frontmatter 字段

字段 说明
name 必填,小写字母加连字符,全局唯一
description 必填,Claude 用这段文字判断何时委托
tools 允许的工具列表,省略则继承主对话所有工具
disallowedTools 明确禁用的工具(从继承列表里剔除)
model haiku / sonnet / opus / fable / 完整 model ID / inherit
permissionMode default / acceptEdits / auto / dontAsk / bypassPermissions / plan
maxTurns 最多跑几轮,防止失控
isolation worktree:给 subagent 一份独立 git worktree
memory user / project / local:跨 session 保留学习内容
background true:默认后台运行
color UI 里的颜色标识(red / blue / green 等)

内置 Subagent

Claude Code 自带三个常用 subagent,不需要任何配置就能用:

Explore — 只读探索,用 Haiku 跑,速度快、成本低。Claude 需要搜代码但不需要修改时自动委托。调用时可以指定深度:quick(单次精准查找)、medium(适度探索)、very thorough(全面扫描多个位置)。

Plan — Plan 模式下的研究 agent。你在 plan 模式里提任务,Claude 把代码库调研丢给 Plan subagent,主对话保持只读等待结果。

General-purpose — 全能型,继承主对话所有工具,用于需要「探索 + 修改 + 多步骤」的复杂任务。

另外还有 claude-code-guide(你问 Claude Code 功能时 Claude 自动用)和 statusline-setup(跑 /statusline 时自动用)。

怎么写 Subagent 的 Prompt

Subagent 的正文(Markdown body)就是它的系统 Prompt。它只收到这段 Prompt 加上基本环境信息(工作目录等),不继承主对话的 Claude Code 系统 Prompt。

这意味着主对话里的 CLAUDE.md 规则、用户设置、历史上下文,subagent 都不知道——除非你明确写进 Prompt 里。

几条实用原则:

1. description 比 prompt 更重要

Claude 是根据 description 决定委托还是自己做,根据 prompt(正文)决定怎么做。description 写得模糊,Claude 就不会用它。要写成「当 X 发生时用我」的形式:

description: >
  Scans any Python file for common security issues: SQL injection,
  hardcoded secrets, unsafe deserialization. Use proactively after
  any change to .py files.

2. 告诉它输出什么格式

Subagent 的输出是主对话唯一能看到的东西。格式不定,主对话很难用结果做后续判断。

Return results as a JSON array:
[{"file": "...", "line": N, "issue": "...", "severity": "high|medium|low"}]
If no issues found, return [].

3. 把约束写死

Subagent 没有主对话的语境,它不知道「不要改生产配置」这种隐性规则。需要限制的就明确写:

NEVER modify files. NEVER run commands that have side effects.
Only read and analyze.

4. 给复杂任务加 maxTurns

没有 maxTurns 的 subagent 可能在遇到意外情况时无限循环。探索型任务通常 maxTurns: 10 够用;复杂实现任务可以到 30-50。

上下文隔离的好处

主对话上下文和 subagent 上下文是完全独立的两个窗口。subagent 做的所有中间工作——tool calls、中间推理、部分结果——全部留在它自己的窗口里。主对话只收一条最终消息。

这个设计有几个实际收益:

成本控制:搜索 100 个文件的 token 消耗,算在 subagent 里。主对话窗口没有膨胀,后续对话的每次推理成本不变。

结果质量:主对话上下文越干净,Claude 越不容易被历史噪音干扰。让 subagent 处理探索,主对话专注决策。

模型优化:用 model: haiku 的 Explore subagent 做文件搜索,用 model: opus 的主对话做架构决策——根据任务选最合适的模型,而不是一刀切。

# 示意:主对话里 Claude 如何自动委托 Explore
用户: 帮我找所有用了 deprecated axios.get 的地方
Claude: → 委托给 Explore subagent(快速,只读)
        Explore: 扫描 47 个 .ts 文件... 找到 12 处
        主对话收到: "找到 12 处,分布在 src/api/ 下的..."

Worktree 隔离

isolation: worktree 给 subagent 一份独立的 git worktree——它读写的是一份代码副本,不会污染主工作目录。

---
name: refactor-agent
description: Refactors code safely in isolation. Use when changes are experimental.
isolation: worktree
tools: Read, Write, Edit, Bash
model: sonnet
---

Perform the requested refactoring. Run tests after each change.
If tests fail, revert and try a different approach.

适合这个模式的场景:

  • 实验性重构,不确定方向对不对
  • 批量格式化或代码迁移(影响范围大,想先看结果再决定要不要合并)
  • 并行处理多个独立功能,各自在独立分支上

Worktree 清理规则:subagent 没有做任何变更时自动删除;有变更时保留,等你决定合并还是丢弃。

实战:几个常见的自定义 Subagent

只读代码审查

---
name: security-reviewer
description: >
  Reviews code for security vulnerabilities. Use after any change
  to authentication, API routes, database queries, or file I/O.
tools: Read, Grep, Glob
model: sonnet
color: red
---

You are a security engineer reviewing code for vulnerabilities.
Focus on: SQL/NoSQL injection, XSS, CSRF, insecure deserialization,
hardcoded secrets, path traversal, broken access control.

For each issue found:
1. File path and line number
2. Vulnerability type (OWASP category)
3. Why it's dangerous
4. Concrete fix with code snippet

If no issues, reply: "No security issues found."

测试生成器

---
name: test-writer
description: Generates unit tests for a given module or function.
tools: Read, Glob, Write, Bash
model: sonnet
maxTurns: 20
---

You write unit tests. When given a file or function:
1. Read the source code
2. Identify edge cases and failure modes
3. Write tests using the project's existing test framework
4. Run the tests and fix failures before returning

Place test files alongside the source (foo.ts → foo.test.ts).
Return a summary: how many tests, pass rate, any known gaps.

批量文档更新(worktree 隔离)

---
name: doc-updater
description: Updates JSDoc/docstring comments across a module. Safe to run on large changes.
tools: Read, Edit, Glob
model: haiku
isolation: worktree
maxTurns: 50
color: blue
---

Update all JSDoc comments in the specified files to accurately reflect
the current implementation. Do not change any logic, only comments.
Flag any functions with no tests as "⚠ untested" in their docstring.

用 CLI flag 临时定义 Subagent

不想写文件?可以在启动 Claude Code 时直接用 --agents 传 JSON,只对当前 session 生效:

claude --agents '{
  "db-migration-checker": {
    "description": "Validates database migrations for safety before applying.",
    "prompt": "Check the migration file for: backwards-incompatible changes, missing rollback, locks on large tables. Return a risk assessment.",
    "tools": ["Read"],
    "model": "sonnet"
  }
}'

适合 CI pipeline 或一次性任务,不需要持久化配置。

禁用特定 Subagent

不想让 Claude 自动委托给某个内置 subagent?在 settings.json 里 deny 它:

{
  "permissions": {
    "deny": ["Agent(explore)", "Agent(plan)"]
  }
}

要完全禁止 subagent 委托,直接 deny Agent 工具本身。

小结

场景 推荐做法
搜代码、读日志、探索文件 用内置 Explore subagent(自动触发)
多个独立任务并行 让 Claude 同时启动多个 subagent
只读审查,不允许修改 tools: Read, Grep, Glob,不加 Write/Edit
实验性修改不想污染工作目录 isolation: worktree
控制成本,探索用便宜模型 model: haiku for Explore,model: opus for 决策
防止 subagent 失控 加 maxTurns: N
跨 session 积累经验 memory: project 或 memory: user

Subagent 不是越多越好——每多一层委托就多一次上下文切换和摘要损耗。判断标准很简单:这个任务的中间过程,主对话以后还需要看吗? 不需要 → 扔给 subagent。需要 → 留在主对话。

作者
一键分享或复制链接
Lightman Wang
Reviewer: Lightman Wang

Founder of JR Academy

查看该作者的更多文章 →

相关学习资源

  • AI Engineer 学习路径
  • Vibe Coding Hub
  • Prompt 工程入门
← 上一篇Claude Code 从入门到实战 — 并行开发:Worktree 让多个 Claude 同时干活下一篇 →匠人学院使用指南 — 快速上手:5 分钟看懂匠人学院

相关文章推荐

太狠了太狠了😭 NVIDIA这个AI免费项目真的太狠了

2026-09-21

GitHub Copilot 实战指南 — 常见问题、定价与选型建议

2026-09-20

GitHub Copilot 实战指南 — 进阶玩法:Coding Agent、自定义指令与 MCP

2026-09-20

GitHub Copilot 实战指南 — 核心功能深度解析:补全、Chat 与 Agent Mode

2026-09-20

GitHub Copilot 实战指南 — 快速上手:注册免费版 + 第一次 AI 编程

2026-09-20

GitHub Copilot 实战指南 — GitHub Copilot 是什么:全球最大的 AI 编程助手

2026-09-20
查看全部文章 →
JR Academy
全球华人学习 AI 第一站
✓15000+ 学员
✓50+ 课程
✓AI 驱动学习平台
训练营免费资源AI学习职业辅导
精选推荐
AI 职业影响地图
测测你的职业风险等级,查看转型路径与学习方向
热门工具
AI & 数据训练营
系统化课程 + 真实项目实战,快速提升竞争力
热门课程
1v1 就业辅导
资深导师一对一指导,简历优化 + 面试准备
就业保障
企业内训定制
AI 技能培训方案,助力团队升级
企业服务
热门标签
Vibe CodingAI 编程CursorClaude求职攻略Prompt前端开发后端开发
订阅更新

获取最新 AI 学习资源、技术教程和求职攻略,直接送达邮箱。

我们尊重您的隐私,不会发送垃圾邮件