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.

呼叫次代理人

代理人啟動與使用者呼叫

次代理人通常是代理人啟動的,不直接由使用者在聊天中呼叫。為了允許主要代理人呼叫次代理人,請確保已啟用 runSubagent 工具。

預設情況下,次代理人本身無法呼叫進一步的次代理人。為了啟用遞迴巢狀,請啟用 chat.subagents.allowInvocationsFromSubagents 在 VS Code 中開啟 在 VS Code Insiders 中開啟 設定。在巢狀次代理人中了解更多。

主要代理人決定何時上下文隔離會有幫助。您無需為每個任務手動輸入「執行次代理人」。這種模式運作方式如下:

  1. 您(或您的自訂代理人指令)描述一個複雜的任務。
  2. 主要代理人識別出任務中可從隔離上下文受益的部分。
  3. 代理人啟動一個次代理人,只傳遞相關的子任務。
  4. 次代理人自主工作並返回摘要。
  5. 主要代理人整合結果並繼續。

您可以透過措辭您的提示,建議隔離研究或平行分析,來暗示您希望進行次代理人委派。主要代理人將啟動一個次代理人,將任務傳遞給它,並只接收最終結果。

提示

為了保持次代理人行為的一致性,請在您的自訂代理人指令中定義何時使用次代理人,而不是每次都手動提示它們。

為了最佳化次代理人效能,請清晰定義任務和預期輸出。這有助於次代理人專注於特定目標,而不會將不必要的上下文傳回主要代理人。

有關如何建構呼叫次代理人的提示範例,請參閱使用情境部分。

在提示檔案中呼叫次代理人

若要在提示檔案內部呼叫次代理人,請確保 runSubagentagent 工具包含在 tools 前言屬性中。

---
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.

在提示指令中,您可以透過建議對特定子任務進行隔離研究或平行分析,來提示代理人使用次代理人。

將自訂代理人作為次代理人執行 (實驗性)

預設情況下,次代理人繼承來自主要聊天會話的代理人,並使用相同的模型和工具。若要定義次代理人的特定行為,請使用自訂代理人。自訂代理人可以指定自己的模型、工具和指令。當作為次代理人使用時,這些設定會覆寫從主要會話繼承的預設值。

控制次代理人呼叫

您可以透過以下兩個前言屬性來控制自訂代理人的呼叫方式:

  • 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 使用自訂或內建代理人作為次代理人。例如:

  • 將 Research 代理人作為次代理人執行,以研究此專案的最佳驗證方法。
  • 在次代理人中使用 Plan 代理人為我的功能建立實作計畫。然後將計畫儲存到 plans/myfeature.plan.md

限制哪些次代理人可以使用 (實驗性功能)

預設情況下,所有未設定 disable-model-invocation: true 的自訂代理人都可以作為次代理人使用。如果兩個或多個代理人具有相似的名稱或描述,AI 可能會選取非預期的代理人。

您可以透過在主要代理人的前言中指定 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

巢狀次代理人

預設情況下,次代理人無法產生進一步的次代理人。這可以防止代理人在迴圈中意外呼叫自己時發生無限遞迴。然而,某些工作流程受益於遞迴委派,例如,一個分治代理人,它將一個大任務分解成小塊,並將每個小塊委派給自身的一個新實例。

為了啟用巢狀次代理人,請啟用 chat.subagents.allowInvocationsFromSubagents 在 VS Code 中開啟 在 VS Code Insiders 中開啟 設定(預設為 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 工具的存取權限。這種方法讓每個觀點都能使用最適合其特定重點的最佳工具。

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