Loading...
    • 构建
    • 管理
    • 模型与定价
    • 客户端 SDK
    • API 参考
    Search...
    ⌘K
    入门步骤
    Claude 简介快速入门
    使用 Claude 构建
    功能概览使用 Messages API处理停止原因
    模型能力
    扩展思考自适应思考努力程度快速模式(测试版:研究预览)结构化输出引用流式消息批量处理搜索结果流式拒绝多语言支持嵌入
    工具
    概览工具使用原理网页搜索工具网页抓取工具代码执行工具记忆工具Bash 工具计算机使用工具文本编辑器工具
    工具基础设施
    工具搜索程序化工具调用细粒度工具流式传输
    上下文管理
    上下文窗口压缩上下文编辑提示词缓存Token 计数
    文件处理
    Files APIPDF 支持图像与视觉
    技能
    概览快速入门最佳实践企业级技能API 中的技能
    MCP
    远程 MCP 服务器MCP 连接器
    提示词工程
    概览提示词最佳实践Console 提示词工具
    测试与评估
    定义成功标准并构建评估在 Console 中使用评估工具降低延迟
    加强安全护栏
    减少幻觉提高输出一致性防范越狱减少提示词泄露
    资源
    术语表
    发布说明
    Claude Platform
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    高级编排

    多智能体会话

    在单个会话中协调多个智能体。

    Was this page helpful?

    多智能体是一项研究预览功能。请求访问权限以试用。

    多智能体编排允许一个智能体与其他智能体协调以完成复杂工作。智能体可以并行运行,每个都有自己隔离的上下文,这有助于提高输出质量并缩短完成时间。

    所有托管智能体 API 请求都需要 managed-agents-2026-04-01 beta 标头。研究预览功能需要额外的 beta 标头。SDK 会自动设置这些 beta 标头。

    工作原理

    所有智能体共享同一个容器和文件系统,但每个智能体在自己的会话线程中运行,这是一个上下文隔离的事件流,具有自己的对话历史。协调器在主线程中报告活动(这与会话级事件流相同);当协调器决定委派时,会在运行时生成额外的线程。

    线程是持久的:协调器可以向之前调用过的智能体发送后续消息,该智能体会保留其之前所有轮次的内容。

    每个智能体使用自己的配置(模型、系统提示、工具、MCP 服务器和技能),如创建该智能体时定义的那样。工具和上下文不共享。

    委派什么

    多智能体会话在有多个范围明确、专业化的任务来完成总体目标时效果最佳:

    • 代码审查: 具有专注系统提示和只读工具的审查者智能体。
    • 测试生成: 编写和运行测试而不接触生产代码的测试智能体。
    • 研究: 具有网络工具的搜索智能体,将发现总结回协调器。

    声明可调用的智能体

    当定义你的智能体时,列出它被允许调用的其他智能体的 ID:

    orchestrator=$(curl -fsS https://api.anthropic.com/v1/agents \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      -d @- <<EOF
    {
      "name": "Engineering Lead",
      "model": "claude-sonnet-4-6",
      "system": "You coordinate engineering work. Delegate code review to the reviewer agent and test writing to the test agent.",
      "tools": [
        {
          "type": "agent_toolset_20260401"
        }
      ],
      "callable_agents": [
        {"type": "agent", "id": "$REVIEWER_AGENT_ID", "version": $REVIEWER_AGENT_VERSION},
        {"type": "agent", "id": "$TEST_WRITER_AGENT_ID", "version": $TEST_WRITER_AGENT_VERSION}
      ]
    }
    EOF
    )

    callable_agents 中的每个条目必须是现有智能体的 ID。仅支持一级委派:协调器可以调用其他智能体,但这些智能体不能调用自己的智能体。

    然后创建一个引用协调器的会话:

    可调用的智能体从协调器的配置中解析。你不需要在会话创建时引用它们。

    会话线程

    会话级事件流(/v1/sessions/:id/stream)被视为主线程,包含所有线程中所有活动的压缩视图。你不会看到被调用智能体的单个跟踪,但你会看到它们工作的开始和结束。会话线程是你深入了解特定智能体推理和工具调用的地方。

    会话状态也是所有智能体活动的聚合;如果至少一个线程处于 running 状态,那么整个会话状态也将是 running。

    列出会话中的所有线程如下:

    从特定线程流式传输事件:

    列出线程的过去事件:

    多智能体事件类型

    这些事件在顶级会话流上显示多智能体活动。

    类型描述
    session.thread_created协调器生成了一个新线程。包括 session_thread_id 和 model。
    session.thread_idle智能体线程完成了其当前工作。
    agent.thread_message_sent智能体向另一个线程发送了消息。包括 to_thread_id 和 content。
    agent.thread_message_received智能体从另一个线程接收了消息。包括 from_thread_id 和 content。

    线程中的工具权限和自定义工具

    当 callable_agent 线程需要来自你的客户端的某些东西(权限来运行 always_ask 工具,或自定义工具的结果)时,请求会在会话流上显示,带有 session_thread_id 字段。在发布你的响应时包括相同的 session_thread_id,以便平台将其路由回等待的线程。

    • session_thread_id 存在: 事件源自子智能体线程。在你的回复中回显它。
    • session_thread_id 不存在: 事件来自主线程。不带该字段回复。
    • 匹配 tool_use_id 以将请求与响应配对。

    下面的示例扩展了工具确认处理程序以路由回复。相同的模式适用于 user.custom_tool_result。

    session=$(curl -fsS https://api.anthropic.com/v1/sessions \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      -d '{"agent": "'$ORCHESTRATOR_ID'", "environment_id": "'$ENVIRONMENT_ID'"}')
    curl -fsS "https://api.anthropic.com/v1/sessions/$SESSION_ID/threads" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      | jq -r '.data[] | "[\(.agent_name)] \(.status)"'
    curl -fsSN "https://api.anthropic.com/v1/sessions/$SESSION_ID/threads/$THREAD_ID/stream" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" |
      while IFS= read -r line; do
        [[ $line == data:* ]] || continue
        json=${line#data: }
        case $(jq -r '.type' <<<"$json") in
          agent.message)
            printf '%s' "$(jq -j '.content[] | select(.type == "text") | .text' <<<"$json")"
            ;;
          session.thread_idle)
            break
            ;;
        esac
      done
    curl -fsS "https://api.anthropic.com/v1/sessions/$SESSION_ID/threads/$THREAD_ID/events" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      | jq -r '.data[] | "[\(.type)] \(.processed_at)"'
    while IFS= read -r event_id; do
      pending=$(jq -r --arg id "$event_id" '.[$id]' <<<"$events_by_id")
      thread_id=$(jq -r '.session_thread_id // empty' <<<"$pending")
      jq -n --arg id "$event_id" --arg thread "$thread_id" '
        {events: [
          {type: "user.tool_confirmation", tool_use_id: $id, result: "allow"}
          + (if $thread != "" then {session_thread_id: $thread} else {} end)
        ]}' |
        curl -fsS "https://api.anthropic.com/v1/sessions/$SESSION_ID/events?beta=true" \
          -H "x-api-key: $ANTHROPIC_API_KEY" \
          -H "anthropic-version: 2023-06-01" \
          -H "anthropic-beta: managed-agents-2026-04-01" \
          -H "content-type: application/json" \
          -d @-
    done < <(jq -r '.stop_reason.event_ids[]' <<<"$data")