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
    将工作委托给智能体

    会话事件流

    发送事件、流式传输响应,以及在会话执行过程中中断或重定向会话。

    与 Claude Managed Agents 的通信是基于事件的。您向代理发送用户事件,并接收代理和会话事件以跟踪状态。

    所有 Managed Agents API 请求都需要 managed-agents-2026-04-01 beta 标头。SDK 会自动设置 beta 标头。

    事件类型

    事件在两个方向上流动。

    • 用户事件是您发送给代理以启动会话并在其进行过程中引导它的事件。
    • 会话事件、跨度事件和代理事件被发送给您,以便观察您的会话状态和代理进度。

    事件类型字符串遵循 {domain}.{action} 命名约定。

    每个事件都包括一个 processed_at 时间戳,指示事件在服务器端何时被记录。如果 processed_at 为 null,则意味着事件已由工具排队,将在前面的事件完成处理后处理。

    有关每个事件类型的完整架构,请参阅会话事件 API 参考。

    集成事件

    其他场景

    处理自定义工具调用

    当代理调用自定义工具时:

    1. 会话发出包含工具名称和输入的 agent.custom_tool_use 事件。
    2. 会话暂停,发出包含 stop_reason: requires_action 的 session.status_idle 事件。阻塞事件 ID 在 stop_reason.requires_action.event_ids 数组中。
    3. 在您的系统中执行工具,并为每个工具发送 user.custom_tool_result 事件,在 custom_tool_use_id 参数中传递事件 ID 以及结果内容。
    4. 一旦所有阻塞事件都被解决,会话就会转回 running。
    exec {fd}< <(curl -sS -N --fail-with-body \
      "https://api.anthropic.com/v1/sessions/$SESSION_ID/stream?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" \
      -H "Accept: text/event-stream")
    
    while IFS= read -r -u "$fd" line; do
      [[ $line == data:* ]] || continue
      data="${line#data: }"
      [[ $(jq -r '.type' <<<"$data") == "session.status_idle" ]] || continue
      case $(jq -r '.stop_reason.type // empty' <<<"$data") in
        requires_action)
          while IFS= read -r event_id; do
            # Look up the custom tool use event and execute it
            result=$(call_tool "$event_id")
            # Send the result back
            jq -n --arg id "$event_id" --arg result "$result" \
              '{events: [{type: "user.custom_tool_result", custom_tool_use_id: $id, content: [{type: "text", text: $result}]}]}' |
              curl -sS --fail-with-body \
                "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")
          ;;
        end_turn)
          break
          ;;
      esac
    done
    exec {fd}<&-

    工具确认

    当权限策略要求在工具执行前进行确认时:

    1. 会话发出 agent.tool_use 或 agent.mcp_tool_use 事件。
    2. 会话暂停,并发出包含 stop_reason: requires_action 的 session.status_idle 事件。阻止事件 ID 位于 stop_reason.requires_action.event_ids 数组中。
    3. 为每个事件发送 user.tool_confirmation 事件,在 tool_use_id 参数中传递事件 ID。将 result 设置为 "allow" 或 "deny"。使用 deny_message 来解释拒绝原因。
    4. 一旦所有阻止事件都被解决,会话将转换回 running。
    exec {fd}< <(curl -sS -N --fail-with-body \
      "https://api.anthropic.com/v1/sessions/$SESSION_ID/stream?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" \
      -H "Accept: text/event-stream")
    
    while IFS= read -r -u "$fd" line; do
      [[ $line == data:* ]] || continue
      data="${line#data: }"
      [[ $(jq -r '.type' <<<"$data") == "session.status_idle" ]] || continue
      case $(jq -r '.stop_reason.type // empty' <<<"$data") in
        requires_action)
          while IFS= read -r event_id; do
            # Approve the pending tool call
            jq -n --arg id "$event_id" \
              '{events: [{type: "user.tool_confirmation", tool_use_id: $id, result: "allow"}]}' |
              curl -sS --fail-with-body \
                "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")
          ;;
        end_turn)
          break
          ;;
      esac
    done
    exec {fd}<&-

    跟踪使用情况

    会话对象包含一个 usage 字段,其中包含累积的令牌统计信息。在会话进入空闲状态后获取会话以读取最新的总数,并使用它们来跟踪成本、强制执行预算或监控消耗。

    {
      "id": "sesn_01...",
      "status": "idle",
      "usage": {
        "input_tokens": 5000,
        "output_tokens": 3200,
        "cache_creation_input_tokens": 2000,
        "cache_read_input_tokens": 20000
      }
    }

    input_tokens 报告未缓存的输入令牌,output_tokens 报告会话中所有模型调用的总输出令牌。cache_creation_input_tokens 和 cache_read_input_tokens 字段反映提示缓存活动。缓存条目使用 5 分钟的 TTL,因此在该时间窗口内的连续轮次受益于缓存读取,这降低了每个令牌的成本。

    Was this page helpful?