主题
追踪 Anthropic 应用
Python(wrap_anthropic)和 TypeScript(wrapAnthropic)中的 Anthropic 包装方法允许你包装 Anthropic 客户端,以自动记录追踪。使用包装器可确保消息(包括工具调用和多模态内容块)在 LangSmith 中良好呈现。该包装器可与 @traceable 装饰器(Python)或 traceable 函数(TypeScript)无缝配合,因此你可以用包装器追踪 Anthropic 调用,并用装饰器或函数追踪应用的其他部分。
该包装器还支持 Claude managed agents(仅 TypeScript)。请参阅追踪 Claude managed agents。
NOTE
即使使用 wrap_anthropic 或 wrapAnthropic,也必须将 LANGSMITH_TRACING 环境变量设为 'true',追踪才会记录到 LangSmith。这样你无需修改代码即可开启或关闭追踪。
此外,你还需要将 LANGSMITH_API_KEY 环境变量设置为你的 API key(更多信息见设置。)
如果你的 LangSmith API key 关联了多个工作区,请设置 LANGSMITH_WORKSPACE_ID 环境变量以指定使用哪个工作区。
默认情况下,追踪会记录到名为 default 的项目。要将追踪记录到其他项目,请参阅将追踪记录到指定项目。
python
import anthropic
from langsmith import traceable
from langsmith.wrappers import wrap_anthropic
client = wrap_anthropic(anthropic.Anthropic())
@traceable(run_type="tool", name="Retrieve Context")
def my_tool(question: str) -> str:
return "During this morning's meeting, we solved all world conflict."
@traceable(name="Chat Pipeline")
def chat_pipeline(question: str):
context = my_tool(question)
messages = [
{ "role": "user", "content": f"Question: {question}\nContext: {context}"}
]
message = client.messages.create(
model="claude-sonnet-4-6",
messages=messages,
max_tokens=1024,
system="You are a helpful assistant. Please respond to the user's request only based on the given context."
)
return message
chat_pipeline("Can you summarize this morning's meetings?")typescript
import Anthropic from "@anthropic-ai/sdk";
import { traceable } from "langsmith/traceable";
import { wrapAnthropic } from "langsmith/wrappers/anthropic";
const client = wrapAnthropic(new Anthropic());
const myTool = traceable(async (question: string) => {
return "During this morning's meeting, we solved all world conflict.";
}, { name: "Retrieve Context", run_type: "tool" });
const chatPipeline = traceable(async (question: string) => {
const context = await myTool(question);
const messages = [
{ role: "user", content: `Question: ${question}\nContext: ${context}` }
];
const message = await client.messages.create({
model: "claude-sonnet-4-6",
messages: messages,
max_tokens: 1024,
system: "You are a helpful assistant. Please respond to the user's request only based on the given context."
});
return message;
}, { name: "Chat Pipeline" });
await chatPipeline("Can you summarize this morning's meetings?");追踪 Claude managed agents
wrapAnthropic 包装器还支持 Claude managed agents(仅 TypeScript)。用 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 的多代理架构中,完整追踪子代理需要 接入单独的事件流,目前尚不支持。只有顶层 会话事件会被追踪。