使用 Litefuse 追踪 Anthropic JS/TS
Anthropic 提供了 Claude 等先进的语言模型,以安全性、有用性和强大的推理能力著称。通过将 Anthropic 的 JS/TS SDK 与 Litefuse 结合使用,你可以在开发和生产环境中追踪、监控和分析你的 AI 工作负载。
本 notebook 演示了如何使用来自 OpenInference 的 AnthropicInstrumentation 库,自动对 Anthropic SDK 调用进行插桩,并将 OpenTelemetry span 发送到 Litefuse。
什么是 Anthropic?
Anthropic 是一家 AI 安全公司,开发了 Claude——一系列旨在做到有用、无害和诚实的大语言模型。Claude 模型在复杂推理、分析和创造性任务上表现优异。
什么是 Litefuse?
Litefuse 是一个用于 LLM 可观测性和监控的开源平台。它通过捕获元数据、prompt 详情、token 用量、延迟等信息,帮助你追踪和监控 AI 应用。
第一步:安装依赖
安装所需的依赖包:
npm install @anthropic-ai/sdk @arizeai/openinference-instrumentation-anthropic @langfuse/otel @opentelemetry/sdk-node注意:本指南使用 Deno.js 运行,导入包和设置环境变量的语法略有不同。对于 Node.js 应用,配置过程类似,但使用标准
npm包和process.env。
第二步:配置环境
设置你的 Litefuse 和 Anthropic API Key。你可以通过注册免费的 Litefuse Cloud 账户或自托管 Litefuse 来获取 Litefuse 密钥。在 Anthropic Console 获取你的 Anthropic API Key。
// Set environment variables using Deno-specific syntax
Deno.env.set("ANTHROPIC_API_KEY", "sk-ant-...");
// Langfuse authentication keys
Deno.env.set("LANGFUSE_PUBLIC_KEY", "pk-lf-...");
Deno.env.set("LANGFUSE_SECRET_KEY", "sk-lf-...");
// Langfuse host configuration
Deno.env.set("LANGFUSE_BASE_URL", "https://litefuse.cloud");第三步:使用 Litefuse 初始化 OpenTelemetry
使用 LangfuseSpanProcessor 和来自 OpenInference 的 AnthropicInstrumentation 配置 OpenTelemetry SDK。该插桩会自动捕获 Anthropic SDK 调用,并以 OpenTelemetry span 的形式发送到 Litefuse。
import { NodeSDK } from "npm:@opentelemetry/sdk-node";
import { LangfuseSpanProcessor } from "npm:@langfuse/otel";
import { AnthropicInstrumentation } from "npm:@arizeai/openinference-instrumentation-anthropic";
import Anthropic from "npm:@anthropic-ai/sdk";
// Configure the instrumentation for the Anthropic SDK
const instrumentation = new AnthropicInstrumentation();
instrumentation.manuallyInstrument(Anthropic);
// Initialize the OpenTelemetry SDK with Langfuse as the span processor
const sdk = new NodeSDK({
spanProcessors: [new LangfuseSpanProcessor()],
instrumentations: [instrumentation],
});
sdk.start();第四步:使用 Anthropic SDK
现在,你可以像往常一样使用 Anthropic SDK。所有调用都会被自动追踪并发送到 Litefuse。
const anthropic = new Anthropic();
const message = await anthropic.messages.create({
model: "claude-haiku-4-5",
max_tokens: 1000,
messages: [{ role: "user", content: "Hello, Claude!" }],
});
console.log(message.content);
await sdk.shutdown();在 Litefuse 中查看 trace
运行应用后,前往你的 Litefuse Trace Table。你将看到应用执行的详细 trace,包括 LLM 调用、输入、输出和性能指标等信息。

Interoperability with the JS/TS SDK
You can use this integration together with the Litefuse SDKs to add additional attributes or group observations into a single trace.
The Context Manager allows you to wrap your instrumented code using context managers (with with statements), which allows you to add additional attributes to the trace. Any observation created inside the callback will automatically be nested under the active observation, and the observation will be ended when the callback finishes.
import { startActiveObservation, propagateAttributes } from "npm:@langfuse/tracing";
await startActiveObservation("context-manager", async (span) => {
span.update({
input: { query: "What is the capital of France?" },
});
// Propagate userId to all child observations
await propagateAttributes(
{
userId: "user-123",
sessionId: "session-123",
metadata: {
source: "api",
region: "us-east-1",
},
tags: ["api", "user"],
version: "1.0.0",
},
async () => {
// YOUR CODE HERE
const { text } = await generateText({
model: openai("gpt-5"),
prompt: "What is the capital of France?",
experimental_telemetry: { isEnabled: true },
});
}
);
span.update({ output: "Paris" });
});Learn more about using the Context Manager in the Langfuse SDK instrumentation docs.
Troubleshooting
No traces appearing
First, enable debug mode in the JS/TS SDK:
export LANGFUSE_LOG_LEVEL="DEBUG"Then run your application and check the debug logs:
- OTel spans appear in the logs: Your application is instrumented correctly but traces are not reaching Litefuse. To resolve this:
- Call
forceFlush()at the end of your application to ensure all traces are exported. This is especially important in short-lived environments like serverless functions. - 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: