使用 Litefuse 追踪 Anthropic JS/TS
Anthropic 提供了 Claude 等先进的语言模型,以安全、有用和强大的推理能力著称。将 Anthropic 的 JS/TS SDK 与 Litefuse 结合,你可以在开发和生产环境中 trace、监控并分析你的 AI 工作负载。
本 notebook 演示如何使用 OpenInference 的 AnthropicInstrumentation 库自动为 Anthropic SDK 调用注入 instrumentation,并把 OpenTelemetry span 发送到 Litefuse。
Anthropic 是什么?
Anthropic 是一家 AI 安全公司,开发了 Claude 这一系列大语言模型,目标是做到有用、无害、诚实。Claude 模型在复杂推理、分析和创意任务上表现优异。
Litefuse 是什么?
Litefuse 是一个开源的 LLM 可观测性与监控平台。它通过捕获元数据、prompt 细节、token 用量、延迟等信息,帮助你 trace 和监控自己的 AI 应用。
步骤 1:安装依赖
安装所需的包:
npm install @anthropic-ai/sdk @arizeai/openinference-instrumentation-anthropic @langfuse/otel @opentelemetry/sdk-node注意:本 cookbook 使用 Deno.js 执行,所以在导入包和设置环境变量上的语法与 Node.js 不同。在 Node.js 应用中流程类似,但使用标准的
npm包和process.env。
步骤 2:配置环境
设置你的 Litefuse 和 Anthropic API key。你可以注册一个免费的 Litefuse Cloud 账号或者自托管 Litefuse 来获取 Litefuse 的 key。Anthropic 的 API key 可以在 Anthropic Console 获取。
// Set environment variables using Deno-specific syntax
Deno.env.set("ANTHROPIC_API_KEY", "sk-ant-...");
// Litefuse authentication keys
Deno.env.set("LANGFUSE_PUBLIC_KEY", "pk-lf-...");
Deno.env.set("LANGFUSE_SECRET_KEY", "sk-lf-...");
// Litefuse host configuration
Deno.env.set("LANGFUSE_BASE_URL", "https://litefuse.cloud");步骤 3:用 Litefuse 初始化 OpenTelemetry
使用 LangfuseSpanProcessor 以及 OpenInference 的 AnthropicInstrumentation 初始化 OpenTelemetry SDK。这套 instrumentation 会自动捕获 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();步骤 4:使用 Anthropic SDK
接下来像平时那样使用 Anthropic SDK 即可,所有调用都会被自动 trace 并发送到 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: