Strands Agents 是一个用于构建模型驱动代理的 SDK。LangSmith 提供了 Strands Agents 集成,可导出 LangSmith 兼容格式的 Strands OpenTelemetry span,包括代理运行、模型调用、工具调用、提示词、补全和 token 使用情况。
安装
安装带有 Strands Agents 支持的 LangSmith:
pip install "langsmith[strands-agents]"
uv add "langsmith[strands-agents]"
这将安装 LangSmith、Strands Agents、Strands Agents 工具和 OpenTelemetry OTLP HTTP 导出器。
设置
1. 配置环境变量
设置您的 LangSmith API 密钥 和项目名称。如果您使用 Amazon Bedrock 作为 Strands Agents 的模型提供商,还需要使用您首选的 AWS 身份验证方法配置 AWS 凭证。
# Required when using Amazon Bedrock.
2. 启用 Strands Agents 遥测
在应用程序启动时创建或调用代理之前调用一次 setup_langsmith_telemetry() :
from langsmith.integrations.strands_agents import setup_langsmith_telemetry
setup_langsmith_telemetry()
对于本地调试,传递 console=True 也可以打印转换后的 span 到标准输出:
setup_langsmith_telemetry(console=True)
3. 创建并运行您的代理
配置完成后,Strands Agents 追踪将自动导出到 LangSmith:
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 导出器,请使用 LangSmithSpanExporter 创建 create_langsmith_exporter() 并将其附加到 Strands 追踪器提供程序:
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))
当您希望通过代码而不是环境变量来配置导出器选项时,请使用此方法。