集成模型服务商Anthropic (Python)

在 Litefuse 中追踪 Anthropic 模型

Python JS/TS

Anthropic 提供了 Claude 等先进的语言模型,以安全性、有用性和强大的推理能力著称。通过将 Anthropic 的模型与 Litefuse 结合使用,你可以在开发和生产环境中追踪、监控和分析你的 AI 工作负载。

本 notebook 演示了将 Anthropic 模型与 Litefuse 集成的两种不同方式:

  1. OpenTelemetry 插桩:使用 AnthropicInstrumentor 库包装 Anthropic SDK 调用,并将 OpenTelemetry span 发送到 Litefuse。
  2. OpenAI SDK:通过 Litefuse 的 OpenAI SDK 包装器 使用 Anthropic 兼容 OpenAI 的端点。

什么是 Anthropic?
Anthropic 是一家 AI 安全公司,开发了 Claude——一系列旨在做到有用、无害和诚实的大语言模型。Claude 模型在复杂推理、分析和创造性任务上表现优异。

什么是 Litefuse?
Litefuse 是一个用于 LLM 可观测性和监控的开源平台。它通过捕获元数据、prompt 详情、token 用量、延迟等信息,帮助你追踪和监控 AI 应用。

第一步:安装依赖

开始之前,请在你的 Python 环境中安装所需的依赖包:

%pip install anthropic openai langfuse opentelemetry-instrumentation-anthropic

第二步:配置 Langfuse SDK

接下来,设置你的 Litefuse API Key。你可以通过注册免费的 Litefuse Cloud 账户或自托管 Litefuse 来获取这些密钥。这些环境变量对于 Langfuse 客户端进行身份认证并向你的 Litefuse 项目发送数据来说至关重要。

同时设置你的 Anthropic API(Anthropic Console)。

import os
 
# Get keys for your project from the project settings page: https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..." 
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..." 
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
 
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."  # Your Anthropic API key

设置好环境变量后,我们就可以初始化 Langfuse 客户端了。get_client() 会使用环境变量中提供的凭据初始化 Langfuse 客户端。

from langfuse import get_client
 
langfuse = get_client()
 
# Verify connection
if langfuse.auth_check():
    print("Langfuse client is authenticated and ready!")
else:
    print("Authentication failed. Please check your credentials and host.")

Langfuse client is authenticated and ready!

方式一:OpenTelemetry 插桩

使用 AnthropicInstrumentor 库包装 Anthropic SDK 调用,并将 OpenTelemetry span 发送到 Litefuse。

from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
 
AnthropicInstrumentor().instrument()
from anthropic import Anthropic
 
# Initialize the Anthropic client
client = Anthropic(
    api_key=os.environ.get("ANTHROPIC_API_KEY")
)
 
# Make the API call to Anthropic
message = client.messages.create(
    model="claude-opus-4-20250514",
    max_tokens=1000,
    temperature=1,
    system="You are a world-class poet. Respond only with short poems.",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Why is the ocean salty?"
                }
            ]
        }
    ]
)
print(message.content)

方式二:作为 OpenAI SDK 的替换方案

Anthropic 提供了兼容 OpenAI 的端点,让你可以使用 OpenAI SDK 与 Claude 模型交互。如果你已有使用 OpenAI SDK 的代码并希望切换到 Claude,这种方式会特别有用。

# Langfuse OpenAI client
from langfuse.openai import OpenAI
 
client = OpenAI(
    api_key=os.environ.get("ANTHROPIC_API_KEY"),  # Your Anthropic API key
    base_url="https://api.anthropic.com/v1/"  # Anthropic's API endpoint
)
 
response = client.chat.completions.create(
    model="claude-opus-4-20250514", # Anthropic model name
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who are you?"}
    ],
)
 
print(response.choices[0].message.content)

在 Litefuse 中查看 trace

执行应用后,前往你的 Litefuse Trace Table。你将看到应用执行的详细 trace,包括 Agent 对话、LLM 调用、输入、输出和性能指标等信息。

Litefuse trace

你也可以在 Litefuse 中查看 trace:

Interoperability with the Python SDK

You can use this integration together with the Litefuse SDKs to add additional attributes to the observation.

The @observe() decorator provides a convenient way to automatically wrap your instrumented code and add additional attributes to the observation.

from langfuse import observe, propagate_attributes, get_client
 
langfuse = get_client()
 
@observe()
def my_llm_pipeline(input):
    # Add additional attributes (user_id, session_id, metadata, version, tags) to all spans created within this execution scope
    with propagate_attributes(
        user_id="user_123",
        session_id="session_abc",
        tags=["agent", "my-observation"],
        metadata={"email": "user@litefuse.ai"},
        version="1.0.0"
    ):
 
        # YOUR APPLICATION CODE HERE
        result = call_llm(input)
 
        return result
 
# Run the function
my_llm_pipeline("Hi")

Learn more about using the Decorator in the Langfuse SDK instrumentation docs.

Troubleshooting

No observations appearing

First, enable debug mode in the Python SDK:

export LANGFUSE_DEBUG="True"

Then run your application and check the debug logs:

  • OTel observations appear in the logs: Your application is instrumented correctly but observations are not reaching Litefuse. To resolve this:
    1. Call langfuse.flush() at the end of your application to ensure all observations are exported.
    2. Verify that you are using the correct API keys and base URL.
  • No OTel spans in the logs: Your application is not instrumented correctly. Make sure the instrumentation runs before your application code.
Unwanted observations in Litefuse

The Langfuse SDK is based on OpenTelemetry. Other libraries in your application may emit OTel spans that are not relevant to you. These still count toward your billable units, so you should filter them out. See Unwanted spans in Litefuse for details.

Missing attributes

Some attributes may be stored in the metadata object of the observation rather than being mapped to the Litefuse data model. If a mapping or integration does not work as expected, please raise an issue on GitHub.

Next Steps

Once you have instrumented your code, you can manage, evaluate and debug your application:

这个页面对你有帮助吗?