Skip to content
LangSmith 文档版本 跟随上游 源快照 ab283103 更新于 2026-10-18

追踪 Claude Managed Agents

使用 LangSmith 自动追踪 Claude Managed Agent 的会话和事件。

Claude Managed Agents 是 Anthropic 托管、基于云的代理,运行在受管环境中。LangSmith 支持通过 wrapAnthropic 包装器追踪 Claude Managed Agent 会话,让你可以查看代理创建、会话事件和消息流。

NOTE

Claude Managed Agents 追踪目前仅支持 TypeScript。

安装

bash
npm install langsmith @anthropic-ai/sdk
bash
yarn add langsmith @anthropic-ai/sdk

环境设置

bash
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-langsmith-api-key>
export ANTHROPIC_API_KEY=<your-anthropic-api-key>

追踪 Claude Managed Agents

用 wrapAnthropic 包装 Anthropic 客户端。包装器会自动追踪代理创建、会话创建以及流经该会话的所有事件。

typescript
import Anthropic from "@anthropic-ai/sdk";
import { wrapAnthropic } from "langsmith/wrappers/anthropic";

const anthropic = wrapAnthropic(new Anthropic());

// Create a managed agent
const agent = await anthropic.beta.agents.create({
  name: "my-agent",
  model: "claude-opus-4-8",
  system: "You are a helpful assistant.",
  tools: [
    // ... your tools here
  ],
});

// Create a cloud environment for the agent to run in
const environment = await anthropic.beta.environments.create({
  name: "my-environment",
  config: {
    type: "cloud",
    networking: { type: "unrestricted" },
  },
});

// Create a session connecting the agent and environment
const session = await anthropic.beta.sessions.create({
  agent: agent.id,
  environment_id: environment.id,
  title: "My session",
});

// Stream session events
const stream = await anthropic.beta.sessions.events.stream(session.id);

// Send a message to the agent
await anthropic.beta.sessions.events.send(session.id, {
  events: [
    {
      type: "user.message",
      content: [
        {
          type: "text",
          text: "Hello! Can you help me with something?",
        },
      ],
    },
  ],
});

// Consume the event stream until the session is idle
for await (const event of stream) {
  if (event.type === "session.status_idle") {
    break;
  }
}

NOTE

在 Anthropic 的多代理架构中,完整追踪子代理需要 接入单独的事件流,目前尚不支持。只有顶层 会话事件会被追踪。

后续步骤


BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。