在 Litefuse 中 trace Anthropic 模型
Anthropic 提供了像 Claude 这样的高级语言模型,以安全、可靠以及出色的推理能力著称。把 Anthropic 的模型与 Litefuse 结合,你可以在开发和生产环境中对 AI 工作负载进行 trace、监控和分析。
本 notebook 演示两种在 Litefuse 中使用 Anthropic 模型的方式:
- OpenTelemetry Instrumentation: 使用
AnthropicInstrumentor库包装 Anthropic SDK 的调用,将 OpenTelemetry span 发送到 Litefuse。 - OpenAI SDK: 通过 Litefuse 的 OpenAI SDK 包装器使用 Anthropic 的 OpenAI 兼容接口。
什么是 Anthropic?
Anthropic 是一家 AI 安全公司,开发了 Claude 这一系列旨在做到有用、无害且诚实的大语言模型。Claude 在复杂推理、分析和创造性任务上表现出色。
什么是 Litefuse?
Litefuse 是一个开源的 LLM 可观测性与监控平台。它通过捕获元数据、prompt 详情、token 用量、延迟等信息,帮助你 trace 和监控自己的 AI 应用。
步骤 1:安装依赖
在开始之前,先在你的 Python 环境中安装必要的包:
%pip install anthropic openai langfuse opentelemetry-instrumentation-anthropic步骤 2:配置 Langfuse SDK
接着配置你的 Litefuse API keys。你可以通过免费注册 Litefuse Cloud 或自托管 Litefuse 来获取这些 keys。这些环境变量对于 Langfuse 客户端的鉴权以及把数据发送到你的 Litefuse 项目至关重要。
同时也设置好你的 Anthropic API key(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!
方式 1:OpenTelemetry Instrumentation
使用 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)方式 2:把 OpenAI SDK 当作 drop-in 替代
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 表,你会看到应用执行过程的详细 trace,包含 agent 对话、LLM 调用、输入输出以及性能指标等信息。

也可以在这里查看 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:
- Call
langfuse.flush()at the end of your application to ensure all observations are exported. - Verify that you are using the correct API keys and base URL.
- Call
- 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: