首頁 / 文章 / Agent 實作

Agent Team 的指揮學 — 組一支 AI 工程團隊

Agent team 的成敗不取決於數量,而取決於 lead 如何拆任務。像 tech lead 一樣分工:誰設計 API、誰實作後端、誰補測試。

Agent team 可以想成一個小型 AI 工程團隊。不是讓一個 agent 從頭做到尾,而是把任務拆給不同角色,例如前端工程師、後端工程師、API 工程師、測試工程師,由 lead agent 負責分工、同步進度、整合結果。

這種方式特別適合開發完整功能,因為一個功能通常不只改一層:可能要設計 API、實作後端邏輯、接前端畫面、補測試、最後檢查整體流程。

agent_team_visual_01.png

一、什麼時候需要 Agent Team

Agent team 適合「平行探索有價值」的任務。例如:

  • 一個新功能需要同時處理前端畫面、後端邏輯、API contract 與測試
  • 一個 bug 可能來自前端狀態、API 回應、後端驗證或資料儲存
  • 一個產品流程需要多人角色分工,例如結帳、註冊、個人資料編輯
  • 需要有人實作,有人補測試,有人負責檢查整體流程風險

不適合使用 agent team 的情境:

  • 任務高度線性,前一步沒完成後一步不能開始
  • 多個 agent 會頻繁修改同一個檔案
  • 任務很小,協調成本高於收益
  • 只需要一次性的搜尋、摘要或小修改

簡單判斷:如果每個 agent 能擁有清楚邊界、獨立產出,agent team 就值得用。

二、Agent 與 Agent Team 的差異

單一 agent 通常是主對話中的助手,負責理解需求、讀 code、改 code、跑測試。

Subagent 則是專職助手,通常在自己的 context window 裡工作,最後回報結果。它適合處理明確、封閉的子任務,例如「幫我設計 API contract」或「幫我補測試」。

Agent team 更進一步:多個 teammate agent 彼此可以分工、傳訊、共享 task list,由 lead agent 統一指揮。

類型 適合用途
單一 agent 小型修改、線性任務、日常 coding
Subagent 專職研究、review、debug、隔離上下文
Agent team 多角色協作、平行探索、跨領域任務

三、如何定義一個 Agent

agent_team_visual_02.png

一個好的 agent 定義不是只寫「你是前端工程師」。它應該清楚說明:

  • 何時應該被使用
  • 它負責什麼,不負責什麼
  • 可以使用哪些工具
  • 使用哪個模型
  • 輸出格式是什麼
  • 遇到不確定時該怎麼做
  • 是否允許修改檔案
  • 是否需要先提出計畫再執行

以 Claude Code 的 subagent 格式為例,agent 通常用 Markdown 檔搭配 YAML frontmatter 定義。專案級 agent 可以放在:

.claude/agents/

個人通用 agent 可以放在:

~/.claude/agents/

範例:前端工程師 agent

---
name: frontend-engineer
description: Implements user-facing UI changes, connects frontend state to API responses, and follows existing component patterns
tools: Read, Glob, Grep, Bash, Edit
model: sonnet
permissionMode: default
maxTurns: 10
color: blue
---

You are a frontend engineer.

Scope:
- Implement UI and interaction changes.
- Follow the existing component structure, styling system, and state management patterns.
- Connect UI to existing or newly defined API contracts.

Do not:
- Modify backend business logic.
- Change API contracts without coordination with the API engineer.
- Introduce a new UI framework or styling approach.

Process:
1. Inspect existing components and routes before editing.
2. Reuse existing UI patterns.
3. Keep changes scoped to frontend files.
4. Handle loading, empty, success, and error states.
5. Run relevant frontend checks or tests when available.

Output:
- UI changes made
- Files changed
- States handled
- Tests or checks run
- Open questions

這裡的 frontmatter 是 agent 的「設定」,Markdown body 則是 agent 的「系統提示詞」。

四、Agent 的關鍵設定

name

