# @arizeai/openinference-instrumentation-anthropic 中文文档

规范地址：https://npmcn.com/package/%40arizeai/openinference-instrumentation-anthropic/

版本：1\.0\.1
npm 元数据更新时间：2026-11-05T17:17:05.741Z
中文译文更新时间：2026-11-06T19:21:33.156Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40arizeai/openinference-instrumentation-anthropic
原文对照：https://npmcn.com/package/%40arizeai/openinference-instrumentation-anthropic/?lang=original
项目主页：https://github.com/Arize-ai/openinference#readme
源码仓库：https://github.com/Arize-ai/openinference

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：829 kB，32 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：5 个
- 首次发布：2025-10-08T00:22:36.059Z
- 最新版本发布：2026-11-05T17:17:05.090Z
- 近 30 天下载量：179983（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：34（近 12 个月 30 个）

## 中文简介

Anthropic 的开源仪器

## 安装

```sh
npm install @arizeai/openinference-instrumentation-anthropic
```

也可以使用：`pnpm add @arizeai/openinference-instrumentation-anthropic`、`yarn add @arizeai/openinference-instrumentation-anthropic`、`bun add @arizeai/openinference-instrumentation-anthropic`

## README

# OpenInform 人类仪器

JavaScript 自动仪器库 [安东尼](https://www.anthropic.com/) SDK 软件

本套件针对以下 Anthropic SDK 方法执行 OpenInform track:

-   `messages.create` (包括流出)
-   `beta.messages.create` (包括服务器侧回落和流出)
-   `messages.countTokens` 财务报告和审定财务报表 `beta.messages.countTokens`
-   `beta.messages.toolRunner` (一个CHAIN涵盖每个请求的LLM间隔和每个工具运行的TOOL间隔)

`messages.stream()` 财务报告和审定财务报表 `beta.messages.stream()` 追踪到 `create`客户 `@anthropic-ai/bedrock-sdk`, (中文(简体) ), `@anthropic-ai/vertex-sdk` 财务报告和审定财务报表 `@anthropic-ai/foundry-sdk` 也追踪,与 `llm.provider` 设置为 `aws`, (中文(简体) ), `google` 财务报告和审定财务报表 `azure`。 。 。 .

这些痕迹是完全相容的 OpenTeleometry 并可以发送到 OpenTeleometry 收藏器取景,例如 [阿里兹·凤凰](https://github.com/Arize-ai/phoenix) 或者说 [调整 AX](https://arize.com/products/ax?utm_source=docs&utm_medium=web&utm_content=openinference)。 。 。 .

### 人类SDK的西班牙人

`@anthropic-ai/sdk` 0.134.0,并随后为每次API呼叫创建自己的OpenTeleometry跨度(`anthropic.messages.create` 除非它被关闭 `openTelemetry: false` 或者说 `ANTHROPIC_OPEN_TELEMETRY=false`。对于这种仪器跟踪的调用,SDK的跨度被 OpenInference LLM 跨度所取代:SDK 记录没有跨度,其请求带有LLM 跨度的微量上下文,而HTTP 跨度是LLM 跨度的儿童. 要求这种仪器不能追踪,例如: `models.list()`保持SDK的长度 A 类 `beta.messages.toolRunner()` 运行还保持了SDK的工具跑道和工具跨度,这些不是模型调用.

## 安装

```shell
npm install @arizeai/openinference-instrumentation-anthropic
```

## 快速启动

安装所需的软件包 :

```shell
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
```

在应用程序中设置仪器 :

```typescript
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();
```

## 配置

这个 `AnthropicInstrumentation` 构造器接受以下选项:

-   `instrumentationConfig`: 标准开远仪表配置
-   `traceConfig`: 遮盖/编辑敏感信息的 OpenInform 特定微量配置
-   `tracerProvider`: 可选的自定义跟踪提供者

## 支持的特性

-   **消息 API**: 全力支持 `anthropic.messages.create()`
-   **Beta 信件 API**: 全力支持 `anthropic.beta.messages.create()`
-   **服务器后退**: 请求的捕获和服务模型跨越后退边界,记录 `fallback` 作为信件内容的交割(降级模式和拒绝类别),并接受服务于答复的尝试的象征性计数
-   **拒绝**: 记录 `llm.finish_reason`,因此分类器下降(`stop_reason: "refusal"`)在跨度上可见
-   **流线**: 自动处理流式响应
-   **工具的使用**: 获取工具/函数调用信息
-   **调用**: 可用时记录输入/输出符号计数
-   **处理错误**: 正确错误记录和跨度状态管理

## 语义公约

该仪器遵循 [开源语义公约](https://github.com/Arize-ai/openinference/blob/main/spec/semantic_conventions.md) 用于LLM可观察性,捕获:

-   模型名称和提供者信息
-   输入/输出信息和内容
-   Token 使用统计
-   调用细节的工具
-   使用参数

## 实例

要运行示例, 请运行以下命令:

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

见 [实例](https://npmcn.com/package/%40arizeai/openinference-instrumentation-anthropic/examples) 更详细的使用示例目录.

要练习服务器侧倒置 :

```shell
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
```

## 许可证

阿帕奇 - 2.0

## 常见问题

### @arizeai/openinference-instrumentation-anthropic 是什么？

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 和提交记录判断。

