BrainBank
AI 课堂/技能Claude Code Deep Dive

AgentTool:子 Agent 调度器

2026/7/31 20:55:39 · 更新于 2026/7/31 21:00:46

#claude-code#agenttool#skill#sub-agent#task-scheduling

AgentTool 是 Claude Code 中的受控派单器,用于在主线程任务过大时创建并管理子 Agent 执行子任务。

AgentTool 是 Claude Code 的核心调度组件,它将“任务拆分、子 Agent 执行与生命周期管理”封装为正式工具,标志着该框架从单线程代码助手向多任务 Agent 系统的架构升级。它通过受控的派发机制、独立的上下文裁剪与完整的工作流绑定,解决了复杂任务场景下的并行处理与主线程上下文污染问题。

解决的核心问题与工具定位

AgentTool 是 Claude Code 最有代表性的工具之一。它解决的不是“读文件”或者“跑命令”这种单点能力,而是:

当主线程模型觉得一个任务太大、太杂、太适合并行时,如何把一部分工作拆给另一个 Agent 去做。

这也是 Claude Code 和普通代码助手的核心分水岭之一。很多 AI 工具只有一个主线程模型一直往下跑,而 Claude Code 明确支持:

  • 研究型子任务
  • 后台执行
  • 多 Agent 协作
  • 本地 / 远程子 Agent

核心参数暴露

tools/AgentTool/AgentTool.tsx 一上来就把核心参数暴露出来了:

const baseInputSchema = z.object({
  description: z.string().describe('A short (3-5 word) description of the task'),
  prompt: z.string().describe('The task for the agent to perform'),
  subagent_type: z.string().optional(),
  model: z.enum(['sonnet', 'opus', 'haiku']).optional(),
  run_in_background: z.boolean().optional(),
})

这几个字段已经很说明它的定位:

  • description:给子任务一个简短标题
  • prompt:真正交给子 Agent 的工作内容
  • subagent_type:选择专门类型的 Agent
  • model:必要时换模型
  • run_in_background:放后台跑

也就是说,AgentTool 本质上是一个任务派发器


在系统架构中的位置

在 Claude Code 的总工具池里,它被放在最前面:

export function getAllBaseTools(): Tools {
  return [
    AgentTool,
    TaskOutputTool,
    BashTool,
    ...
  ]
}

这不一定代表“最常调用”,但说明 Anthropic 把它视为第一层核心能力。因为一旦有了 AgentTool,其他工具就不再只是“主线程自己用”,而是可以被子 Agent 继续调用。

它在系统里的位置:

image.png


底层机制与受控派单逻辑

并非“再开一个模型”那么简单

AgentTool.tsx 的导入就能看出来,这个工具背后其实串着很多子系统:

import { enhanceSystemPromptWithEnvDetails, getSystemPrompt } from '../../constants/prompts.js'
import { assembleToolPool } from '../../tools.js'
import { runAgent } from './runAgent.js'
import { registerAsyncAgent } from '../../tasks/LocalAgentTask/LocalAgentTask.js'
import { registerRemoteAgentTask } from '../../tasks/RemoteAgentTask/RemoteAgentTask.js'

这意味着 AgentTool 至少做了 4 件事:

  1. 重新生成子 Agent 的 system prompt
  2. 重新裁剪一套工具池
  3. 决定是本地任务还是远程任务
  4. 把子 Agent 挂到任务系统里

所以它的真实含义更接近:

用 Claude Code 的运行时框架,再启动一个受控的、带任务上下文的工作线程

子 Agent 为什么不会变成“失控副本”

如果只是简单 fork 一个模型,系统很快就会失控。Claude Code 通过几层机制约束它:

  • 子 Agent 有自己的输入 schema
  • 子 Agent 会重新组装 prompt
  • 子 Agent 的工具池是单独过滤的
  • 子 Agent 会被挂进任务系统,支持状态、输出、停止、通知

所以 AgentTool 不是一个“自由分身器”,而是一个受控派单器

调用链可进一步细看:

image.png


任务生命周期绑定与工具协作

与 Task 系统的强绑定

Claude Code 不是让子 Agent 偷偷在后台跑,而是把它们做成了正式任务:

  • 可查看输出
  • 可后台执行
  • 可停止
  • 可恢复

这就是为什么 AgentTool 后面紧跟着就是 TaskOutputTool。两者天然是一组:

  • AgentTool 负责派活
  • TaskOutputTool 负责取结果

典型使用路径

