跳至主要內容

Director Mode 框架:像 Director 一樣帶領 Claude Code

停止逐行輸入指令,開始領導團隊達成目標。學習這個思維轉變,透過平行代理的策略性委派,徹底改變你使用 AI 開發的方式。

大多數開發者使用 Claude Code 就像一個進階的自動補全工具——逐一輸入指令、等待回應、手動管理每一個步驟。這就是 親力親為模式 (Hands-on Developer Mode)

有一個更好的方式。Director Mode 把 Claude Code 當作你的開發團隊,擁有不同的專業成員。你定義想要的結果、設定目標,讓團隊成員並行工作——就像 Director 不親自演每一個角色,卻能帶領團隊創造出色作品。

本指南將教你基於 Claude Code 官方功能的完整Director Mode框架。

核心理念

「Director 不會親自做每一件事——他們創造讓團隊能夠一起出色工作的條件。」

Director Mode建立在三大支柱上:

  1. 目標優於指令 - 定義你想要什麼結果,而非如何達成
  2. 團隊委派 - 信任專門的代理同時處理任務
  3. 結果驗證 - 驗證結果,而非每一個步驟

理解 Claude Code 的代理架構

在實施Director Mode之前,先了解 Claude Code 實際提供什麼。

子代理與 Agent 工具

Claude Code 可以透過 Agent 工具把工作委派給隔離的子代理。互動使用時,說清楚角色、範圍與預期輸出即可;工具呼叫 schema 是可能變動的實作細節:

Use an Explore subagent to map the authentication module.
Find every JWT-related function and its callers, then return conclusions
and relevant file paths rather than raw file dumps.

可用的內建與自訂 agent 取決於目前 Claude Code 版本和專案設定。唯讀程式碼庫調查可用 Explore,範圍較廣的任務可用 general-purpose,或使用 .claude/agents/ 中已定義的自訂 agent。

模型選擇策略

模型名稱、預設值、供應情況、延遲與價格都會變動。先讓子代理繼承 session 模型;只有在實測顯示特定任務能獲得品質、延遲或成本優勢時才覆寫,並以 /model 或目前模型文件為準,不要把層級對照表永久寫死。

問題所在:親力親為模式

看看一個典型開發者如何使用 Claude Code:

User: "Please read the file src/auth.ts"
Claude: [reads file]
User: "Now find the validateToken function"
Claude: [shows function]
User: "Add input validation for the token parameter"
Claude: [makes change]
User: "Now run the tests"
Claude: [runs tests]

這就是 親力親為模式——微觀管理每一個動作。代價是:

  • 一個簡單任務需要 5+ 次往返
  • 在思考和下指令之間 切換上下文
  • 沒有平行執行——所有事情都是順序的
  • 你需要記住每一步的 認知負擔

解決方案:Director Mode

同樣的任務在Director Mode下:

User: "Implement input validation for token handling in our
      auth module. Follow our security guidelines and ensure
      test coverage."

Claude: [autonomously]
  → Explores codebase structure
  → Identifies all token-related functions
  → Analyzes existing validation patterns
  → Implements validation following project conventions
  → Writes comprehensive tests
  → Validates against security requirements

一個以成果為中心的提示,減少手動交接。

三大支柱實踐

支柱 1:目標優於指令

親力親為方式:

"Read src/api/users.ts, find the createUser function,
add try-catch around the database call, log any errors
using console.error with the prefix '[UserAPI]', then
return a 500 status code."

Director Mode 方式:

"Add proper error handling to the user creation endpoint
following our API error standards."

當你在 CLAUDE.md 中建立了專案的錯誤處理標準,Claude 就知道:

  • 使用哪個 logger
  • 遵循什麼錯誤格式
  • 如何結構化回應
  • 錯誤訊息該包含什麼

支柱 2:團隊委派

Claude Code 2.0+ 支援 平行代理執行。這是改變遊戲規則的關鍵。

不要順序執行任務:

Task 1 (30s) → Task 2 (30s) → Task 3 (30s) = 90 seconds total

平行執行它們:

Task 1 (30s) ┐
Task 2 (30s) ├→ 30 seconds total
Task 3 (30s) ┘

實際範例——分析程式碼庫:

"I need to understand how authentication works in this project."

Claude spawns 5 parallel agents:
→ 成員 1 (Explore): Maps auth-related file structure
→ 成員 2 (Grep): Finds all JWT/token references
→ 成員 3 (Read): Analyzes middleware patterns
→ 成員 4 (Grep): Identifies security guidelines
→ 成員 5 (Read): Reviews existing tests

