October 23, 2026

如何平行執行多個 Claude Code 代理

讓每個 Claude Code 代理擁有自己的 git worktree,分派給每個代理一個修改不同檔案的任務,再逐一審查各個分支。本指南先以單純的 Claude Code 逐步說明設定方式,接著介紹 Chief 如何替你規劃、執行並審查平行代理。

簡短答案

要同時執行多個 Claude Code 代理:

  1. 拆分工作,讓每個任務修改不同的檔案。
  2. 在各自的 worktree 中啟動每個代理,使用 claude --worktree <name>,每個代理一個終端機,讓編輯永遠不會互相衝突。
  3. 複製本機設定,例如用 .worktreeinclude 檔案將 .env 複製到每個 worktree,並在其中安裝相依套件。
  4. 在同一個畫面監看所有代理,使用背景代理:claude --bg "<task>" 啟動一個代理,claude agents 列出全部代理。
  5. 逐一審查並合併每個分支,再讓 Claude Code 清理 worktree。

以下所有內容只用 Claude Code 就能完成。同樣的模式也適用於 OpenAI Codex,或兩者混用。

開始之前

  • 已安裝並登入 Claude Code。 執行 claude --version 確認。以下旗標都在目前的版本中;如果缺少某個旗標,請執行 claude update。
  • 至少有一次 commit 的 git 儲存庫。 Worktree 會從既有的 commit 建立分支。
  • 用量餘裕。 每個代理都從同一個方案扣用量。三個代理大約會用掉一個代理三倍的 token,所以請先確認你的限制。AI 程式代理成本詳細說明各方案包含的內容。

步驟 1:挑選可以並行的任務

平行代理在工作互不重疊時速度最快。啟動任何代理之前,針對每個任務問一個問題:它是否需要另一個任務的產出,或會編輯相同的檔案?

適合平行執行最好依序執行
一個新的 API 端點和一個不相關的設定頁面資料庫遷移,以及讀取新欄位的程式碼
模組 A 的測試和模組 B 的重構對同一個檔案的兩次重構
說明文件更新和一個錯誤修正共用型別的變更,以及所有使用該型別的功能

當兩個任務互相依賴時,先執行共用的部分(介面、型別或遷移),再啟動建立在其上的任務。三個切分清楚的任務,勝過六個不斷在相同檔案中碰頭的任務。

步驟 2:在各自的 worktree 中啟動每個代理

git worktree 是儲存庫的第二份簽出,位於自己的分支上,共用相同的歷史紀錄。每個代理編輯自己那份檔案,所以它寫入的任何內容都不會落入另一個代理的工作中。

Claude Code 會替你建立 worktree。在儲存庫根目錄中,為每個代理開啟一個終端機:

# Terminal 1
claude --worktree add-invoices-api

# Terminal 2
claude --worktree settings-page

# Terminal 3
claude -w fix-flaky-login-test

每個指令會在名為 worktree-<name> 的新分支上建立 .claude/worktrees/<name>/,然後在其中啟動一個 Claude Code 工作階段。把任務當作第一則訊息交給每個工作階段。

將 worktree 資料夾加入 .gitignore 一次,這些簽出就不會在主要簽出中顯示為未追蹤的檔案:

echo ".claude/worktrees/" >> .gitignore

步驟 3:為每個 worktree 準備執行所需的一切

Worktree 是全新的簽出,只包含已追蹤的檔案。你的 .env、本機數位憑證和 node_modules 都會留在主要簽出中。

在儲存庫根目錄建立 .worktreeinclude 檔案。它使用 .gitignore 語法,Claude Code 會將每個符合規則、且已被 Git 忽略的檔案複製到每個新的 worktree:

.env
.env.local
config/local-settings.json

接著請每個代理在開始前安裝相依套件,或自己在 worktree 目錄中執行設定指令。注意兩種共用資源:

  • 連接埠。 兩個開發伺服器無法同時監聽 3000 埠。告訴每個代理要使用哪個連接埠。
  • 資料庫。 對同一個本機資料庫執行遷移的代理可能會互相干擾。給每個代理各自的資料庫名稱,或只在一個任務中執行遷移。

步驟 4:在同一個畫面監看每個代理

三個終端機分頁足以應付三個代理。超過這個數量,就改用背景代理。每個背景代理都是完整的 Claude Code 工作階段,不需連接終端機也會持續執行,而且 Claude Code 會在它編輯任何內容之前,先把它移到自己的 worktree 中。

claude --bg "add pagination to the invoices API and cover it with tests"
claude --bg "fix the flaky login test in tests/auth"
claude agents

