@arizeai/openinference-instrumentation-anthropic

中文文档
1.0.1发布于 2026/11/06Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 18万

OpenInference instrumentation for Anthropic

原始使用文档
文档目录8 节
  1. Spans of the Anthropic SDK
  2. Installation
  3. Quickstart
  4. Configuration
  5. Supported Features
  6. Semantic Conventions
  7. Examples
  8. License

OpenInference Anthropic Instrumentation

JavaScript auto-instrumentation library for the Anthropic SDK

This package implements OpenInference tracing for the following Anthropic SDK methods:

  • messages.create (including streaming)
  • beta.messages.create (including server-side fallback and streaming)
  • messages.countTokens and beta.messages.countTokens
  • beta.messages.toolRunner (a CHAIN span parenting the LLM span of each request and a TOOL span for each tool run)

messages.stream() and beta.messages.stream() are traced through create. Clients from @anthropic-ai/bedrock-sdk, @anthropic-ai/vertex-sdk and @anthropic-ai/foundry-sdk are traced too, with llm.provider set to aws, google and azure.

These traces are fully OpenTelemetry compatible and can be sent to an OpenTelemetry collector for viewing, such as Arize Phoenix or Arize AX.

Spans of the Anthropic SDK

@anthropic-ai/sdk 0.134.0 and later create an OpenTelemetry span of their own for each API call (anthropic.messages.create and so on), unless it is turned off with openTelemetry: false or ANTHROPIC_OPEN_TELEMETRY=false. For a call that this instrumentation traces, the SDK's span is replaced by the OpenInference LLM span: the SDK records no span, its request carries the LLM span's trace context, and HTTP spans are children of the LLM span. Calls that this instrumentation does not trace, such as models.list(), keep the SDK's span. A beta.messages.toolRunner() run also keeps the SDK's tool runner and tool spans, which are not model calls.

Installation

npm install @arizeai/openinference-instrumentation-anthropic

Quickstart

Install required packages:

npm install @arizeai/openinference-instrumentation-anthropic @arizeai/openinference-semantic-conventions @anthropic-ai/sdk @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-proto @opentelemetry/sdk-trace-node @opentelemetry/semantic-conventions

Set up instrumentation in your application:

import { NodeSDK, resources } from "@opentelemetry/sdk-node";
import { AnthropicInstrumentation } from "@arizeai/openinference-instrumentation-anthropic";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { SEMRESATTRS_PROJECT_NAME } from "@arizeai/openinference-semantic-conventions";

import Anthropic from "@anthropic-ai/sdk";

// Configure the SDK with Anthropic instrumentation
const instrumentation = new AnthropicInstrumentation({
  // Optional: configure trace settings
  traceConfig: {
    // hideInputs: true,
    // hideOutputs: true,
  },
});
// necessary when instrumenting in an ESM environment
instrumentation.manuallyInstrument(Anthropic);
const sdk = new NodeSDK({
  spanProcessors: [
    new SimpleSpanProcessor(
      new OTLPTraceExporter({
        url: "http://localhost:6006/v1/traces",
      }),
    ),
  ],
  resource: resources.resourceFromAttributes({
    [ATTR_SERVICE_NAME]: "anthropic-service",
    [SEMRESATTRS_PROJECT_NAME]: "anthropic-service",
  }),
  instrumentations: [instrumentation],
});

// Initialize the SDK
sdk.start();

// Now use Anthropic as normal

const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

const message = await anthropic.messages.create({
  model: "claude-3-5-haiku-latest",
  max_tokens: 1000,
  messages: [{ role: "user", content: "Hello, Claude!" }],
});

console.log(message.content);

sdk.shutdown();

Configuration

The AnthropicInstrumentation constructor accepts the following options:

  • instrumentationConfig: Standard OpenTelemetry instrumentation configuration
  • traceConfig: OpenInference-specific trace configuration for masking/redacting sensitive information
  • tracerProvider: Optional custom tracer provider

Supported Features

  • Messages API: Full support for anthropic.messages.create()
  • Beta Messages API: Full support for anthropic.beta.messages.create()
  • Server-Side Fallback: Captures requested and serving models across fallback boundaries, records the fallback handoff (declining model and refusal category) as message content, and takes token counts from the attempt that served the response
  • Refusals: Records llm.finish_reason, so a classifier decline (stop_reason: "refusal") is visible on the span
  • Streaming: Automatic handling of streaming responses
  • Tool Use: Captures tool/function calling information
  • Token Usage: Records input/output token counts when available
  • Error Handling: Proper error recording and span status management

Semantic Conventions

This instrumentation follows the OpenInference semantic conventions for LLM observability, capturing:

  • Model name and provider information
  • Input/output messages and content
  • Token usage statistics
  • Tool calling details
  • Invocation parameters

Examples

To run an example, run the following commands:

cd js/packages/openinference-instrumentation-anthropic
pnpm install
pnpm -r build
pnpx tsx examples/basic-usage.ts # or streaming.ts, tool-use.ts, etc

See the examples directory for more detailed usage examples.

To exercise server-side fallback:

export ANTHROPIC_API_KEY=your-api-key

# Real Anthropic beta request that triggers a reasoning-extraction fallback
pnpm exec tsx examples/server-side-fallback.ts

# Deterministic SSE fixture that guarantees a mid-stream fallback boundary
pnpm exec tsx examples/server-side-fallback-streaming.ts

License

Apache-2.0

版本与发布记录

@arizeai/openinference-instrumentation-anthropic 共发布 34 个版本,首次发布于 2025/10/08;过去 12 个月发布了 30 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.1latest2026/11/062 天前
1.0.02026/10/2414 天前
0.2.112026/10/021 个月前
0.2.102026/10/021 个月前
0.2.92026/10/011 个月前
0.2.82026/09/191 个月前

关于 @arizeai/openinference-instrumentation-anthropic 的常见问题

@arizeai/openinference-instrumentation-anthropic 是什么?

OpenInference instrumentation for Anthropic

如何安装 @arizeai/openinference-instrumentation-anthropic?

在项目目录运行 npm install @arizeai/openinference-instrumentation-anthropic,或使用 pnpm add / yarn add / bun add。

@arizeai/openinference-instrumentation-anthropic 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@arizeai/openinference-instrumentation-anthropic 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@arizeai/openinference-instrumentation-anthropic 可以商用吗?

@arizeai/openinference-instrumentation-anthropic 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@arizeai/openinference-instrumentation-anthropic 的下载量有多少?

根据 npm 官方下载统计,@arizeai/openinference-instrumentation-anthropic 在 2026/10/07 – 2026/11/05 这 30 天内被下载 179,983 次,日均约 5,999 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@arizeai/openinference-instrumentation-anthropic 还在维护吗?

最新版本 1.0.1 发布于 2026/11/06(2 天前);至今共发布 34 个版本,其中过去 12 个月发布了 30 个;首次发布于 2025/10/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。