Results synthesized in ~30 seconds instead of 3+ minutes.

支柱 3:專案藍圖

你的 CLAUDE.md 檔案是專案的藍圖。它定義了指導所有開發的核心原則。

# CLAUDE.md - 團隊指南

## 核心原則
- All code changes require corresponding tests
- Use TypeScript strict mode—no `any` types
- Follow the repository's existing patterns
- Security-first: validate all inputs at boundaries

## 自主操作(不需確認)
- Read any project file
- Run tests and linting
- Create/modify files in src/ and tests/
- Create local git branches

## 需要確認
- Push to remote
- Modify environment variables
- Delete files
- Install production dependencies

## 團隊分工
When encountering these situations:
- Code quality questions → invoke code-reviewer agent
- Security-sensitive code → invoke security-auditor agent
- Performance concerns → run benchmarks before committing

Explore Agent 深入解析

Explore agent 是用於程式碼庫導航的唯讀子代理。它使用的模型與執行時間取決於目前設定,因此應按調查範圍選擇徹底程度,不要承諾固定秒數。

徹底程度等級

Quick: Explore the auth module and find the login handler.
Medium: Map the JWT flow, including callers and tests.
Very thorough: Trace the complete authentication boundary and report evidence,
open questions, and relevant file paths.

舊方法 vs 新方法

舊方法(5 個手動步驟):

成員 1: Glob find *auth*.ts
成員 2: Grep search "JWT"
成員 3: Read auth/index.ts
成員 4: Grep find authenticate() usage
成員 5: Read test files

新方法(1 個 Explore agent):

Use an Explore subagent to inspect the authentication implementation.
Focus on JWT handling, middleware, and test coverage. Return a concise map
with relevant file paths.

結果: 一次有邊界的委派可取代多次手動交接,也能把探索細節留在主 context 之外。實際節省時間取決於 repo 與任務。

Agent 角色

Claude Code 內建部分子代理,也支援自訂 agent。以下名稱只是範例,不保證內建;引用前須先在 .claude/agents/ 中定義:

代理角色最適用於
code-reviewer程式碼品質分析實作後
security-auditor漏洞偵測認證/支付變更
test-runner測試執行和分析程式碼變更後
debugger根本原因分析錯誤發生時
refactor-assistant程式碼改進降低複雜度
doc-writer文件撰寫API 變更

委派範例:

"Implement the payment integration.

Delegation:
→ security-auditor: Review before merge
→ code-reviewer: Check patterns and quality
→ test-runner: Ensure 80%+ coverage"

自動執行的 Hooks

Director Mode 可以使用 hooks 執行已設定的生命週期檢查。參考 hooks 文件

Stop Hook - 驗證完成

{
  "Stop": [{
    "matcher": "*",
    "hooks": [{
      "type": "prompt",
      "prompt": "Check if code was modified. If Write/Edit was used, verify tests were run. If no tests ran, block and explain."
    }]
  }]
}

PreToolUse Hook - 阻擋危險操作

{
  "PreToolUse": [{
    "matcher": "Bash",
    "hooks": [{
      "type": "prompt",
      "prompt": "If command contains 'rm -rf', 'drop', or destructive operations, return 'ask' for user confirmation. Otherwise 'approve'."
    }]
  }]
}

可用的 Hook 事件

事件觸發時機使用案例
PreToolUse工具執行前核准/拒絕/修改
PostToolUse工具執行後回應結果
Stop代理考慮停止時驗證完成
SessionStart會話開始載入上下文
SubagentStop子代理停止時確保任務完成
UserPromptSubmit使用者提交提示時添加上下文/驗證

平行執行模式

模式 1:分析蜂群

"Analyze the authentication system.

Launch in parallel:
→ 成員 1 (Explore quick): Map file structure
→ 成員 2 (Explore medium): Find all auth middleware
→ 成員 3 (Grep): Search for JWT patterns
→ 成員 4 (Read): Review existing tests
→ 成員 5 (Grep): Find recent auth changes"

時間比較: 順序工作的耗時會逐項累加;平行工作則由最慢的獨立分支加上協調成本決定。請在自己的任務上實測,不要預設固定倍數。

模式 2:分而治之

用於多檔案重構:

"Migrate from REST to GraphQL.

Parallel implementation:
→ 成員 1: Convert users endpoints
→ 成員 2: Convert products endpoints
→ 成員 3: Convert orders endpoints
→ 成員 4: Update shared types
→ 成員 5: Update tests"

模式 3:實作與審查

"Build user profile editing.