claude agents 會開啟一個檢視畫面,列出所有工作階段,並依 Needs input、Working 和 Completed 分組。按 Space 可以快速查看某個工作階段並回答它的問題,不必連接;按 Enter 則開啟完整對話。在指令碼中,claude attach <id>、claude logs <id> 和 claude stop <id> 可完成相同的工作。

步驟 5:在單一任務中使用子代理平行處理

子代理與平行工作階段不同。子代理在一個 Claude Code 對話中執行,在自己的上下文視窗中完成一項特定的工作,然後回報結果。它們適合單一任務內的研究與分散處理:「用子代理檢查每個服務是否還在使用舊的驗證標頭。」

當子代理平行編輯檔案時,給每個子代理各自的 worktree。請 Claude「為你的代理使用 worktree」,或在 .claude/agents/ 中自訂子代理的 frontmatter 加入 isolation: worktree:

---
name: refactorer
description: Applies mechanical refactors across many files
isolation: worktree
---

Apply the requested refactor across every affected file, then run the tests and report the results.

對於想以獨立分支審查的獨立任務,使用平行工作階段。要拆分單一任務時,使用子代理。

步驟 6:一次審查並合併一個分支

每個代理都在自己的分支上完成工作,所以審查就是一般的分支審查:

git worktree list
git log --oneline main..worktree-add-invoices-api
git diff main...worktree-add-invoices-api

依序合併每個分支或為其開啟 pull request,並在每次合併後執行測試。一次合併一個,代表任何衝突都很小,也容易定位。合併之前,檢查每個分支的三件事:它完成了任務要求的內容、測試通過,而且沒有包含範圍以外的變更。

若要處理更大的批次,如何大規模審查 AI 產生的程式碼介紹了審查佇列與以代理擔任審查者的工作流程。

步驟 7:清理

當你結束 worktree 工作階段時,Claude Code 會檢查是否有未儲存的工作。來自未命名工作階段的乾淨 worktree 會連同分支一起移除。有變更的 worktree 會提示你保留或移除。對於你自己建立的 worktree,或非互動式執行遺留下來的 worktree:

git worktree remove .claude/worktrees/add-invoices-api
git worktree prune

應該執行多少個代理?

從兩到三個開始。限制很少來自你的筆電:Claude Code 的繁重工作在雲端完成,而每個 worktree 在磁碟上大約只占用一份原始碼檔案的空間。真正的限制是:

  • 你的審查時間。 如果完成的分支開始堆積,增加代理只會讓待審查的分支堆得更多。
  • 任務的獨立性。 你需要有那麼多個不會編輯相同檔案的任務。
  • 你的用量限制。 每個代理都從同一個方案扣用量。

合適的數量能讓每個代理都保持忙碌,同時讓你的審查佇列保持簡短。

Chief 如何替你執行多個代理

以上所有做法都有效。但當你每天執行好幾個代理時,大部分時間會花在程式碼周邊的步驟上:挑選不會衝突的任務、撰寫每個提示、查看哪個代理在等待,以及審查回傳的內容。Chief 會接手這個循環。

  • 你設定目標,Chief 規劃工作。 用一句話描述你要的成果。Chief 會將它拆分成里程碑,每個里程碑都有完成標準及其所依賴的里程碑。
  • 依計畫平行執行。 Chief 會同時執行所有依賴項目已完成的里程碑。它會先規劃型別、介面和遷移等共用部分,並將大多會編輯相同檔案的里程碑串接或合併,讓平行代理不會互相妨礙。
  • 在你的電腦上執行,使用你的訂閱。 Chief 中繼會在你的專案資料夾中啟動 Claude Code、Codex 和其他代理,使用你已經付費的帳號。每個代理只 commit 自己的檔案,並標記其任務,讓每項變更都能追溯到對應的里程碑。
  • 每個結果都經過審查。 Chief 會依照完成標準檢查每個完成的里程碑。只有你能做的決定會集中到同一個佇列。
  • 有預算的 Auto mode。 設定任務上限和供應商用量的每週上限,Chief 就會持續朝你的目標工作,直到達到其中任一上限為止。
  • 隨時隨地查看進度。 透過手機、Claude、Claude Code 或 Codex 中的 Chief MCP 伺服器,或透過 Chief for Discord 的私訊,核准里程碑並回覆佇列。

你設定方向並做出決定,Chief 讓代理持續工作。請參閱 Chief 與 Claude Code 了解兩者如何搭配,並參閱多代理協調指南了解全貌。

常見問題

我可以在同一個資料夾中執行多個 Claude Code 工作階段嗎?