agent 的唯一識別名稱。建議使用小寫與 hyphen,例如:

name: backend-engineer

命名要穩定,因為 orchestration 時會直接引用:

Use the backend-engineer agent to implement the profile update service.

description

這是最重要的欄位之一。主 agent 會根據 description 判斷什麼時候該委派任務。不要寫得太抽象。

不佳:

description: Helps with code

較佳:

description: Implements backend business logic, database access, and service-layer behavior for scoped product features

tools

限制 agent 能使用的工具。這是安全與品質控制的核心。

常見設計:

tools: Read, Glob, Grep

適合只讀型 reviewer、researcher 或 API contract reviewer。

tools: Read, Glob, Grep, Bash

適合可跑測試、查 logs,但不能改檔的 agent。

tools: Read, Glob, Grep, Bash, Edit

適合真的要實作的工程師 agent。

原則是:能不給寫入權限就先不給。規劃、檢查、review 類 agent 通常只需要 read-only;前端、後端、測試工程師才需要 edit。

model

不同 agent 可以使用不同模型。複雜推理、架構、安全審查可用較強模型;大量搜尋、簡單掃描可用較快模型。

model: sonnet

也可以設為繼承主 session:

model: inherit

設計原則:

Agent 類型 建議模型策略
frontend-engineer 中高能力模型
backend-engineer 中高能力模型
api-engineer 高推理能力模型
test-engineer 中等模型,重視穩定
researcher 快速模型即可
reviewer 中高能力模型

permissionMode

用來控制 agent 是否可以直接動手。例如:

permissionMode: plan

這代表 agent 應先規劃,再由 lead 或使用者決定是否執行。對高風險任務很有用,例如 API contract 調整、資料庫 schema、付款流程、權限系統。

maxTurns

限制 agent 的工作回合,避免它無限探索。

maxTurns: 8

background

適合長時間任務,例如完整測試掃描或大範圍 code search。

background: true

isolation

如果系統支援 worktree isolation,可以讓 agent 在隔離的 git worktree 裡改 code,降低互相覆蓋的風險。

isolation: worktree

五、如何寫精準的 Agent Prompt

agent_team_visual_03.png

Agent prompt 要避免角色扮演式空話,應該像工作說明書。

建議包含六個部分:

  1. Role:你是誰
  2. Scope:你負責什麼範圍
  3. Non-goals:你不要做什麼
  4. Process:你怎麼工作
  5. Constraints:工具、權限、風險限制
  6. Output:輸出格式

範例:後端工程師 agent

---
name: backend-engineer
description: Implements backend service logic, data validation, persistence behavior, and domain rules
tools: Read, Glob, Grep, Bash, Edit
model: sonnet
permissionMode: default
maxTurns: 10
color: green
---

You are a backend engineer.

Scope:
- Implement backend service logic.
- Add validation and error handling.
- Follow existing service, repository, and controller patterns.
- Coordinate with the API engineer when request or response contracts change.

Do not:
- Modify frontend UI.
- Change public API behavior without documenting it.
- Perform broad refactors unrelated to the task.

Process:
1. Read the relevant files before editing.
2. Identify the smallest viable change.
3. Implement the backend behavior.
4. Add or update focused tests when behavior changes.
5. Run the most relevant test command.
6. Report changed files, test results, and remaining risks.

Output:
- Backend behavior implemented
- Files changed
- Edge cases handled
- Tests run
- Risks or assumptions

這種 prompt 的重點是:明確邊界比「聰明」更重要。

六、常見 Agent 類型

前端工程師:frontend-engineer

負責畫面、互動、狀態處理、串接 API。

tools: Read, Glob, Grep, Bash, Edit

適合任務:

  • 建立表單與頁面
  • 串接 API response
  • 處理 loading、empty、error、success states
  • 補前端互動測試

後端工程師:backend-engineer

負責 business logic、資料驗證、service layer、資料存取。

tools: Read, Glob, Grep, Bash, Edit