Phase 1 (parallel):
→ Implementation member: Build the feature
→ Test member: Write test cases
→ Doc member: Draft documentation

Phase 2 (parallel):
→ security-auditor: Security review
→ code-reviewer: Quality check"

實施層級

層級 1:基礎Director Mode

將指令轉換為結果:

不要這樣說改這樣說
”Add console.log statements to debug…""Debug why the user login fails"
"Create a new file called X and add…""Add a utility for date formatting"
"Run npm test and show me failures""Ensure all tests pass”

層級 2:CLAUDE.md 藍圖

建立全面的 CLAUDE.md,包含:

  1. 專案脈絡 - 技術棧、結構、指令
  2. 開發標準 - 程式碼風格、測試、文件
  3. 工作流程指南 - 什麼需要確認、自主操作
  4. 團隊分工 - 哪些代理處理哪些任務

層級 3:團隊並行協作

"Analyze and refactor the authentication module.

Phase 1 - Analysis (parallel):
→ 3 Explore agents: Map structure, find patterns, check tests

Phase 2 - Implementation (parallel):
→ Multiple agents: One per file

Phase 3 - Review (parallel):
→ code-reviewer + security-auditor + test-runner"

層級 4:完全自主模式

"Implement feature X end-to-end.

Constraints:
- Follow existing patterns
- 80%+ test coverage
- No breaking changes
- Security review required

Freedom to:
- Choose implementation approach
- Structure code as appropriate
- Design tests

Verify before completing:
- All tests pass
- No TypeScript errors
- Documentation updated"

真實世界範例

Bug 調查

"Users report 'undefined is not a function' on dashboard.

Investigate in parallel:
→ Explore: Search for error patterns
→ Grep: Find recent dashboard changes
→ Read: Check related components

Then:
- Fix root cause
- Add regression test
- Verify no other occurrences"

功能開發

"Add dark mode to the application.

Constraints:
- Use CSS variables
- Persist preference
- Default to system preference

Delegation:
→ Explore agents: Find all color usage
→ Implementation: Add theme system
→ test-runner: Visual regression tests"

程式碼審查

"Review PR #123 comprehensively.

Parallel review:
→ code-reviewer: Quality and patterns
→ security-auditor: Vulnerabilities
→ test-runner: Coverage analysis
→ Explore agent: Impact assessment

Output: Categorized findings with severity"

思維轉變

親力親為思維Director 思維
我告訴 Claude 具體該做什麼我告訴 Claude 我需要什麼結果
我分解每一個步驟我定義約束讓 Claude 規劃
我等待每一個回應我讓代理平行工作
我驗證每一個動作我信任但驗證最終結果
我下指令像在訓練我委派像在帶領團隊

衡量成功

效率指標

指標親力親為模式Director Mode
提示模式許多逐步交接較少的成果層級交接
上下文切換
完成時間順序執行基準取決於任務獨立性與協調成本
一致性取決於臨時提示透過清楚上下文與驗證改善

品質指標

當你的Director Mode運作良好時:

  • Claude 首次就產出符合你風格的程式碼
  • 你很少兩次糾正同樣的錯誤
  • 任務分支真正獨立時,平行 agent 可縮短實際等待時間
  • 審查會在明確委派或已設定的自動化強制時執行

今天就開始

第 1 天: 注意你目前的提示模式。你是否在給一步一步的指令?

第 1 週:

  1. 將 3 個提示從指令轉換為結果
  2. 建立基本的 CLAUDE.md 包含核心指南
  3. 嘗試一次平行 Explore 蜂群

第 1 個月:

  1. 完成包含所有章節的 CLAUDE.md
  2. 設定自動驗證的 hooks
  3. 對所有適合的任務使用平行代理
  4. 衡量效率改進

快速參考

Director Mode提示模板

"[Outcome description]

Constraints:
- [Must-have requirements]

Freedom to:
- [Decisions Claude can make]

Delegation:
- [Which agents should review]

Verify:
- [Completion criteria]"

模型選擇

Explore/Search → 預設繼承;有實測依據時再試較低成本模型
Implementation → 使用 session 模型,除非量測支持覆寫
Security/Architecture → 選擇足夠能力,並驗證結果

平行執行規則

Independent tasks → Launch simultaneously
Dependent tasks → Run sequentially
Mixed → Phase approach

準備好轉變你的 Claude Code 工作流程了嗎?從親力親為到 Director 的轉變不僅僅是關於效率——而是在結果層面而非操作層面工作。

來源:Claude Code subagentsClaude Code model configurationClaude Code hooks