可以把一个典型路径理解成这样:

  1. 主线程发现“这个问题需要深入查某个子系统”
  2. 主线程调用 AgentTool
  3. 子 Agent 自己去搜索、读文件、推理、执行
  4. 子 Agent 给出压缩后的结论
  5. 主线程拿这个结论继续向前推进

这和“主线程自己执行所有搜索”相比,最大的优势是:

  • 减少主上下文污染
  • 容易并行
  • 更适合做重研究、重排查类任务

与相邻工具的关系

image.png

  • TaskOutputTool 配合:读取子 Agent 输出
  • SendMessageTool 配合:多 Agent 模式下互相通信
  • BashToolReadEdit 配合:子 Agent 自己继续完成任务
  • SkillTool 配合:某些 skill 会在独立子 Agent 中执行

常见误解澄清

🚫 误解一:AgentTool 就是多轮聊天

不是。 它是明确的工具调用,带 schema、任务生命周期和结果回注。

🚫 误解二:子 Agent 和主线程完全一样

也不是。 子 Agent 的 prompt、工具池、运行模式都可能不同。

🚫 误解三:AgentTool 只是“更高级的 prompt”

不够准确。 它更像“把另一个 Agent 作为正式运行时对象挂进系统”。


Key takeaways

  • AgentTool 不是单点能力工具,而是 Claude Code 封装任务拆分、子进程执行与生命周期管理的调度中枢
  • 通过独立 schema、prompt 重裁、工具池隔离与 Task 系统绑定,子 Agent 被严格约束为受控派单对象,而非自由分身。
  • TaskOutputTool 天然成对,形成“主线程派发 → 上下文隔离处理 → 结果回流”的标准化工作流。
  • 核心业务价值在于降低主上下文污染、开启原生并行能力,为研究型与重排查类任务提供架构级支撑。

学习地图

Learning Map

Stage 1 – Foundations

  • Understand the purpose of AgentTool and how it differs from single‑call tools.
  • Read the base input schema (description, prompt, subagent_type, model, run_in_background).

Stage 2 – Integration Basics

  • Learn how AgentTool is positioned in the global tool pool (first entry).
  • Explore the four internal steps it performs: system‑prompt enhancement, tool‑pool pruning, task type selection, and registration with the task system.

Stage 3 – Practical Use

  • Pair AgentTool with TaskOutputTool to retrieve sub‑agent results.
  • Experiment with background execution and model overrides.

Stage 4 – Advanced Patterns

  • Combine AgentTool with SendMessageTool for multi‑Agent communication.
  • Use BashTool, Read/Edit tools inside a sub‑agent for complex workflows.
  • Implement custom subagent_type to specialize agents for research, debugging, or data extraction.

Stage 5 – Production Ready

  • Monitor task lifecycle (status, stop, resume) via the task system UI.
  • Apply best‑practice patterns for error handling and result validation.

动手实践——分步指南

  1. Open your Claude Code project and locate tools/AgentTool/AgentTool.tsx.
  2. Add a new entry in your workflow script that calls AgentTool with:
    {
      "description": "Search API docs",
      "prompt": "Find the authentication flow for the Anthropic API and summarize it.",
      "subagent_type": "research",
      "model": "sonnet",
      "run_in_background": true
    }
    
  3. Immediately after the AgentTool call, invoke TaskOutputTool with the task ID returned in step 2 to fetch the sub‑agent’s result.
  4. Print the retrieved summary to the console or feed it into the next stage of your main agent.
  5. Test the flow:
    • Run the script.
    • Verify that a background sub‑agent is created (check the task dashboard).
    • Confirm the output matches the expected API documentation summary.
  6. Experiment: change run_in_background to false and observe synchronous execution, then try model": "opus" for a higher‑capacity model.

三大推荐资源

  1. 1
    Anthropic Claude Documentation – Tool Use

    Official guide that explains how Claude tools work, including schema definitions and execution semantics.

    https://docs.anthropic.com/claude/docs/tool-use

  2. 2
    Claude API Reference – Tools Section

    Detailed reference for all built‑in tools, with examples of AgentTool usage and parameters.

    https://docs.anthropic.com/claude/reference/tools

  3. 3
    Anthropic News – Introducing Claude Code

    Announcement blog post describing the multi‑agent capabilities of Claude Code and the role of AgentTool as a task dispatcher.

    https://www.anthropic.com/news/claude-code

链接由 AI 推荐——使用前建议快速核实。