跳到主要内容
Python 中的 wrap_anthropic 方法允许您包装您的 Anthropic 客户端,以便自动记录跟踪 — 无需装饰器或函数包装!使用包装器可确保消息(包括工具调用和多模态内容块)在 LangSmith 中得到良好呈现。包装器与 @traceable 装饰器或 traceable 函数无缝协作,您可以在同一应用程序中同时使用它们。
即使在使用 wrap_anthropic 时,也必须将 LANGSMITH_TRACING 环境变量设置为 'true',以便将跟踪记录到 LangSmith。这允许您在不更改代码的情况下打开和关闭跟踪。此外,您需要将 LANGSMITH_API_KEY 环境变量设置为您的 API 密钥(有关更多信息,请参阅设置)。如果您的 LangSmith API 密钥链接到多个工作区,请设置 LANGSMITH_WORKSPACE_ID 环境变量以指定要使用的工作区。默认情况下,跟踪将记录到名为 default 的项目。要将跟踪记录到不同的项目,请参阅此部分
import anthropic
from langsmith import traceable
from langsmith.wrappers import wrap_anthropic

client = wrap_anthropic(anthropic.Anthropic())

# You can also wrap the async client as well
# async_client = wrap_anthropic(anthropic.AsyncAnthropic())

@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}"}
    ]
    messages = client.messages.create(
      model="claude-sonnet-4-5-20250929",
      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 messages

chat_pipeline("Can you summarize this morning's meetings?")

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.