自定义 Agent

使用 Claude Code 进行软件开发

Dani Zysman

AI Curriculum Manager @ DataCamp

什么是 Agent?

一组携带不同工具的专业机器人助手

在 Claude Code 中,Agent 是专业化的 AI 助手:

  • 各自具备特定能力,独立工作
  • 可内置,亦可自定义创建
使用 Claude Code 进行软件开发

内置 Agent

Agent 目的 何时使用
Explore 只读代码库调研 "Map this architecture"
Plan 研究与制定策略 "How should we approach this?"
General 使用全套工具执行任务 大多数工作的默认选项

使用 /agents 列出全部可用 agent

使用 Claude Code 进行软件开发

为何创建自定义 Agent?

用于专项任务的自定义 agent

  • 优势: 可复用、一致、可与团队共享
  • 常见用例:
    • 代码评审——执行团队规范
    • 安全审计——检查漏洞
    • 文档——生成一致化文档
    • 测试——建议测试用例
使用 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 直接处理

"Add validation here"
  -> Claude 直接处理

触发子 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 的益处

用于安全探索的放大镜与防护盾

  • 上下文隔离
    • Agent 独立运行
    • 主对话保持干净
  • 安全探索
    • Explore agent 仅只读
    • 适合不熟悉的代码库
使用 Claude Code 进行软件开发

何时使用 Agent

问题类型 系统响应
"What does this function do?" Claude 直接回答
"How does auth work across this codebase?" Explore agent 调研
"What's the best approach to refactor this?" Plan agent 研究方案
"code review @routes.py" 由自定义 agent 处理
使用 Claude Code 进行软件开发

让我们一起练习吧!

使用 Claude Code 进行软件开发

Preparing Video For Download...