可以,但同一個資料夾中的代理會編輯相同的檔案,所以一個代理的變更可能會落在另一個代理的工作進行到一半時。用 claude --worktree <name> 給每個工作階段各自的 worktree,或規劃任務讓它們永遠不會修改相同的檔案,這正是 Chief 在單一專案資料夾中執行平行代理的方式。

子代理和平行代理有什麼不同?

子代理在一個 Claude Code 對話中執行,並將結果回報給該對話。平行代理則是獨立的 Claude Code 工作階段,有自己的任務,通常也有自己的分支。用子代理拆分單一任務,用平行工作階段處理獨立的任務。

平行的 Claude Code 代理會共用我的用量限制嗎?

會。登入同一個帳號的每個工作階段都從同一個方案扣用量,所以三個代理用完用量額度的速度大約是一個代理的三倍。AI 程式代理成本比較了各方案與限制。

我可以在同一個儲存庫上平行執行 Claude Code 和 Codex 嗎?

可以。用 git worktree add 讓每個工具在各自的 worktree 中啟動,然後在其中一個執行 claude,另一個執行 codex。Chief 會從同一個計畫執行兩者,並將每個里程碑交給你選擇的代理。請參閱多供應商協調指南,或參閱 Claude Code 替代方案,了解可與 Claude Code 搭配的其他代理。

有 Claude Code 的協調工具嗎?

Claude Code 內建子代理、背景代理和 claude agents 檢視畫面,讓你自己執行多個工作階段。像 Chief 這樣的協調工具則為這些工作階段加入規劃、分派、審查和佇列功能,讓代理持續朝你的目標工作,不需要你逐一啟動。

More articles

AI Agent Orchestration Guide

What AI agent orchestration is, how multi-agent orchestration works for Claude Code and Codex, and a comparison of orchestration approaches. Free guide.

What Is Agentic Engineering?

Agentic engineering means directing AI coding agents that write and test code while you set goals and review results. How it differs from agentic coding.

AI Code Review at Scale

What AI code review is, the main types of AI code review tools, and five workflows for reviewing AI-generated code from several coding agents at once.

Claude Code and Codex Pricing

Claude Code pricing from $20/mo (Pro) to $200/mo (Max 20x), Codex pricing from ChatGPT Free to Pro, usage limits and a comparison table, checked Oct 2026.

From Single Agent to Multi-Agent

Already using one AI coding agent? Here's how to scale to multiple instances running in parallel — the concurrency patterns, worktree isolation, and task decomposition strategies that make it work.

Vibe Coding at Scale

Vibe coding with one agent is fun. Vibe coding with five is chaos — unless you have orchestration. Here's how to scale the vibe without losing the quality.

Why AI Agents Need an Orchestration Layer

Running AI coding agents in terminal tabs works for two. Five signs you've outgrown it, tabs vs an orchestration layer, and what to look for in one.

Multi-Provider AI Orchestration

Claude Code, Codex, Cursor, Aider — each excels at different work. Here's how to combine providers into one coordinated workflow and why cross-provider review catches bugs single-provider setups miss.

Use Chief From Your AI Assistant

Add Chief's remote MCP server to Claude, Claude Code or Codex. Ask what's waiting on you, catch up on what shipped and propose milestones from chat.

Claude Code vs Codex: When to Use Each

Codex vs Claude Code: when each fits, a comparison table and FAQ. Claude Code vs Codex isn't either-or; Chief runs both on your projects.

Claude Code vs Cursor: When to Use Each

Cursor vs Claude Code: when each fits, a comparison table and FAQ. Claude Code vs Cursor isn't either-or; keep Cursor and let Chief run Claude Code.

Git Worktrees for AI Coding Agents

How to use git worktrees with Claude Code and Codex: one per agent, parallel runs, .env files, merging and cleanup. Every command, step by step.

How to Run Claude Code From Your Phone

Run Claude Code from your phone: Remote Control, cloud sessions in the Claude mobile app, or SSH and tmux, step by step. Plus approving agents in Discord.

The Best AI Coding Agents in 2026

The best AI coding agents of 2026: Claude Code, Codex, Cursor, Copilot and more agentic AI coding tools compared, and how to run several with Chief.

Subagents vs Parallel Agents

What is a subagent? How Claude Code subagents and Codex subagents work, how they differ from parallel agents, and when to use each one.

How to Install and Set Up Codex CLI

Install Codex CLI on macOS, Linux or Windows, sign in, run your first session and update it. Every command, then run Codex alongside Claude Code in Chief.

Ready to manage your agents?

Chief 會規劃工作、平行執行你的 Claude Code 和 Codex 代理,並彙整只有你能做的決定。