點點學院 banner
推薦課程平台
前往點點學院,查看更多 AI 與實戰課程
如果你喜歡這篇內容,可以直接到點點學院延伸閱讀更多工具教學、工作流與實作型課程。
立即前往 →
← 回到大綱
Lesson 4-3

設計數個 Agent

Agent Design Workshop

🎨 設計練習:電商系統

任務

為一個電商系統設計 Agent 分工:

  • 商品管理
  • 購物車與結帳
  • 使用者管理
  • 訂單處理
  • 金流串接

✅ 建議設計

  1. Product Agent:商品 CRUD、庫存管理、分類標籤
  2. Cart Agent:購物車操作、優惠券計算
  3. Order Agent:訂單建立、狀態追蹤、通知
  4. Payment Agent:金流串接、交易驗證
  5. User Agent:認證、權限、個人資料

💻 在 Claude Code 中實作多 Agent 協作

方法一:順序執行 — 逐個完成

適合有依賴關係的任務,前一個 agent 完成後再啟動下一個。

# 1. 先建立 User Agent
請建立 User Agent,包含:
- 使用者註冊/登入功能
- JWT token 驗證
- 儲存在 users/ 目錄

# 2. 等第一個完成後,建立 Product Agent
請建立 Product Agent,包含:
- 商品 CRUD API
- 使用剛才的 User Agent 做權限檢查
- 儲存在 products/ 目錄

# 3. 接著建立 Cart Agent
請建立 Cart Agent,整合:
- User Agent(驗證使用者)
- Product Agent(查詢商品資訊)
- 購物車邏輯儲存在 cart/ 目錄

方法二:並行開發 — 同時進行

適合獨立模組,可以同時開發後再整合。

# 一次性給完整指令
請建立電商系統的以下 5 個 Agent,
每個 Agent 獨立運作,使用統一的資料格式:

1. User Agent (users/)
   - POST /auth/register
   - POST /auth/login
   - GET /users/:id

2. Product Agent (products/)
   - GET /products
   - GET /products/:id
   - POST /products (需管理員權限)
   - PUT /products/:id
   - DELETE /products/:id

3. Cart Agent (cart/)
   - POST /cart/add
   - GET /cart/:userId
   - DELETE /cart/:userId/:productId

4. Order Agent (orders/)
   - POST /orders
   - GET /orders/:userId
   - PUT /orders/:id/status

5. Payment Agent (payments/)
   - POST /payments/checkout
   - GET /payments/:orderId
   - POST /payments/webhook (金流回調)

統一使用 Express.js,
資料庫使用 SQLite,
每個 Agent 有獨立的 router 檔案

方法三:主從架構 — 指定主 Agent

先建立一個主控 Agent,再讓它呼叫其他 Agent。

# 第一步:建立主控 Agent
請建立一個 API Gateway (main-agent/),
負責:
- 路由分配
- 統一錯誤處理
- CORS 設定
- Rate limiting

# 第二步:建立子 Agent 並註冊到主控
請建立 Product Agent,
並在 main-agent/routes.js 中註冊路由:
  /api/products/* → ProductAgent

# 第三步:繼續註冊其他 Agent
請建立 User Agent,
並註冊到 main-agent:
  /api/users/* → UserAgent
  /api/auth/* → UserAgent

方法四:使用 Workspace 切換

在不同 workspace 開發不同 Agent,最後整合。

# Terminal 1: User Agent
cd ~/projects/ecommerce-user-agent
code .
# 在 Claude Code 中開發 User Agent

# Terminal 2: Product Agent  
cd ~/projects/ecommerce-product-agent
code .
# 在 Claude Code 中開發 Product Agent

# 完成後整合
cd ~/projects/ecommerce-main
# 請整合 User Agent 和 Product Agent
# 從 ../ecommerce-user-agent 和 ../ecommerce-product-agent 複製程式碼

🎯 實際指令範例

完整實作流程

# 步驟 1:初始化專案
請建立一個電商系統專案架構:
ecommerce-system/
├── agents/
│   ├── user/
│   ├── product/
│   ├── cart/
│   ├── order/
│   └── payment/
├── shared/
│   ├── db/
│   ├── models/
│   └── utils/
├── main.js (主程式)
└── package.json

# 步驟 2:建立共用模組
請在 shared/ 建立:
1. db/sqlite.js - SQLite 連線
2. models/User.js - 使用者資料模型
3. models/Product.js - 商品資料模型
4. utils/jwt.js - JWT 工具
5. utils/validator.js - 資料驗證

# 步驟 3:逐一建立 Agent
請建立 User Agent (agents/user/):
- routes.js (路由)
- controller.js (邏輯)
- service.js (資料庫操作)
使用 shared/models/User.js

# 步驟 4:測試第一個 Agent
請建立測試檔案 agents/user/test.js
包含:
- 註冊測試
- 登入測試
- Token 驗證測試

# 步驟 5:建立下一個 Agent
User Agent 測試通過後,
請建立 Product Agent (agents/product/)
並整合 User Agent 的權限驗證

# 步驟 6:建立整合主程式
請在 main.js 整合所有 Agent:
- 載入所有 Agent 的 routes
- 設定中介層
- 啟動 Express server
- 監聽 port 3000

⚡ 進階技巧

1. 使用 Git 分支管理

# 為每個 Agent 建立分支
git checkout -b feature/user-agent
# 請開發 User Agent

git checkout -b feature/product-agent  
# 請開發 Product Agent

# 開發完成後合併
git checkout main
git merge feature/user-agent
git merge feature/product-agent

2. 使用環境變數區分 Agent

# .env
AGENT_MODE=product  # user / product / cart / order / payment

# 請根據 process.env.AGENT_MODE 
# 只啟動對應的 Agent
# 這樣可以分別部署不同的 Agent 到不同的伺服器

3. 使用 Message Queue 溝通

# 請建立一個簡單的 Event Bus (shared/eventBus.js)
# 讓 Agent 之間可以發送事件:

// Order Agent 發送事件
eventBus.emit('order:created', { orderId, userId, amount })

// Payment Agent 監聽事件
eventBus.on('order:created', async (data) => {
  // 自動建立付款請求
})

// Product Agent 監聽事件
eventBus.on('order:created', async (data) => {
  // 扣除庫存
})

💡 最佳實踐

  • 明確分工:每個 Agent 專注一個職責
  • 統一介面:所有 Agent 使用相同的錯誤格式、回應格式
  • 獨立測試:每個 Agent 都要有自己的測試
  • 文件先行:先定義好 API 規格,再請 Claude Code 實作
  • 版本控制:使用 Git 追蹤每個 Agent 的變更
  • 漸進整合:一個一個 Agent 加入,不要一次全部
點點學院 banner
延伸閱讀
喜歡這篇內容?到點點學院看更多完整課程
從工具理解到實戰應用,點點學院整理了更多 AI、工作流與教學型內容,適合繼續往下學。
前往點點學院 →