<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Worktree &#8211; 小人物看世界</title>
	<atom:link href="https://blog.che-ya.com/tag/worktree/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.che-ya.com</link>
	<description>軟體工程師的技術筆記</description>
	<lastBuildDate>Wed, 08 Apr 2026 08:05:35 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.che-ya.com/wp-content/uploads/2021/08/cropped-APP_icon-32x32.png</url>
	<title>Worktree &#8211; 小人物看世界</title>
	<link>https://blog.che-ya.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Multi-agent 架構：Subagents 與任務分派</title>
		<link>https://blog.che-ya.com/claude-code-multi-agent-subagents/</link>
		
		<dc:creator><![CDATA[ㄚ槌]]></dc:creator>
		<pubDate>Sat, 18 Apr 2026 02:25:00 +0000</pubDate>
				<category><![CDATA[Claude Code 進階]]></category>
		<category><![CDATA[Claude Code]]></category>
		<category><![CDATA[AI 編程工具]]></category>
		<category><![CDATA[Anthropic]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Multi-agent]]></category>
		<category><![CDATA[Subagent]]></category>
		<category><![CDATA[Worktree]]></category>
		<guid isPermaLink="false">https://blog.che-ya.com/?p=774</guid>

					<description><![CDATA[當你的專案規模越來越大，一個 Claude Code 對話要同時處理搜尋程式碼、撰寫測試、執行部署等多項任務時 ... <a title="Multi-agent 架構：Subagents 與任務分派" class="read-more" href="https://blog.che-ya.com/claude-code-multi-agent-subagents/" aria-label="Read more about Multi-agent 架構：Subagents 與任務分派">閱讀全文</a>]]></description>
										<content:encoded><![CDATA[
<p>當你的專案規模越來越大，一個 Claude Code 對話要同時處理搜尋程式碼、撰寫測試、執行部署等多項任務時，Context Window 很快就會被塞滿，效率也會大幅下降。Claude Code 的 Multi-agent 架構透過 Subagents（子代理）機制，讓你可以將複雜任務拆分給多個獨立的 Agent 分別執行，每個 Subagent 擁有自己的 Context Window、工具權限和專屬系統提示。主 Agent 只需接收精簡的執行結果，大幅節省 Token 用量，同時提升任務品質與執行效率。</p>



<h2 class="wp-block-heading">什麼是 Subagents</h2>



<p>Subagents 是在 Claude Code 主對話中產生的子代理程序，每個 Subagent 擁有獨立的 Context Window，在自己的環境中完成指定任務後，將精簡的結果回傳給主 Agent。你可以把它想像成一個團隊主管（主 Agent）將不同工作分配給專業成員（Subagents），每位成員各自完成工作後回報結果，主管不需要了解每個人的所有工作細節。</p>



<p>這個機制帶來三大核心優勢：</p>



<ul class="wp-block-list">
<li><strong>Context 隔離</strong>：Subagent 的詳細執行過程（如搜尋了哪些檔案、讀取了哪些內容）留在自己的 Context 中，只有最終結果回傳主 Agent，大幅減少主對話的 Token 消耗</li>



<li><strong>專業分工</strong>：每個 Subagent 可以擁有不同的工具權限、系統提示和模型設定，針對特定任務進行最佳化</li>



<li><strong>並行執行</strong>：多個 Subagent 可以同時執行不同任務，縮短整體工作時間</li>
</ul>



<h2 class="wp-block-heading">內建的 Subagent 類型</h2>



<p>Claude Code 預設提供四種內建的 Subagent 類型，各自適合不同的使用場景：</p>



<figure class="wp-block-table"><table><thead><tr><th>Subagent 類型</th><th>預設模型</th><th>可用工具</th><th>適用場景</th></tr></thead><tbody><tr><td><strong>Explore</strong></td><td>Haiku</td><td>唯讀（Glob、Grep、Read、Bash）</td><td>快速搜尋程式碼、探索專案結構</td></tr><tr><td><strong>Plan</strong></td><td>繼承主 Agent</td><td>唯讀</td><td>研究階段、規劃實作方案</td></tr><tr><td><strong>general-purpose</strong></td><td>繼承主 Agent</td><td>所有工具</td><td>複雜的多步驟任務</td></tr><tr><td><strong>Claude Code Guide</strong></td><td>Haiku</td><td>知識查詢</td><td>回答 Claude Code 功能相關問題</td></tr></tbody></table></figure>



<p>其中 <strong>Explore</strong> 是最常被自動使用的類型。當 Claude Code 需要在大型程式庫中搜尋特定程式碼時，它會自動產生一個 Explore Subagent 來執行搜尋，避免將大量搜尋結果塞入主 Agent 的 Context Window。</p>



<h2 class="wp-block-heading">如何觸發 Subagent</h2>



<p>觸發 Subagent 有幾種方式：</p>



<h3 class="wp-block-heading">自然語言指派</h3>



<p>最簡單的方式是在對話中用自然語言描述你想要的分工：</p>



<pre class="wp-block-code"><code class=""># 讓 Claude 自動選擇適合的 Subagent 類型
用 subagent 分別搜尋 authentication、database 和 API 模組的程式碼結構

# 明確指定使用特定類型
使用 Explore subagent 搜尋所有包含 TODO 的檔案</code></pre>



<h3 class="wp-block-heading">@ 提及方式</h3>



<p>如果你有自訂的 Subagent，可以使用 <code>@</code> 符號直接指定：</p>



<pre class="wp-block-code"><code class="">@code-reviewer 請檢查 src/auth/ 目錄下的所有檔案</code></pre>



<h3 class="wp-block-heading">CLI 啟動整個 Session</h3>



<p>使用 <code>--agent</code> 參數可以讓整個 Claude Code Session 以特定 Agent 身份運行：</p>



<pre class="wp-block-code"><code class="">claude --agent code-reviewer</code></pre>



<h2 class="wp-block-heading">自訂 Subagent</h2>



<p>除了內建類型，你可以建立完全客製化的 Subagent 來滿足團隊的特定需求。自訂 Subagent 定義檔使用 Markdown 格式搭配 YAML frontmatter，存放在以下位置：</p>



<figure class="wp-block-table"><table><thead><tr><th>存放位置</th><th>適用範圍</th><th>說明</th></tr></thead><tbody><tr><td><code>.claude/agents/</code></td><td>當前專案</td><td>可加入版本控制與團隊共享</td></tr><tr><td><code>~/.claude/agents/</code></td><td>所有專案</td><td>個人全域使用</td></tr><tr><td>Plugin 的 <code>agents/</code> 目錄</td><td>啟用該 Plugin 的專案</td><td>隨 Plugin 安裝</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">Subagent 定義檔範例</h3>



<p>以下是一個 Code Reviewer Subagent 的完整定義範例：</p>



<pre class="wp-block-code"><code class=""># 檔案路徑：.claude/agents/code-reviewer.md
---
name: code-reviewer
description: 專業程式碼審查員。主動檢查程式碼品質、安全性和可維護性。
tools: Read, Grep, Glob, Bash
disallowedTools: Write, Edit
model: sonnet
permissionMode: dontAsk
maxTurns: 50
memory: project
---

你是一位資深程式碼審查員，專注於以下面向：
1. 程式碼品質：命名規範、函式長度、重複程式碼
2. 安全性：SQL Injection、XSS、敏感資料外洩
3. 效能：不必要的迴圈、N+1 查詢、記憶體洩漏
4. 可維護性：註解品質、模組化程度、測試覆蓋率

請用繁體中文回報審查結果，並以嚴重程度排序。</code></pre>



<h3 class="wp-block-heading">Frontmatter 設定欄位完整參考</h3>



<figure class="wp-block-table"><table><thead><tr><th>欄位</th><th>類型</th><th>預設值</th><th>說明</th></tr></thead><tbody><tr><td><code>name</code></td><td>string</td><td>必填</td><td>唯一識別名稱（小寫）</td></tr><tr><td><code>description</code></td><td>string</td><td>必填</td><td>描述何時應該委派給此 Agent</td></tr><tr><td><code>tools</code></td><td>array</td><td>繼承全部</td><td>允許使用的工具白名單</td></tr><tr><td><code>disallowedTools</code></td><td>array</td><td>無</td><td>禁止使用的工具黑名單</td></tr><tr><td><code>model</code></td><td>string</td><td>繼承</td><td>使用的模型（sonnet、opus、haiku）</td></tr><tr><td><code>permissionMode</code></td><td>string</td><td>繼承</td><td>權限模式（default、auto、dontAsk 等）</td></tr><tr><td><code>maxTurns</code></td><td>integer</td><td>無限制</td><td>最大執行回合數</td></tr><tr><td><code>memory</code></td><td>string</td><td>無</td><td>持久記憶範圍（user、project、local）</td></tr><tr><td><code>isolation</code></td><td>string</td><td>無</td><td>設為 worktree 啟用 Git Worktree 隔離</td></tr><tr><td><code>background</code></td><td>boolean</td><td>false</td><td>是否在背景執行</td></tr><tr><td><code>effort</code></td><td>string</td><td>繼承</td><td>推理深度（low、medium、high、max）</td></tr><tr><td><code>skills</code></td><td>array</td><td>無</td><td>啟動時注入的 Skill 內容</td></tr><tr><td><code>mcpServers</code></td><td>array</td><td>無</td><td>可使用的 MCP Server</td></tr><tr><td><code>hooks</code></td><td>object</td><td>無</td><td>生命週期事件掛鉤</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">工具權限控制</h2>



<p>Subagent 的工具權限控制是安全性的關鍵。你可以使用白名單或黑名單模式來精確控制每個 Subagent 能使用哪些工具。</p>



<h3 class="wp-block-heading">白名單模式</h3>



<p>明確指定 Subagent 可以使用的工具：</p>



<pre class="wp-block-code"><code class="">---
tools: Read, Grep, Glob, Bash
---</code></pre>



<p>這種模式適合需要嚴格限制的場景，例如只讀的程式碼審查 Subagent。</p>



<h3 class="wp-block-heading">黑名單模式</h3>



<p>繼承主 Agent 的所有工具，但排除特定工具：</p>



<pre class="wp-block-code"><code class="">---
disallowedTools: Write, Edit
---</code></pre>



<p>這種模式適合大部分工具都需要使用，只需排除少數高風險工具的情況。</p>



<h3 class="wp-block-heading">限制可產生的 Subagent</h3>



<p>你甚至可以控制一個 Subagent 能夠產生哪些其他 Subagent：</p>



<pre class="wp-block-code"><code class="">---
tools: Agent(worker, researcher), Read, Bash
---</code></pre>



<p>上面的設定只允許該 Subagent 產生名為 <code>worker</code> 和 <code>researcher</code> 的子代理。</p>



<h2 class="wp-block-heading">權限模式詳解</h2>



<p>每個 Subagent 可以設定不同的權限模式，決定它在執行工具時如何處理權限請求：</p>



<figure class="wp-block-table"><table><thead><tr><th>模式</th><th>行為</th><th>適用場景</th></tr></thead><tbody><tr><td><code>default</code></td><td>標準權限檢查，需要使用者確認</td><td>需要互動確認的工作</td></tr><tr><td><code>acceptEdits</code></td><td>自動接受檔案編輯（受保護目錄除外）</td><td>受信任的自動化修改</td></tr><tr><td><code>auto</code></td><td>背景分類器自動審核並批准</td><td>高量操作</td></tr><tr><td><code>dontAsk</code></td><td>自動拒絕所有權限請求</td><td>純唯讀操作</td></tr><tr><td><code>bypassPermissions</code></td><td>跳過所有權限檢查</td><td>完全受信任的操作（謹慎使用）</td></tr><tr><td><code>plan</code></td><td>唯讀探索模式</td><td>僅限研究的 Agent</td></tr></tbody></table></figure>



<p>權限模式有繼承規則：如果主 Agent 使用 <code>bypassPermissions</code>，則該設定具有最高優先權，Subagent 無法覆寫；如果主 Agent 使用 <code>auto</code> 模式，Subagent 也會繼承 auto 模式。</p>



<h2 class="wp-block-heading">Worktree 隔離</h2>



<p>當多個 Subagent 同時對程式碼進行修改時，可能會產生檔案衝突。Worktree 隔離機制透過 Git Worktree 為每個 Subagent 提供獨立的工作目錄副本，避免互相干擾。</p>



<h3 class="wp-block-heading">啟用方式</h3>



<p>在 Subagent 定義的 frontmatter 中加入 <code>isolation: worktree</code>：</p>



<pre class="wp-block-code"><code class="">---
name: parallel-worker
description: 可並行執行的工作者
isolation: worktree
---</code></pre>



<h3 class="wp-block-heading">Worktree 運作機制</h3>



<ul class="wp-block-list">
<li>Subagent 啟動時會自動建立一個新的 Git Worktree checkout</li>



<li>所有檔案修改都在這個獨立的 Worktree 中進行，不會影響主 Session 或其他 Subagent</li>



<li>如果 Subagent 沒有做任何變更，Worktree 會自動清除</li>



<li>如果 Subagent 提交了變更，Worktree 會保留，讓你可以檢視或合併這些變更</li>
</ul>



<p>Worktree 隔離特別適合需要多個 Subagent 同時修改不同模組的場景，例如一個 Subagent 負責重構 API 層，另一個 Subagent 同時處理資料庫遷移。</p>



<h2 class="wp-block-heading">持久記憶機制</h2>



<p>Subagent 可以透過持久記憶（Persistent Memory）在不同 Session 之間保留知識。這讓 Subagent 能夠累積專案相關的經驗，隨著時間變得越來越「了解」你的專案。</p>



<h3 class="wp-block-heading">記憶範圍</h3>



<figure class="wp-block-table"><table><thead><tr><th>範圍</th><th>存放位置</th><th>用途</th></tr></thead><tbody><tr><td><code>user</code></td><td><code>~/.claude/agent-memory/&lt;agent-name&gt;/</code></td><td>跨所有專案的通用知識</td></tr><tr><td><code>project</code></td><td><code>.claude/agent-memory/&lt;agent-name&gt;/</code></td><td>專案特定知識，可透過版本控制共享</td></tr><tr><td><code>local</code></td><td><code>.claude/agent-memory-local/&lt;agent-name&gt;/</code></td><td>專案特定知識，不加入版本控制</td></tr></tbody></table></figure>



<p>啟用持久記憶後，Subagent 的 <code>MEMORY.md</code> 檔案前 200 行（或 25KB）會在啟動時自動注入到 Context 中。Subagent 會自動管理記憶內容的大小，確保最重要的資訊被保留。</p>



<pre class="wp-block-code"><code class="">---
name: code-reviewer
memory: project
---</code></pre>



<h2 class="wp-block-heading">前景與背景執行</h2>



<p>Subagent 支援兩種執行模式：</p>



<h3 class="wp-block-heading">前景執行（預設）</h3>



<ul class="wp-block-list">
<li>主對話會等待 Subagent 完成才繼續</li>



<li>權限請求會傳遞給使用者確認</li>



<li>適合需要互動回饋的場景</li>
</ul>



<h3 class="wp-block-heading">背景執行</h3>



<p>在 Subagent 定義中加入 <code>background: true</code> 可啟用背景執行：</p>



<pre class="wp-block-code"><code class="">---
name: test-runner
description: 在背景執行測試套件
background: true
---</code></pre>



<ul class="wp-block-list">
<li>Subagent 與主對話並行執行</li>



<li>只能使用預先核准的權限（啟動後不再提示）</li>



<li>未預先核准的工具呼叫會自動失敗（但 Subagent 會繼續執行）</li>



<li>適合高量並行操作，如執行測試、批次搜尋</li>
</ul>



<p>你也可以在 Subagent 執行中按下 <code>Ctrl+B</code> 將前景 Subagent 切換到背景執行，或者在對話中說「在背景執行」來觸發背景模式。</p>



<h2 class="wp-block-heading">並行任務執行模式</h2>



<p>Multi-agent 架構最強大的應用之一就是並行任務執行。以下是幾種常見的並行模式：</p>



<h3 class="wp-block-heading">模式一：多個研究 Subagent</h3>



<p>同時派出多個 Subagent 探索不同模組：</p>



<pre class="wp-block-code"><code class=""># 在 Claude Code 對話中輸入：
請分別用 subagent 搜尋以下三個模組的程式碼結構：
1. authentication 模組
2. database 模組
3. API 模組
每個用獨立的 subagent 並行處理</code></pre>



<p>每個 Subagent 獨立探索，最後由主 Agent 整合所有結果。</p>



<h3 class="wp-block-heading">模式二：鏈式 Subagent</h3>



<p>一個 Subagent 的輸出作為下一個的輸入：</p>



<pre class="wp-block-code"><code class=""># 先用 code-reviewer 找出問題，再用 optimizer 修復
先用 code-reviewer subagent 找出效能問題，
然後用 optimizer subagent 修復這些問題</code></pre>



<h3 class="wp-block-heading">模式三：高量隔離操作</h3>



<p>將產生大量輸出的操作隔離在 Subagent 中：</p>



<pre class="wp-block-code"><code class=""># 測試套件的輸出留在 Subagent 中，只回傳摘要
用 subagent 執行測試套件，只回報失敗的測試和錯誤訊息</code></pre>



<p>這種模式能有效避免冗長的測試輸出塞滿主 Agent 的 Context Window。</p>



<h2 class="wp-block-heading">Subagent 生命週期 Hooks</h2>



<p>你可以透過 Hooks 在 Subagent 的生命週期中插入自訂邏輯，分為兩個層級：</p>



<h3 class="wp-block-heading">Subagent 層級 Hooks（在 frontmatter 中定義）</h3>



<pre class="wp-block-code"><code class="">---
name: db-migrator
hooks:
  PreToolUse:
    - matcher: "Bash"
      hooks:
        - type: command
          command: "./scripts/validate-sql.sh"
  PostToolUse:
    - matcher: "Edit|Write"
      hooks:
        - type: command
          command: "./scripts/run-linter.sh"
  Stop:
    - matcher: ""
      hooks:
        - type: command
          command: "./scripts/cleanup.sh"
---</code></pre>



<h3 class="wp-block-heading">專案層級 Hooks（在 settings.json 中定義）</h3>



<pre class="wp-block-code"><code class="">{
  "hooks": {
    "SubagentStart": [
      {
        "matcher": "db-agent",
        "hooks": [
          {"type": "command", "command": "./scripts/setup-db.sh"}
        ]
      }
    ],
    "SubagentStop": [
      {
        "hooks": [
          {"type": "command", "command": "./scripts/log-completion.sh"}
        ]
      }
    ]
  }
}</code></pre>



<p>專案層級的 <code>SubagentStart</code> 和 <code>SubagentStop</code> 事件可以用 <code>matcher</code> 過濾特定的 Subagent 名稱，讓你針對不同 Subagent 執行不同的初始化或清理邏輯。</p>



<h2 class="wp-block-heading">Agent Teams：多 Session 協作（實驗性功能）</h2>



<p>除了單一 Session 內的 Subagent，Claude Code 還提供了實驗性的 Agent Teams 功能，允許多個獨立的 Claude Code Session 協同工作。</p>



<h3 class="wp-block-heading">啟用方式</h3>



<pre class="wp-block-code"><code class=""># 設定環境變數啟用 Agent Teams
export CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1</code></pre>



<h3 class="wp-block-heading">Subagent vs Agent Teams 比較</h3>



<figure class="wp-block-table"><table><thead><tr><th>特性</th><th>Subagent</th><th>Agent Teams</th></tr></thead><tbody><tr><td>架構</td><td>單一 Session 內的子代理</td><td>多個獨立 Session 協作</td></tr><tr><td>溝通方式</td><td>只回報結果給主 Agent</td><td>成員之間可直接訊息溝通</td></tr><tr><td>共享資源</td><td>無</td><td>共享任務清單和訊息系統</td></tr><tr><td>Token 成本</td><td>較低（結果摘要回傳）</td><td>較高（每個成員獨立 Context）</td></tr><tr><td>適用場景</td><td>獨立、專注的子任務</td><td>需要跨成員討論的複雜協作</td></tr><tr><td>穩定性</td><td>正式功能</td><td>實驗性功能</td></tr></tbody></table></figure>



<p>Agent Teams 適合需要多個 Agent 互相討論和協作的大型任務，例如讓一個 Agent 專注 UX 設計、一個負責技術架構、一個扮演魔鬼代言人來挑戰方案。但由於 Token 成本較高且功能仍在實驗階段，建議大多數場景優先使用 Subagent 模式。</p>



<h2 class="wp-block-heading">實戰範例：建立完整的 Code Review 工作流</h2>



<p>以下是一個使用多個 Subagent 組成完整 Code Review 工作流的實際範例：</p>



<h3 class="wp-block-heading">步驟一：建立安全檢查 Subagent</h3>



<pre class="wp-block-code"><code class=""># .claude/agents/security-checker.md
---
name: security-checker
description: 安全性漏洞掃描專家。檢查常見安全漏洞如 SQL Injection、XSS、CSRF。
tools: Read, Grep, Glob
model: sonnet
permissionMode: dontAsk
memory: project
---

你是一位資安專家，專注於檢查以下安全漏洞：
- SQL Injection
- Cross-Site Scripting (XSS)
- Cross-Site Request Forgery (CSRF)
- 敏感資料硬編碼（API Key、密碼等）
- 不安全的依賴套件

回報格式：
1. 嚴重程度（Critical/High/Medium/Low）
2. 檔案路徑和行號
3. 問題描述
4. 建議修復方式</code></pre>



<h3 class="wp-block-heading">步驟二：建立效能分析 Subagent</h3>



<pre class="wp-block-code"><code class=""># .claude/agents/performance-analyzer.md
---
name: performance-analyzer
description: 效能分析專家。找出潛在的效能瓶頸和最佳化機會。
tools: Read, Grep, Glob, Bash
model: sonnet
permissionMode: dontAsk
---

你是一位效能最佳化專家，專注於：
- N+1 查詢問題
- 不必要的迴圈和重複計算
- 記憶體洩漏風險
- 未最佳化的資料庫查詢
- 可快取但未快取的操作</code></pre>



<h3 class="wp-block-heading">步驟三：在對話中組合使用</h3>



<pre class="wp-block-code"><code class=""># 在 Claude Code 對話中這樣指示：
請幫我做完整的 Code Review，使用以下流程：
1. 先用 security-checker 檢查安全漏洞
2. 同時用 performance-analyzer 分析效能問題
3. 最後幫我彙整兩份報告，按嚴重程度排序產出一份完整的 Review 報告</code></pre>



<p>Claude Code 會自動並行執行兩個 Subagent，然後整合它們的結果。由於每個 Subagent 的詳細搜尋過程都留在各自的 Context 中，主 Agent 只需要處理精簡的結果報告，整個過程既高效又節省 Token。</p>



<h2 class="wp-block-heading">相關環境變數</h2>



<figure class="wp-block-table"><table><thead><tr><th>環境變數</th><th>說明</th></tr></thead><tbody><tr><td><code>CLAUDE_CODE_SUBAGENT_MODEL</code></td><td>覆寫所有 Subagent 的預設模型</td></tr><tr><td><code>CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS</code></td><td>設為 1 啟用 Agent Teams 功能</td></tr><tr><td><code>CLAUDE_CODE_DISABLE_BACKGROUND_TASKS</code></td><td>設為 1 禁用背景執行</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">Subagent 最佳實踐</h2>



<ul class="wp-block-list">
<li><strong>每個 Subagent 專注一項任務</strong>：將 Subagent 設計為專注於單一職責（如 code-reviewer、debugger、researcher），而非萬用型 Agent。專注的 Subagent 效果更好，也更容易維護</li>



<li><strong>撰寫詳細的 description</strong>：Claude Code 會根據 description 決定何時自動委派任務給 Subagent，描述越精確，自動分派就越準確</li>



<li><strong>最小化工具權限</strong>：只授予 Subagent 實際需要的工具。唯讀的 Subagent 就不要給 Write 權限，這既提升安全性也讓 Subagent 更專注</li>



<li><strong>善用持久記憶</strong>：對於經常使用的 Subagent（如 code-reviewer），啟用 project 層級的持久記憶，讓它累積對專案的了解</li>



<li><strong>將高量輸出隔離</strong>：任何會產生大量輸出的操作（如執行測試、搜尋日誌）都應該放在 Subagent 中執行，只回傳摘要結果</li>



<li><strong>加入版本控制</strong>：將專案層級的 Subagent 定義（<code>.claude/agents/</code>）加入 Git，讓團隊成員都能使用同一套 Subagent 設定</li>



<li><strong>需要並行修改時使用 Worktree</strong>：當多個 Subagent 可能修改同一個專案的不同檔案時，啟用 <code>isolation: worktree</code> 避免衝突</li>



<li><strong>從簡單開始</strong>：先用內建的 Explore Subagent 體驗 Multi-agent 的效果，確認理解運作方式後再建立自訂 Subagent</li>
</ul>



<h2 class="wp-block-heading">常見問題</h2>



<h3 class="wp-block-heading">Subagent 可以再產生 Subagent 嗎？</h3>



<p>預設情況下 Subagent 不能巢狀產生其他 Subagent。如果需要，你可以在工具白名單中明確允許 <code>Agent</code> 工具，並指定可產生的 Subagent 名稱。</p>



<h3 class="wp-block-heading">Subagent 的結果如何回傳？</h3>



<p>Subagent 完成後，它的最後一則回應會作為結果回傳給主 Agent。Subagent 執行過程中的所有中間步驟（搜尋、讀檔等）都留在 Subagent 自己的 Context 中，不會佔用主 Agent 的 Token。</p>



<h3 class="wp-block-heading">可以恢復已完成的 Subagent 嗎？</h3>



<p>可以。你可以透過 <code>SendMessage</code> 工具指定 Subagent 的 ID 來恢復已完成的 Subagent，它會保留完整的對話歷史和狀態。</p>



<h3 class="wp-block-heading">如何控制 Subagent 使用的模型？</h3>



<p>模型選擇的優先順序為：環境變數 <code>CLAUDE_CODE_SUBAGENT_MODEL</code> &gt; 呼叫時的 <code>model</code> 參數 &gt; Subagent frontmatter 的 <code>model</code> 欄位 &gt; 繼承主 Agent 的模型。</p>



<h2 class="wp-block-heading">總結</h2>



<p>Claude Code 的 Multi-agent 架構是處理大型專案和複雜任務的利器。透過 Subagent 機制，你可以將工作拆分給多個專業的子代理分別執行，每個 Subagent 在自己的 Context Window 中獨立運作，只將精簡結果回傳主 Agent。結合 Worktree 隔離、持久記憶和生命週期 Hooks，你可以打造出高效且安全的自動化工作流程。</p>



<p>建議從使用內建的 Explore Subagent 開始，體會 Context 隔離帶來的效率提升。接著根據團隊需求建立自訂的 Code Review、安全檢查等專業 Subagent，逐步建立完整的 Multi-agent 工作流程。</p>



<h2 class="wp-block-heading">延伸閱讀</h2>



<ul class="wp-block-list">
<li><a href="https://blog.che-ya.com/claude-code-introduction/">Claude Code 是什麼？完整介紹與安裝教學</a></li>



<li><a href="https://blog.che-ya.com/claude-code-getting-started/">Claude Code 新手上路：第一次使用就上手</a></li>



<li><a href="https://blog.che-ya.com/claude-code-claude-md/">CLAUDE.md 專案記憶：讓 Claude 記住你的專案規範</a></li>



<li><a href="https://blog.che-ya.com/claude-code-custom-skills/">Custom Skills：打造你的專屬自訂技能</a></li>



<li><a href="https://blog.che-ya.com/claude-code-hooks/">Hooks 事件掛鉤：自動化你的 Claude Code 工作流程</a></li>



<li><a href="https://docs.anthropic.com/en/docs/claude-code/sub-agents" rel="nofollow noopener" target="_blank">Claude Code Sub-agents 官方文件</a></li>
</ul>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='ㄚ槌' src='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/9914399915f96350f302945e8ddddee1a9b1995350182f513fd2e1fa816c100a?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://blog.che-ya.com/author/a3230230/" class="vcard author" rel="author"><span class="fn">ㄚ槌</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="saboxplugin-web "><a href="https://blog.che-ya.com" target="_self">blog.che-ya.com</a></div><div class="clearfix"></div></div></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fclaude-code-multi-agent-subagents%2F&amp;linkname=Multi-agent%20%E6%9E%B6%E6%A7%8B%EF%BC%9ASubagents%20%E8%88%87%E4%BB%BB%E5%8B%99%E5%88%86%E6%B4%BE" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fclaude-code-multi-agent-subagents%2F&amp;linkname=Multi-agent%20%E6%9E%B6%E6%A7%8B%EF%BC%9ASubagents%20%E8%88%87%E4%BB%BB%E5%8B%99%E5%88%86%E6%B4%BE" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_x" href="https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fblog.che-ya.com%2Fclaude-code-multi-agent-subagents%2F&amp;linkname=Multi-agent%20%E6%9E%B6%E6%A7%8B%EF%BC%9ASubagents%20%E8%88%87%E4%BB%BB%E5%8B%99%E5%88%86%E6%B4%BE" title="X" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.che-ya.com%2Fclaude-code-multi-agent-subagents%2F&#038;title=Multi-agent%20%E6%9E%B6%E6%A7%8B%EF%BC%9ASubagents%20%E8%88%87%E4%BB%BB%E5%8B%99%E5%88%86%E6%B4%BE" data-a2a-url="https://blog.che-ya.com/claude-code-multi-agent-subagents/" data-a2a-title="Multi-agent 架構：Subagents 與任務分派"></a></p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
