Skip to content

Agent 工具

Agent 工具让 Claude Code 可以"分身"——起子 Agent 处理独立的子任务,主 Agent 继续干自己的活。这是复杂任务能高效完成的关键。

基本用法

typescript
Agent({
  description: "Search for API usage",
  prompt: "Find all places where the payment API is called",
  subagent_type: "Explore"
})

Agent 类型

类型用途可用工具
general-purpose通用任务全部工具
Explore代码搜索只读工具
Plan架构规划只读工具

Explore Agent

专门用于代码探索,速度快但只能读不能写。适合:

  • 找特定文件或函数
  • 搜索代码模式
  • 了解代码库结构

Plan Agent

设计实现方案,返回步骤计划。适合:

  • 规划复杂功能的实现路径
  • 评估架构权衡

并发执行

多个 Agent 可以并行运行:

typescript
// 两个 Agent 同时执行
Agent({ prompt: "查找所有 API 端点" })
Agent({ prompt: "查找所有数据库模型" })

后台执行

typescript
Agent({
  prompt: "执行完整的代码审查",
  run_in_background: true
})

后台 Agent 不阻塞主对话,完成后通知。

工作树隔离

typescript
Agent({
  prompt: "尝试重构这个模块",
  isolation: "worktree"
})

在 Git worktree 中运行,对主分支无影响。重构失败了直接丢掉,不用 git reset