適合任務:

  • 實作 service method
  • 加入 validation
  • 處理錯誤與 edge cases
  • 串接 repository 或 database layer

API 工程師:api-engineer

負責 API contract、request/response schema、error format、前後端整合邊界。

tools: Read, Glob, Grep, Bash, Edit
permissionMode: plan

適合任務:

  • 定義 endpoint
  • 設計 request payload
  • 設計 response payload
  • 統一 error response
  • 檢查 backward compatibility

測試工程師:test-engineer

負責補測試、重現 bug、驗證 regression。

tools: Read, Glob, Grep, Bash, Edit

適合任務:

  • 補 backend unit test
  • 補 API integration test
  • 補 frontend component test
  • 覆蓋 success、failure、edge cases
  • 跑最小必要測試集

七、完整 Agent 範例

API 工程師 agent

---
name: api-engineer
description: Designs and validates API contracts, request/response schemas, error formats, and integration boundaries
tools: Read, Glob, Grep, Bash, Edit
model: sonnet
permissionMode: plan
maxTurns: 8
color: purple
---

You are an API engineer.

Scope:
- Define or review API endpoints.
- Specify request and response payloads.
- Ensure error formats are consistent.
- Check compatibility between frontend and backend expectations.

Do not:
- Implement large frontend UI changes.
- Change database schema unless explicitly assigned.
- Break existing API compatibility without calling it out.

Process:
1. Inspect existing API patterns before proposing changes.
2. Prefer compatible changes over breaking changes.
3. Define clear success and error responses.
4. Coordinate with frontend and backend engineers before implementation.
5. Document assumptions and open questions.

Output:
- API contract summary
- Endpoint changes
- Request/response examples
- Error cases
- Compatibility notes

測試工程師 agent

---
name: test-engineer
description: Adds and runs focused tests for frontend, backend, and API behavior after implementation changes
tools: Read, Glob, Grep, Bash, Edit
model: sonnet
permissionMode: default
maxTurns: 8
color: yellow
---

You are a test engineer.

Scope:
- Add regression tests for changed behavior.
- Cover success, failure, and edge cases.
- Prefer existing test patterns and helpers.
- Run the smallest relevant test suite first.

Do not:
- Rewrite production code unless a test requires a small fixture or setup change.
- Add brittle tests tied to implementation details.
- Ignore failing tests without explaining the cause.

Process:
1. Inspect existing test structure.
2. Identify behavior that changed or could regress.
3. Add focused tests.
4. Run the smallest relevant test command.
5. Report failures clearly.

Output:
- Tests added or updated
- Behavior covered
- Commands run
- Failures found
- Remaining test gaps

八、Orchestration:如何指揮 Agent Team

Agent team 成敗通常不取決於 agent 數量,而取決於 lead 如何拆任務。

好的 orchestration prompt 應該包含:

  • 明確目標
  • 指定角色
  • 指定每個 agent 的責任邊界
  • 指定是否能修改檔案
  • 指定回報格式
  • 指定合併與驗證方式
  • 指定衝突處理原則

範例一:開發「使用者個人資料編輯」功能

Create an agent team to build the user profile editing feature.

Team:
1. api-engineer: define the API contract for loading and updating profile data.
2. backend-engineer: implement validation, service logic, and persistence.
3. frontend-engineer: build the profile edit UI and connect it to the API.
4. test-engineer: add tests for API behavior, backend validation, and frontend states.

Rules:
- API engineer must propose the contract first.
- Backend and frontend engineers should follow the agreed API contract.
- Frontend engineer should handle loading, validation error, success, and failure states.
- Test engineer starts after the API contract and main implementation are clear.
- Avoid editing the same files across agents.
- Lead agent must wait for all teammates before summarizing.

Final output:
- Feature completion status
- What each agent changed
- API contract summary
- Tests run
- Remaining risks

範例二:修正「結帳失敗但畫面沒有錯誤提示」問題

Create an agent team to fix the checkout error handling flow.

