自訂 Agents

使用 Claude Code 進行軟體開發

Dani Zysman

AI Curriculum Manager @ DataCamp

什麼是 Agents?

一組配備不同工具的專業機器人助理團隊

Agents 是 Claude Code 裡的專用 AI 助理:

  • 各自具備特定能力,獨立運作
  • 可為內建或自訂建立
使用 Claude Code 進行軟體開發

內建 Agents

Agent 目的 何時使用
Explore 只讀瀏覽程式碼庫 「Map this architecture」
Plan 研究與擬定策略 「How should we approach this?」
General 以完整工具執行工作 多數情境的預設

使用 /agents 列出所有可用 agents。

使用 Claude Code 進行軟體開發

為何建立自訂 Agents?

針對專門任務的自訂 agents

  • 優點:可重複使用、一致、可與團隊共享
  • 常見情境:
    • 程式碼審查-落實團隊規範
    • 安全稽核-檢查弱點
    • 文件撰寫-產生一致文件
    • 測試-建議測試案例
使用 Claude Code 進行軟體開發

建立 Agent 檔案

  • 檔案位置:
    • .claude/agents/code-reviewer.md
  • 必要段落:
    • name:Agent 識別名稱
    • description:觸發條件說明
    • Body:Markdown 說明與指示
___
# code-reviewer.md
name: code-reviewer
description: Reviews code for 
best practices
___
Check for:
1. Clear naming conventions
2. Proper error handling
3. Performance concerns
使用 Claude Code 進行軟體開發

建立程式碼審查自訂 Agent

> /agents

> [Select] Create new agent
> [Select] Project level (.claude/agents)
> [Select] Generate with Claude (recommended)
> Create a code reviewer agent that reviews code for naming, error handling, and performance.

檢視並核可產生的檔案。

使用 Claude Code 進行軟體開發

目前專案結構

music-analytics-api/
  app.py
  routes.py        # <- 我們要審查這個檔案
  models.py
  analytics.py
  CLAUDE.md
  .claude/
    agents/
      code-reviewer.md  # <- 我們的自訂 agent
使用 Claude Code 進行軟體開發

何時會產生子 Agent

無子 agent(直接處理):

"Fix the bug in routes.py"
  -> Claude handles it

"Add validation here"
  -> Claude handles it

會啟動子 agent:

"Map the architecture"
  -> Explore agent

"How should we refactor?"
  -> Plan agent
使用 Claude Code 進行軟體開發

使用自訂 Agent

> @code-reviewer review @routes.py
Review Complete

Quick Fixes:
- Remove unused `session` import (line 3)
- Replace hardcoded `* 5` popularity metric with a named constant

Structural Issues:
- Album dictionaries built twice in get_album_stats() and
  get_average_tracks() extract to a shared helper
- get_artist_albums() returns empty list if artist doesn't exist;
  validate artist first
- No range validation on artist_id parameter

Would you like me to fix any of these?
使用 Claude Code 進行軟體開發

Agent 的好處

帶防護罩的放大鏡,安全探索

  • 獨立脈絡
    • Agents 各自隔離運作
    • 主對話保持乾淨
  • 安全探索
    • Explore 為唯讀
    • 適合不熟悉的程式碼庫
使用 Claude Code 進行軟體開發

何時使用 Agents

問題類型 系統動作
「What does this function do?」 由 Claude 直接回答
「How does auth work across this codebase?」 交由 Explore 調查
「What's the best approach to refactor this?」 交由 Plan 研究
「code review @routes.py」 交由自訂 agent 處理
使用 Claude Code 進行軟體開發

一起來練習吧!

使用 Claude Code 進行軟體開發

Preparing Video For Download...