Visual Studio Code 中的子智能体 (Subagents)
在处理复杂任务时,您可以将子任务委托给子智能体。子智能体是一个独立的 AI 智能体,负责执行聚焦的工作(例如研究主题、分析代码或审查变更),并将结果汇报给主智能体。
有关子智能体概念(上下文隔离、同步与并行执行)的背景信息,请参阅智能体概念。
本文介绍了如何在 VS Code 中使用子智能体,包括使用场景、调用模式以及如何将自定义智能体作为子智能体运行。
用户视角
当子智能体运行时,它会在聊天窗口中以可折叠工具调用的形式显示。默认情况下,子智能体处于折叠状态,并显示
- 自定义智能体的名称(如果您指定了名称)
- 当前正在运行的工具(例如“正在读取文件...”或“正在搜索代码库...”)
选择子智能体工具调用即可展开并查看完整详情,包括子智能体执行的所有工具调用、传递给子智能体的提示词以及返回的结果。
这种可见性使您可以控制所查看的细节程度,而不会让主对话中充斥着中间步骤。
使用场景
以下场景说明了子智能体在何时可以改善您的 AI 辅助开发工作流。
实施前的研究
在构建新功能时,利用子智能体研究最佳实践、评估库或分析代码库中的现有模式,然后再由主智能体开始实施。
Perform isolated research into different OAuth 2.0 implementation patterns for Node.js applications.
Compare each against the current implementation and return a recommendation with pros and cons.
主智能体仅接收最终建议,从而保持其上下文的简洁,专注于实际的实施工作。
并行代码分析
在重构或审查代码时,可以并行运行多个子智能体以分析不同方面。
Analyze this codebase for refactoring opportunities. Perform these tasks in parallel:
1. Find duplicate code patterns
2. Identify unused exports and dead code
3. Review error handling consistency
4. Check for security vulnerabilities
Compile the findings into a prioritized action plan.
探索多种解决方案
当您不确定最佳方法时,可以使用子智能体探索不同的选项,而不会污染您的主要上下文。
I need to implement caching for this API. Do some isolated research on these three approaches:
1. Design a Redis-based caching solution
2. Design an in-memory caching solution with LRU eviction
3. Design a hybrid approach with tiered caching
Compare the results and recommend the best approach for our use case.
专注于特定领域的代码审查
使用自定义智能体作为子智能体,以应用不同的审查视角。
Review the changes in this PR from different angles. Perform these reviews in parallel:
- Run the security-reviewer agent to check for vulnerabilities
- Run the performance-reviewer agent to identify bottlenecks
- Run the accessibility-reviewer agent to verify a11y compliance
Consolidate findings into a single review summary.
多模型共识
使用不同模型的子智能体,对同一个问题获取多样化的视角。
I need to evaluate the error handling in our payment service.
Run two subagents in parallel, each with a different model:
1. Use GPT-4o to review the code for error handling gaps
2. Use Claude Sonnet 4.6 to review the code for error handling gaps
Compare their findings and highlight where they agree and disagree.
主智能体会收集所有结果并综合出汇总分析。
调用子智能体
智能体发起 vs 用户触发
子智能体通常是由智能体发起的,而不是由用户在聊天中直接调用的。要允许主智能体调用子智能体,请确保启用了 runSubagent 工具。
默认情况下,子智能体本身无法调用进一步的子智能体。要启用递归嵌套,请启用 chat.subagents.allowInvocationsFromSubagents 设置。了解更多信息,请参阅嵌套子智能体。
主智能体会决定何时需要上下文隔离。您无需为每个任务手动输入“运行子智能体”。该模式的工作方式如下:
- 您(或您的自定义智能体指令)描述一个复杂的任务。
- 主智能体识别出任务中受益于隔离上下文的部分。
- 智能体启动一个子智能体,仅传递相关的子任务。
- 子智能体自主工作并返回摘要。
- 主智能体整合结果并继续执行。
您可以通过调整提示词措辞来暗示需要子智能体委托,例如建议进行独立研究或并行分析。主智能体将启动子智能体、传递任务并仅接收最终结果。
为了实现一致的子智能体行为,请在自定义智能体的指令中定义何时使用子智能体,而不是每次都手动提示。
为了优化子智能体的性能,请清晰定义任务和预期输出。这有助于子智能体专注于特定目标,而不会将不必要的上下文传回给主智能体。
有关如何构建调用子智能体的提示词示例,请参阅使用场景章节。
在提示词文件中调用子智能体
要在提示词文件中调用子智能体,请确保 tools 的 frontmatter 属性中包含了 runSubagent 或 agent 工具。
---
name: document-feature
tools: ['agent', 'read', 'search', 'edit']
---
Run a subagent to research the new feature implementation details and return only information relevant for user documentation.
Then update the docs/ folder with the new documentation.
在提示词指令中,您可以通过建议对特定子任务进行独立研究或并行分析来引导智能体使用子智能体。
运行自定义智能体作为子智能体
默认情况下,子智能体继承主聊天会话的智能体,并使用相同的模型和工具。要为子智能体定义特定行为,请使用自定义智能体。自定义智能体可以指定自己的模型、工具和指令。当用作子智能体时,这些设置会覆盖从主会话继承的默认值。
主智能体在调用子智能体时也可以请求特定模型。了解更多信息,请参阅子智能体的模型选择章节。
控制子智能体调用
您可以通过两个 frontmatter 属性来控制自定义智能体如何被调用:
user-invocable:控制该智能体是否显示在聊天界面的智能体下拉菜单中(默认值为true)。设置为false可创建仅作为子智能体可访问的智能体。disable-model-invocation:防止该智能体被其他智能体作为子智能体调用(默认值为false)。当希望智能体仅由用户显式触发时,将其设置为true。
例如,要创建一个仅能作为子智能体使用的智能体(在下拉菜单中不可见):
---
name: internal-helper
user-invocable: false
---
This agent can only be invoked as a subagent.
infer 属性已弃用。请改用 user-invocable 和 disable-model-invocation 以实现更精细的控制。
要将自定义智能体作为子智能体运行,请提示 AI 使用自定义或内置智能体作为子智能体。例如:
运行“Research”智能体作为子智能体,来研究本项目最佳的身份验证方法。使用“Plan”智能体作为子智能体,为 myfeature 创建实施计划。然后将该计划保存到 plans/myfeature.plan.md。
子智能体的模型选择
当子智能体运行时,模型由以下优先顺序决定:
- 显式模型参数:主智能体在调用
runSubagent工具时直接指定模型。 - 智能体配置的模型:自定义智能体
.agent.mdfrontmatter 中的model属性。 - 主模型:运行父对话的模型。
要为子智能体请求特定模型,请在提示词中包含模型偏好:
运行使用 Claude Sonnet 4.6 的子智能体,以研究代码库中的身份验证模式。在子智能体中使用 GPT-4o 来分析该模块的性能。
您也可以在自定义智能体的指令中定义模型偏好,从而持续将子智能体任务路由到特定模型。
请求的模型成本不得超过主模型的成本等级。如果您请求更昂贵的模型,子智能体会回退到主模型。
限制可使用的子智能体(实验性)
默认情况下,所有未设置 disable-model-invocation: true 的自定义智能体均可用作子智能体。如果两个或多个智能体名称或描述相似,AI 可能会选择非预期的智能体。
您可以通过在主智能体的 frontmatter 中指定 agents 属性并提供允许的自定义智能体列表,来限制哪些自定义智能体可以用作子智能体。
agents 属性接受:
- 智能体名称列表(例如
['Edit', 'Search']),仅允许特定智能体。 *以允许所有可用智能体(默认行为)。- 空数组
[]以阻止使用任何子智能体。
在 agents 数组中显式列出智能体会覆盖 disable-model-invocation: true。这意味着您可以创建受保护的、无法被常规调用的智能体,但仍允许特定的协调智能体显式访问它们。
例如,测试驱动开发 (TDD) 智能体应仅使用 Red、Green 和 Refactor 智能体作为子智能体。如果不加以限制,TDD 智能体可能会选择更通用的编码智能体来实施测试,而不是专业的 TDD 智能体。
---
name: TDD
tools: ['agent']
agents: ['Red', 'Green', 'Refactor']
---
Implement the following feature using test-driven development. Use subagents to guide the following steps:
1. Use the Red agent to write failing tests
2. Use the Green agent to implement code to pass the tests
3. Use the Refactor agent to improve the code quality
嵌套子智能体
默认情况下,子智能体无法派生更多的子智能体。这防止了当智能体意外陷入循环调用自身时的无限递归。但是,某些工作流受益于递归委托,例如“分而治之”型智能体,它将大任务拆分为小部分,并将每个部分委托给自身的新实例。
要启用嵌套子智能体,请启用 chat.subagents.allowInvocationsFromSubagents 设置(默认为 false)。启用后,子智能体可以派生自己的子智能体,最大嵌套深度为 5 层。
示例:递归智能体
递归智能体在其自身的 agents 属性中列出自己。这启用了“分而治之”模式,智能体将问题分解为更小的部分,并将每个部分委托给自身的新实例。
---
name: RecursiveProcessor
tools: ['agent', 'read', 'search']
agents: [RecursiveProcessor]
argument-hint: A list of items to process
---
You process a list of items by dividing and conquering:
- If the list has more than 4 items, split it in half and delegate each half to a RecursiveProcessor subagent.
- If the list has 4 or fewer items, process the items directly.
- Merge the results from each subagent into a final result.
编排模式
子智能体实现了编排模式,即由协调者智能体将工作委托给专业的执行者智能体。这种方法有助于构建复杂的工程工作流,同时让每个智能体保持在其最擅长的领域。
协调者与执行者模式
协调者智能体管理整体任务并将子任务委托给专门的子智能体。每个执行者智能体都可以拥有量身定制的工具集。例如,规划和审查智能体只需只读访问权限,而实施者需要编辑能力。
---
name: Feature Builder
tools: ['agent', 'edit', 'search', 'read']
agents: ['Planner', 'Plan Architect', 'Implementer', 'Reviewer']
---
You are a feature development coordinator. For each feature request:
1. Use the Planner agent to break down the feature into tasks.
2. Use the Plan Architect agent to validate the plan against codebase patterns.
3. If the architect identifies reusable patterns or libraries, send feedback to the Planner to update the plan.
4. Use the Implementer agent to write the code for each task.
5. Use the Reviewer agent to check the implementation.
6. If the reviewer identifies issues, use the Implementer agent again to apply fixes.
Iterate between planning and architecture, and between review and implementation, until each phase converges.
每个执行者智能体都定义了自己的工具访问权限,并且由于任务范围较窄,可以选择速度更快或性价比更高的模型。
---
name: Planner
user-invocable: false
tools: ['read', 'search']
---
Break down feature requests into implementation tasks. Incorporate feedback from the Plan Architect.
---
name: Plan Architect
user-invocable: false
tools: ['read', 'search']
---
Validate plans against the codebase. Identify existing patterns, utilities, and libraries that should be reused. Flag any plan steps that duplicate existing functionality.
---
name: Implementer
user-invocable: false
model: ['Claude Haiku 4.5 (copilot)', 'Gemini 3 Flash (Preview) (copilot)']
---
Write code to complete assigned tasks.
这种模式使协调者的上下文专注于高级工作流,同时确保每个执行者智能体都有干净的上下文和适合其特定工作的适当权限。
多视角代码审查
代码审查受益于多个视角。单次审查往往会遗漏从不同角度审视时才会显现的问题。使用子智能体并行运行每个审查视角,然后合成调查结果。
---
name: Thorough Reviewer
tools: ['agent', 'read', 'search']
---
You review code through multiple perspectives simultaneously. Run each perspective as a parallel subagent so findings are independent and unbiased.
When asked to review code, run these subagents in parallel:
- Correctness reviewer: logic errors, edge cases, type issues.
- Code quality reviewer: readability, naming, duplication.
- Security reviewer: input validation, injection risks, data exposure.
- Architecture reviewer: codebase patterns, design consistency, structural alignment.
After all subagents complete, synthesize findings into a prioritized summary. Note which issues are critical versus nice-to-have. Acknowledge what the code does well.
这种模式之所以有效,是因为每个子智能体都会以全新的视角处理代码,不会受到其他视角的先入之见影响。在此示例中,协调者通过提示词塑造了每个子智能体的关注领域。这是一种轻量级方法,无需额外的智能体文件。
如需更多控制,每个审查视角都可以是具有专门工具访问权限的独立自定义智能体。例如,安全审查员可以使用面向安全的 MCP 服务器,而代码质量审查员可以使用 linting CLI 工具。这种方法让每个视角都能使用最适合其特定关注点的工具。