> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 追踪 Claude Agent SDK 应用

[Claude Agent SDK](https://platform.claude.com/docs/en/agent-sdk/overview) 是一个用于构建基于 Claude 的智能体应用的 SDK。LangSmith 提供了与 Claude Agent SDK 的原生集成，能够自动追踪您的智能体执行过程、工具调用以及与 Claude 模型的交互。

## 安装

安装 Claude Agent SDK 的 LangSmith 集成

{/* Source: https://github.com/langchain-ai/ls-integration-examples/tree/main/integrations/claude-agent-sdk/ */}

<CodeGroup>
  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langsmith[claude-agent-sdk]
  ```

  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install langsmith[claude-agent-sdk]
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @anthropic-ai/claude-agent-sdk langsmith zod
  ```

  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @anthropic-ai/claude-agent-sdk langsmith zod
  ```
</CodeGroup>

## 设置

设置您的 [API 密钥](/langsmith/create-account-api-key)：

{/* Source: https://github.com/langchain-ai/ls-integration-examples/tree/main/integrations/claude-agent-sdk/ */}

<CodeGroup>
  ```bash shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  export LANGSMITH_TRACING=true
  export LANGSMITH_ENDPOINT=https://api.smith.langchain.com
  export LANGSMITH_API_KEY=<your_langsmith_api_key>
  export LANGSMITH_PROJECT=<your_langsmith_project>

  export ANTHROPIC_API_KEY=<your_anthropic_api_key>
  ```

  ```dotenv .env theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  LANGSMITH_TRACING=true
  LANGSMITH_ENDPOINT=https://api.smith.langchain.com
  LANGSMITH_API_KEY=<your_langsmith_api_key>
  LANGSMITH_PROJECT=<your_langsmith_project>

  ANTHROPIC_API_KEY=<your_anthropic_api_key>
  ```
</CodeGroup>

您可以在 [LangSmith UI](https://smith.langchain.com) 的 **设置** 中找到您的 LangSmith API 密钥和项目名称。

关于 Anthropic API 密钥，请参考 [Claude 控制台](https://claude.ai/login)。

## 快速开始

要为您的 Claude Agent SDK 应用启用 LangSmith 追踪，请在应用启动时调用 `configure_claude_agent_sdk()`：

{/* 来源：https://github.com/langchain-ai/ls-integration-examples/tree/main/integrations/claude-agent-sdk/ */}

<CodeGroup>
  ```python Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import asyncio
  from typing import Any

  from claude_agent_sdk import (
      ClaudeAgentOptions,
      ClaudeSDKClient,
      create_sdk_mcp_server,
      tool,
  )
  from langsmith.integrations.claude_agent_sdk import configure_claude_agent_sdk

  configure_claude_agent_sdk()


  @tool(
      "get_weather",
      "Gets the current weather for a given city",
      {"city": str},
  )
  async def get_weather(args: dict[str, Any]) -> dict[str, Any]:
      city = args["city"]
      weather_data = {
          "San Francisco": "Foggy, 62°F",
          "New York": "Sunny, 75°F",
          "London": "Rainy, 55°F",
          "Tokyo": "Clear, 68°F",
      }
      weather = weather_data.get(city, "Weather data not available")
      return {"content": [{"type": "text", "text": f"Weather in {city}: {weather}"}]}


  async def main() -> None:
      weather_server = create_sdk_mcp_server(
          name="weather",
          version="1.0.0",
          tools=[get_weather],
      )

      options = ClaudeAgentOptions(
          model="claude-sonnet-4-5-20250929",
          system_prompt="You are a friendly travel assistant who helps with weather information.",
          mcp_servers={"weather": weather_server},
          allowed_tools=["mcp__weather__get_weather"],
      )

      async with ClaudeSDKClient(options=options) as client:
          await client.query("What's the weather like in San Francisco and Tokyo?")

          async for message in client.receive_response():
              print(message)


  if __name__ == "__main__":
      asyncio.run(main())
  ```

  ```typescript TypeScript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import * as originalSdk from '@anthropic-ai/claude-agent-sdk';

  import { wrapClaudeAgentSDK } from 'langsmith/experimental/anthropic';
  import { z } from 'zod/v4';

  const sdk = wrapClaudeAgentSDK(originalSdk);

  const getWeather = sdk.tool(
    'get_weather',
    'Gets the current weather for a given city',
    {
      city: z.string(),
    },
    async ({ city }) => {
      const weatherData: Record<string, string> = {
        'San Francisco': 'Foggy, 62°F',
        'New York': 'Sunny, 75°F',
        London: 'Rainy, 55°F',
        Tokyo: 'Clear, 68°F',
      };
      const weather = weatherData[city] ?? 'Weather data not available';
      return {
        content: [{ type: 'text' as const, text: weather }],
      };
    }
  );

  const weatherServer = sdk.createSdkMcpServer({
    name: 'weather',
    version: '1.0.0',
    tools: [getWeather],
  });

  const query = sdk.query({
    prompt: "What's the weather like in San Francisco and Tokyo?",
    options: {
      model: 'claude-sonnet-4-5-20250929',
      systemPrompt:
        'You are a friendly travel assistant who helps with weather information.',
      mcpServers: { weather: weatherServer },
      allowedTools: ['mcp__weather__get_weather'],
    },
  });

  for await (const chunk of query) {
    console.log(chunk);
  }
  ```
</CodeGroup>

配置完成后，所有 Claude Agent SDK 操作都将自动追踪到 LangSmith，包括：

* 智能体查询与响应
* 工具调用与结果
* Claude 模型交互
* MCP 服务器操作

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\langsmith\trace-claude-agent-sdk.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
