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 | 抓取属性 |
|---|---|---|
agent | AGENT | graph.node.id, (中文(简体) ), graph.node.parent_id (在交割目的地) |
generation | LLM | llm.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 |
response | LLM | 以上全部加起来 llm.tools.*,系统指令作为输入消息 0 |
function | TOOL | tool.name, (中文(简体) ), input.value, (中文(简体) ), output.value |
handoff | TOOL | 扩展名 handoff to <to_agent>;目的地代理人收到 graph.node.parent_id 连接回源 |
mcp_tools | TOOL | output.value (JSON工具名列表) |
guardrail | GUARDRAIL | tool.name, (中文(简体) ), guardrail.triggered |
custom | CHAIN | output.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见
语义公约 说明这些分项与及时总数的关系,以及
缓存符号示例 凤凰城的往返旅行.