@arizeai/openinference-instrumentation-openai-agents

中文文档
0.3.3发布于 2026/10/24Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 7190

OpenInference instrumentation for OpenAI Agents SDK

原始使用文档
文档目录8 节
  1. Installation
  2. Quickstart
  3. How it works
  4. Configuration
  5. Span coverage
  6. Examples
  7. License
  8. Prompt cache token counts

OpenInference Instrumentation for OpenAI Agents SDK (Node.js)

OpenTelemetry-based instrumentation for the OpenAI Agents SDK (@openai/agents). Bridges the SDK's native tracing events to OpenTelemetry spans following the OpenInference semantic conventions, so agent runs can be observed in any OpenTelemetry-compatible backend such as Arize Phoenix, Arize AX, Jaeger, or your collector of choice.

Installation

npm install @arizeai/openinference-instrumentation-openai-agents @arizeai/openinference-semantic-conventions @openai/agents

Quickstart

import * as agents from "@openai/agents";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { Resource } from "@opentelemetry/resources";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";

import { OpenAIAgentsInstrumentation } from "@arizeai/openinference-instrumentation-openai-agents";

// 1. Configure OpenTelemetry.
const provider = new NodeTracerProvider({
  resource: new Resource({
    [ATTR_SERVICE_NAME]: "my-agent-app",
  }),
  spanProcessors: [
    new BatchSpanProcessor(new OTLPTraceExporter({ url: "http://localhost:6006/v1/traces" })),
  ],
});
provider.register();

// 2. Register the OpenInference processor with the agents SDK.
const instrumentation = new OpenAIAgentsInstrumentation({ tracerProvider: provider });
instrumentation.manuallyInstrument(agents);

// 3. Use the agents SDK as usual.
const agent = new agents.Agent({
  name: "Assistant",
  instructions: "You are a helpful assistant.",
});

const result = await agents.run(agent, "What is the capital of France?");
console.log(result.finalOutput);

How it works

Unlike most OpenInference instrumentations, this package does not monkey-patch the SDK. The agents SDK exposes a first-class TracingProcessor interface; this package implements it and registers via the SDK's setTraceProcessors / addTraceProcessor APIs.

ModeCallBehaviour
Exclusive (default)instrument() (CommonJS) or manuallyInstrument(agents) (ESM)Replaces every existing trace processor with the OpenInference one. Use when OpenTelemetry is your sole tracing destination.
Additiveinstrument({ exclusiveProcessor: false }) (CommonJS) or manuallyInstrument(agents, { exclusiveProcessor: false }) (ESM)Adds the OpenInference processor alongside any existing processors (e.g. the SDK's default OpenAI tracing exporter). Use when you want OpenInference and OpenAI native tracing.

When the module is patched automatically (via registerInstrumentations or NodeSDK), choose the mode through the constructor instead: new OpenAIAgentsInstrumentation({ exclusiveProcessor: false }).

To stop tracing, call instrumentation.uninstrument(). In additive mode, the agents SDK does not expose a single-processor removal API, so uninstrument() disables the OpenInference processor in place without clearing other SDK processors.

For ESM-only environments, pass the imported SDK namespace explicitly:

import * as agents from "@openai/agents";

const instrumentation = new OpenAIAgentsInstrumentation({ tracerProvider: provider });
instrumentation.manuallyInstrument(agents, { exclusiveProcessor: false });

Configuration

new OpenAIAgentsInstrumentation({
  // Optional: an OTel TracerProvider. Defaults to the global provider.
  tracerProvider,

  // Optional: processor registration mode (see "How it works" above).
  // This is the only way to choose additive mode when the module is patched
  // automatically (registerInstrumentations / NodeSDK). Defaults to true.
  exclusiveProcessor: true,

  // Optional: OpenInference trace configuration for masking/redacting
  // sensitive data on emitted spans.
  // See https://github.com/Arize-ai/openinference/blob/main/js/packages/openinference-core
  traceConfig: {
    hideInputs: true,
    hideOutputs: true,
  },

  // Optional: maximum number of root trace spans kept in memory while traces
  // are in flight. If the limit is exceeded, the oldest root span is ended
  // and evicted. Defaults to 1000.
  maxRootSpansInFlight: 1000,
});

Span coverage

Each agents SDK span type is mapped to an OpenInference span kind:

SDK spanopeninference.span.kindCaptured attributes
agentAGENTgraph.node.id, graph.node.parent_id (on handoff destination)
generationLLMllm.model_name, llm.invocation_parameters, llm.input_messages.*, llm.output_messages.*, llm.token_count.{prompt,completion,total}, llm.token_count.prompt_details.cache_read, llm.token_count.completion_details.reasoning
responseLLMAll of the above plus llm.tools.*, system instructions as input message 0
functionTOOLtool.name, input.value, output.value
handoffTOOLSpan name handoff to <to_agent>; the destination agent receives graph.node.parent_id linking back to the source
mcp_toolsTOOLoutput.value (JSON list of tool names)
guardrailGUARDRAILtool.name, guardrail.triggered
customCHAINoutput.value (JSON-serialised user data)

Both the chat_completions and responses transports are supported. In chat_completions mode the SDK stores raw response objects in output[]; this instrumentation extracts messages from choices[].message and accumulates token usage across all responses.

Examples

cd js/packages/openinference-instrumentation-openai-agents
pnpm install
pnpm -r build

OPENAI_API_KEY=sk-... npx tsx examples/chat.ts     # single agent + tool call
OPENAI_API_KEY=sk-... npx tsx examples/handoff.ts  # multi-agent handoff
OPENAI_API_KEY=sk-... npx tsx examples/streaming.ts # streamed agent run
OPENAI_API_KEY=sk-... npx tsx examples/guardrail.ts # input guardrail

The shared OTel setup lives in examples/instrumentation.ts — modify it to swap in an OTLP exporter or any other span processor.

License

Apache-2.0

Prompt cache token counts

OpenAI cache reads (cached_tokens) and writes (cache_write_tokens) are recorded as llm.token_count.prompt_details.cache_read and cache_write. See the semantic conventions for how these sub-counts relate to the prompt total, and the cache token example for a Phoenix round trip.

版本与发布记录

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

版本发布日期距今
0.3.3latest2026/10/2414 天前
0.3.22026/10/021 个月前
0.3.12026/10/021 个月前
0.3.02026/10/011 个月前
0.2.152026/09/191 个月前
0.2.142026/09/151 个月前

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

@arizeai/openinference-instrumentation-openai-agents 是什么?

OpenInference instrumentation for OpenAI Agents SDK

如何安装 @arizeai/openinference-instrumentation-openai-agents?

在项目目录运行 npm install @arizeai/openinference-instrumentation-openai-agents,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@openai/agents(>=0.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@arizeai/openinference-instrumentation-openai-agents 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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