– 代理会话日,2月19日

Visual Studio Code 中的子代理

在处理复杂任务时,您可以将子任务委派给子代理。子代理是一个独立的 AI 代理,执行专注的工作,例如研究主题、分析代码或审查更改,并将结果报告给主代理。由于每个子代理都在自己的上下文窗口中运行,因此不会向主对话中添加噪音。VS Code 还可以并行运行多个子代理,以加快多部分任务的速度。

例如,内置的 计划代理 使用子代理执行研究和分析,然后再创建实施计划。每个子代理自主工作并仅返回其发现。计划代理将这些发现综合成最终计划。

默认情况下,子代理使用与主聊天会话相同的模型和工具,但从干净的上下文窗口开始。子代理不会继承主代理的指令或对话历史记录。它们仅接收您提供的任务提示。通过将 自定义代理 作为子代理运行,您可以为特定任务应用专门的行为、工具和模型。

子代理执行方式

下图显示了子代理的工作方式。主代理接收您的任务,将子任务委派给一个或多个子代理,每个子代理都在自己的上下文窗口中运行,然后组合结果。

Diagram that shows the subagent execution flow where the main agent delegates subtasks to subagents running in isolated context windows and receives result summaries back.

子代理是同步的:主代理在继续之前等待子代理的结果。这种阻塞行为是故意的:子代理的发现通常会告知任务的下一步。如果没有子代理的结果,主代理就缺乏有效进行所需的必要信息。

但是,VS Code 可以生成多个并行子代理。当您请求并行分析时(例如,“同时分析安全性、性能和可访问性”),VS Code 会并行运行这些子代理,并在主代理继续之前等待所有结果。

注意

子代理与启动新的代理会话不同。新的会话会创建与当前任务没有任何联系的完全独立的对话。子代理保持关系:它们执行专注的工作并向主代理报告,主代理控制整个任务。

用户看到的内容

当子代理运行时,它会以可折叠的工具调用形式出现在聊天中。默认情况下,子代理是折叠的,并显示

  • 自定义代理的名称(如果指定了的话)
  • 当前正在运行的工具(例如,“正在读取文件...”或“正在搜索代码库...”)

选择子代理工具调用以展开它并查看完整详细信息,包括子代理发出的所有工具调用、传递给子代理的提示以及返回的结果。

这种可见性使您能够控制所看到的详细程度,而不会用中间步骤使主对话混乱。

为什么使用子代理?

子代理帮助您更有效地管理复杂的 AI 辅助工作流程

  • 保持主代理上下文专注:主代理的上下文窗口会积累来自每个提示和响应的信息。通过将研究、分析或实施任务卸载到子代理,您可以防止上下文膨胀并帮助主代理专注于协调整个任务。

  • 通过并行执行提高性能:VS Code 可以同时运行多个子代理。例如,在实施功能时,您可以并行研究身份验证模式、分析现有代码结构和查看文档,而不是按顺序进行。

  • 隔离实验性或探索性工作:子代理非常适合您希望在不承诺方向的情况下探索选项的任务。如果子代理的研究陷入僵局,只有最终摘要会影响您的主上下文 - 而不是所有的中间探索。

  • 为特定任务应用专门的行为:通过将子代理与 自定义代理 结合使用,您可以为特定子任务应用专门的工具、指令和模型。例如,使用以安全为中心的自定义代理来审查代码是否存在漏洞,而文档代理生成用户指南。

  • 减少令牌使用量和成本:由于子代理有自己的上下文窗口,因此它们不会将其完整的对话历史记录添加到主代理的上下文中。仅返回最终结果,这可以显著减少复杂任务的整体令牌消耗。

使用场景

以下场景说明了何时可以使用子代理来改进您的 AI 辅助开发工作流程。

实施前的研究

在构建新功能时,使用子代理研究最佳实践、评估库或分析代码库中的现有模式,然后再让主代理开始实施

Use a subagent to research OAuth 2.0 implementation patterns for Node.js applications.
Compare passport.js vs auth0 vs custom implementation. Return a recommendation with pros and cons.

主代理仅接收最终建议,保持其上下文清洁,以便进行实际实施工作。

并行代码分析

在重构或审查代码时,并行运行多个子代理以分析不同的方面

Analyze this codebase for refactoring opportunities. Use subagents to:
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. Run three subagents in parallel to:
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 using subagents:
- 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.

调用子代理

代理发起与用户发起

子代理通常是代理发起的,而不是由用户在聊天中直接发起的。要允许主代理调用子代理,请确保 runSubagent 工具已启用。

主代理决定何时隔离上下文有所帮助。您不需要手动键入“运行子代理”来执行每个任务。该模式的工作方式如下

  1. 您(或您的自定义代理指令)描述一个复杂的任务。
  2. 主代理识别任务中受益于隔离上下文的部分。
  3. 代理启动一个子代理,仅传递相关的子任务。
  4. 子代理自主工作并返回摘要。
  5. 主代理合并结果并继续。

您可以通过将提示措辞为暗示隔离研究或并行分析来提示您希望进行子代理委派。主代理将启动一个子代理,将任务传递给它,并仅接收最终结果。

提示

为了保持一致的子代理行为,请在自定义代理的指令中定义何时使用子代理,而不是每次都手动提示它们。

为了优化子代理性能,请明确定义任务和预期输出。这有助于子代理专注于特定目标,而不会将不必要的上下文传递回主代理。

请参阅 使用场景 部分,了解如何构建调用子代理的提示的示例。

在提示文件中调用子代理

要在提示文件中调用子代理,请确保 runSubagentagent 工具包含在 tools frontmatter 属性中

---
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-invocabledisable-model-invocation 以获得更精细的控制。

要将自定义代理作为子代理运行,请提示 AI 使用自定义或内置代理作为子代理。例如

  • 将研究代理作为子代理运行,以研究此项目的最佳身份验证方法。
  • 在子代理中使用计划代理来为 myfeature 创建实施计划。然后将计划保存在 plans/myfeature.plan.md 中

限制可用的子代理(实验性)

默认情况下,所有没有 disable-model-invocation: true 的自定义代理都可以用作子代理。如果两个或多个代理具有相似的名称或描述,则 AI 可能会选择意外的代理。

您可以通过在主代理的 frontmatter 中指定 agents 属性并提供允许的自定义代理列表来限制哪些自定义代理可以用作子代理。

agents 属性接受

  • 代理名称列表(例如 ['Edit', 'Search'])以仅允许特定代理
  • * 以允许所有可用代理(默认行为)
  • 空数组 [] 以防止任何子代理使用
注意

agents 数组中显式列出代理会覆盖 disable-model-invocation: true。这意味着您可以创建受常规子代理使用保护的代理,但仍然可以被明确允许它们的特定协调器代理访问。

例如,测试驱动开发 (TDD) 代理应仅使用 RedGreenRefactor 代理作为子代理。如果未受限制,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

编排模式

子代理支持编排模式,其中协调器代理将工作委派给专门的工作代理。这种方法可帮助您构建复杂的工作流程,同时让每个代理专注于它最擅长的事情。

协调器和工作者模式

协调器代理管理整个任务并将子任务委派给专门的子代理。每个工作者代理可以拥有一套定制的工具。例如,计划和审查代理只需要只读访问权限,而实施者需要编辑功能。

---
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 工具。这种方法允许每个视角使用最适合其特定关注点的工具。

© . This site is unofficial and not affiliated with Microsoft.