@arizeai/openinference-instrumentation-openai-agents

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

OpenAI 代理 SDK 的 OpenInformation 仪器

中文使用文档
文档目录8 节
  1. 安装
  2. 快速启动
  3. 如何运作
  4. 配置
  5. 宽幅覆盖
  6. 实例
  7. 许可证
  8. 快速缓存令牌计数

OpenAI 代理 SDK (Node.js) 的 OpenInference 仪器

以 OpenTelemeth 为基础的仪器 OpenAI 代理服务器 SDK (单位:千美元)@openai/agents) (中文(简体) ). 将 SDK 的本地跟踪事件连接到 OpenTeleometry 之后的 开源语义公约,因此可以在任何 OpenTeleometry 兼容后端中观察到代理运行,例如 阿里兹·凤凰, (中文(简体) ), 调整 AXJaeger 或者你的收藏家选择的.

安装

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

快速启动

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);

如何运作

与大多数 OpenInference 仪器不同,此软件包确实 没有 给SDK打个针 特工SDK曝光头等舱 TracingProcessor 接口; 此软件包通过 SDK 执行并注册 setTraceProcessors 编号 : addTraceProcessor APIs (英语).

模式打电话行为
专属( 默认)instrument() (CommonJS)或 (中文(简体) ) manuallyInstrument(agents) (ESM) (英语)以 Open Information 1 替换所有已有的微量处理器。 当 OpenTeleometry 是您唯一的追踪目的地时使用 .
添加instrument({ exclusiveProcessor: false }) (CommonJS)或 (中文(简体) ) manuallyInstrument(agents, { exclusiveProcessor: false }) (ESM) (英语)将 OpenInference 处理器与任何已存在的处理器(例如 SDK 默认的 OpenAI 跟踪导出器)相并列. 需要 Open 推论时使用 财务报告和审定财务报表 OpenAI本土寻踪.

当模块自动被补丁时(通过) registerInstrumentations 或 NodeSDK),通过构造器选择模式取而代之: new OpenAIAgentsInstrumentation({ exclusiveProcessor: false })。 。 。 .

为了停止追踪,打电话 instrumentation.uninstrument()。在添加剂模式下,SDK剂不会暴露出单处理器去除API,所以 uninstrument() 不清理其他 SDK 处理器而禁用已设置的 OpenInference 处理器.

对于仅限无害环境管理的环境,通过进口的SDK命名空间:

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

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

配置

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,
});

宽幅覆盖

每个代理SDK跨度类型被映射到 OpenInference 跨度类型:

SDK 跨度openinference.span.kind抓取属性
agentAGENTgraph.node.id, (中文(简体) ), graph.node.parent_id (在交割目的地)
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
responseLLM以上全部加起来 llm.tools.*,系统指令作为输入消息 0
functionTOOLtool.name, (中文(简体) ), input.value, (中文(简体) ), output.value
handoffTOOL扩展名 handoff to <to_agent>;目的地代理人收到 graph.node.parent_id 连接回源
mcp_toolsTOOLoutput.value (JSON工具名列表)
guardrailGUARDRAILtool.name, (中文(简体) ), guardrail.triggered
customCHAINoutput.value (JSON序列化用户数据)

两者 聊天 补全 财务报告和审定财务报表 答复 运输得到支持。 在聊天 补全模式中, SDK 将原始响应对象存储在 output[]; 此仪器从 choices[].message 并累积所有回复的符号使用.

实例

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

共享的 OTel 设置在 examples/instrumentation.ts 修改它,以便在OTLP出口商或任何其他处理器中互换.

许可证

阿帕奇 - 2.0

快速缓存令牌计数

OpenAI 缓存读取( O)cached_tokens写作(cache_write_tokens)记录为 llm.token_count.prompt_details.cache_read 财务报告和审定财务报表 cache_write见 语义公约 说明这些分项与及时总数的关系,以及 缓存符号示例 凤凰城的往返旅行.

版本与发布记录

@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 是什么?

OpenAI 代理 SDK 的 OpenInformation 仪器

如何安装 @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 和提交记录判断。