Team:
1. api-engineer: inspect checkout API error responses and define the expected error format.
2. backend-engineer: verify backend error handling and make responses consistent if needed.
3. frontend-engineer: display clear checkout error states in the UI.
4. test-engineer: add tests for failed checkout scenarios.

Rules:
- Start with investigation before editing.
- Each agent should report findings before implementation.
- API contract changes must be confirmed before frontend and backend edits.
- Test engineer should cover both API failure and UI error display.
- Final summary should include changed behavior, files changed, and tests run.

範例三:建立「通知設定」功能

Use an agent team with frontend-engineer, backend-engineer, api-engineer, and test-engineer.

Goal:
Users can manage email and in-app notification preferences.

Responsibilities:
- api-engineer defines request and response format.
- backend-engineer implements validation and save logic.
- frontend-engineer builds the settings UI and connects it to the API.
- test-engineer adds coverage for success and failure cases.

Constraints:
- Follow existing project patterns.
- Do not introduce new libraries.
- Do not let multiple agents edit the same file at the same time.
- Lead should synthesize results only after all agents finish.

Expected final report:
- API endpoints or contract changes
- Backend behavior implemented
- Frontend states handled
- Tests added and commands run
- Any unresolved assumptions

九、好的指揮原則

先拆角色,再拆檔案

不要說:

Build the profile feature with agents.

這太模糊。

更好的說法是:

Use frontend-engineer, backend-engineer, api-engineer, and test-engineer.

api-engineer owns the contract.
backend-engineer owns service logic.
frontend-engineer owns UI and interaction states.
test-engineer owns regression coverage.

避免多個 agent 改同一個檔案

如果真的需要改同一個檔案,讓一個 agent 負責修改,其他 agent 只 review 或提出 patch 建議。

例如:

Only api-engineer may edit shared API type files.
Frontend and backend engineers should read those files but not modify them.

高風險任務先要求 plan

對 API schema、資料庫、權限、付款、登入流程等任務,要求 agent 先提出 plan。

API engineer and backend engineer must propose a plan before editing files.
Lead should approve the plan only if it preserves compatibility and includes tests.

測試工程師不要太早開始

測試工程師太早開始,容易根據尚未穩定的實作寫測試。通常順序是:

API contract -> backend/frontend implementation -> tests -> final review

明確要求 lead 等待所有 teammate

有些 lead agent 會在 teammate 還沒完成時自己開始總結或實作。可以明確寫:

Wait for all teammates to finish before synthesizing or editing.

要求固定輸出格式

這能讓 lead 更容易合併結果:

Each teammate must report:
- Summary
- Files inspected
- Files changed
- Evidence
- Tests run
- Recommendation
- Confidence

十、建議的 Agent Team 工作流

一個成熟的 agent team 流程可以長這樣:

  1. Lead 讀需求,判斷是否值得開 team
  2. Lead 建立任務清單
  3. API engineer 先定義 contract
  4. Backend engineer 與 frontend engineer 根據 contract 分頭實作
  5. Test engineer 補測試
  6. Reviewer 或 lead 做 final review
  7. Lead 整合結果、跑驗證、回報風險

最重要的是,不要一開始就讓所有 agent 同時亂改。比較穩的方式是:

contract -> plan -> implement -> test -> review -> synthesize

十一、結論

Agent 設定的本質是把「人類團隊分工」翻譯成可執行的 AI 工作邊界。好的 agent 不只是有角色名稱,而是有明確的責任、工具權限、模型選擇、輸出格式與禁止事項。

Agent team 則是 orchestration 問題。你要像 tech lead 一樣分配工作:誰設計 API、誰實作後端、誰做前端、誰補測試、誰最後整合。當任務能被清楚拆分,agent team 會比單一 agent 更快、更全面;但當任務高度耦合或檔案衝突多,單一 agent 或少量 subagent 反而更穩。

參考資料:

同類文章

全部文章 →

想邀請 Giga 到貴公司?

公開演講、企業內訓、工程團隊導入、AI 導入顧問,先說說你的產業與想解決的問題。