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

# 追踪 Strands Agents 应用

> 使用 LangSmith 追踪 Strands Agents 应用。

[Strands Agents](https://strandsagents.com/) 是一个用于构建模型驱动代理的 SDK。LangSmith 提供了 Strands Agents 集成，该集成以 LangSmith 兼容的格式导出 Strands OpenTelemetry span，包括代理运行、模型调用、工具调用、提示、补全和 token 使用情况。

## 安装

安装支持 Strands Agents 的 LangSmith：

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install "langsmith[strands-agents]"
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add "langsmith[strands-agents]"
  ```
</CodeGroup>

这将安装 LangSmith、Strands Agents、Strands Agents 工具以及 OpenTelemetry OTLP HTTP 导出器。

## 设置

### 1. 配置环境变量

设置你的 [LangSmith API 密钥](/langsmith/create-account-api-key) 和项目名称。如果你将 Amazon Bedrock 用作 Strands Agents 的模型提供商，还需使用你偏好的 AWS 认证方法配置 AWS 凭证。

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export OTEL_EXPORTER_OTLP_ENDPOINT=https://api.smith.langchain.com/otel/v1/traces
export OTEL_EXPORTER_OTLP_HEADERS="x-api-key=<your_langsmith_api_key>,Langsmith-Project=<your_project_name>"

# 使用 Amazon Bedrock 时必需。
export AWS_REGION=<your_aws_region>
```

<Note>
  Strands Agents 集成使用标准的 OpenTelemetry OTLP 导出器。在调用 `setup_langsmith_telemetry()` 之前，请配置 LangSmith 端点和请求头。
</Note>

### 2. 启用 Strands Agents 遥测

在应用启动时，于创建或调用代理之前，调用一次 `setup_langsmith_telemetry()`：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langsmith.integrations.strands_agents import setup_langsmith_telemetry

setup_langsmith_telemetry()
```

对于本地调试，传入 `console=True` 以同时将转换后的 span 打印到标准输出：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
setup_langsmith_telemetry(console=True)
```

### 3. 创建并运行你的代理

配置完成后，Strands Agents 的追踪将自动导出到 LangSmith：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langsmith.integrations.strands_agents import setup_langsmith_telemetry
from strands import Agent

setup_langsmith_telemetry()

agent = Agent(
    system_prompt="You are a concise assistant.",
)

response = agent("Explain what LangSmith tracing is in one sentence.")
print(response)
```

## 在 LangSmith 中查看追踪

运行应用后，打开你的 LangSmith 项目以查看包含以下内容的追踪：

* 代理调用 span
* 事件循环周期 span
* 包含提示、补全和 token 使用情况的 LLM 调用 span
* 当你的代理使用工具时，包含工具输入和输出的工具调用 span

## 自定义 OTLP 导出器

如果你需要向底层的 OpenTelemetry 导出器传递自定义选项，请使用 `create_langsmith_exporter()` 创建一个 `LangSmithSpanExporter`，并将其手动附加到 Strands tracer provider：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langsmith.integrations.strands_agents import create_langsmith_exporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from strands.telemetry import StrandsTelemetry

telemetry = StrandsTelemetry()
exporter = create_langsmith_exporter(
    endpoint="https://api.smith.langchain.com/otel/v1/traces",
    headers={
        "x-api-key": "<your_langsmith_api_key>",
        "Langsmith-Project": "<your_project_name>",
    },
)
telemetry.tracer_provider.add_span_processor(BatchSpanProcessor(exporter))
```

当你希望在代码中而非通过环境变量配置导出器选项时，请使用此方法。

## 资源

* [Strands Agents 文档](https://strandsagents.com/)
* [LangSmith OpenTelemetry 指南](/langsmith/trace-with-opentelemetry)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/trace-with-strands-agents.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
