Skip to content

feat(sdk-openai-agents): add the openai agents sdk integration - #205

Merged
aikins01 merged 7 commits into
mainfrom
feat/openai-agents
Nov 21, 2026
Merged

aikins01 merged 7 commits into
mainfrom
feat/openai-agents

Conversation

@aikins01

@aikins01 aikins01 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Adds @telemetry-dev/openai-agents, a trace processor for the OpenAI Agents SDK (@openai/agents 0.22 and later). The SDK has its own tracing model and no OpenTelemetry export, so the processor maps each Agents trace to OTel spans through @telemetry-dev/sdk: invoke_agent and execute_tool spans, turns, chat spans for model calls, handoffs, guardrails, and the sandbox.* spans of sandbox agents. It's registered with addTraceProcessor, which keeps the OpenAI Traces exporter, or setTraceProcessors to replace it.

Only model calls (chat spans) carry token usage. The task, turn, and agent spans repeat the totals of their calls, so sending them too would count each call two or three times. Chat Completions calls keep their usage only in the stored completion, so the processor reads it from there.

Sandbox spans get sandbox.operation, and the spans that create or resume a session also get sandbox.backend. The exec_command and write_stdin tool spans get process.exit.code, read from the header line Process exited with code N in the tool output, since the JS SDK puts no exit code on its spans. A non-zero exit is recorded but doesn't mark the span as an error, because commands like grep exit 1 without failing. The command and working directory of sandbox.exec are input content: they're sent only with captureInput on and go through mask.

The root span is where I'd look most closely. Each Agents trace gets one root, which ends when the trace ends. The SDK never ends the trace of a run that throws, so the root also ends when the run's task or agent span ends with the error, and forceFlush and shutdown end the root of every trace that has no open span, at the end of its last span. Roots stay in a map capped at 10,000 traces, evicting the least recently active one, so spans that end after their root, such as under a slower processor registered first, still nest under it. A root can therefore end before later work in its trace, for example a retry after a caught run error inside withTrace. Those spans still nest under the root, and the ingest takes a trace's start and end from all of its spans.

Before the first release, @telemetry-dev/openai-agents needs to exist on npm with its GitHub Actions trusted publisher set, as RELEASING.md describes. Otherwise the release's npm publish fails.

The lockfile adds the package's dependencies. @openai/agents 0.22 needs ws 8.22 and openai 7.45 or later, so pnpm moves the shared ws peer from 8.21.3 to 8.23.0, and the optional openai peer of @langchain/core in packages/langchain now resolves to 7.60.0. That package never imports openai, and its tests pass. The package's own openai dev dependency is 7.60.0, the version the Agents SDK uses, so the test clients type-check against it.

Tests run: vp test run in packages/openai-agents (25 tests, running real agents against the local sandbox with the OpenAI HTTP calls faked) and in packages/langchain.

A trace processor for @openai/agents that sends each agent run to
telemetry.dev as one trace: agents, turns, model calls with token usage,
tool calls, and the commands a sandbox agent ran, with their exit codes
and the sandbox backend. instrumentSandboxClient records the provider's
sandbox id so the provider's own sandbox events can join the trace.

Only model-call spans carry usage; task, turn, and agent spans repeat
the totals of their calls. The root of a trace ends once the trace and
all of its spans have ended, because other processors can delay span
ends past the trace end, and a run that throws never ends its trace.
…ning

The sandbox example used an undefined agent, and a plain Agent ignores
the sandbox client, so it now builds a SandboxAgent. The double-count
warning now also covers instrumentOpenAI(), which instruments every
OpenAI client in the process.
The intro said each run is one trace. Runs that share a groupId land in
one session trace, a run started inside an active span joins that span's
trace, and sessionMode process groups runs without a groupId.
Match the docs guide: cover propagateAttributes() sessions and set
sessionMode on the processor.
Name propagateAttributes() and process sessions, and the user ID and
metadata that propagateAttributes() sends.
Test the rules the README states through a real Runner: groupId and
propagateAttributes() sessions share one session trace with groupId
winning, a run inside an active span joins that span, and process
sessions group runs without a session. In the SDK, test that a root
inside a span of the host's own context manager joins that span.
… sandbox id

Removes instrumentSandboxClient and sandbox.id. Nothing reads the id,
and it was written into the Agents span data that OpenAI's default
exporter also sends. Session spans keep sandbox.backend.

The root of a trace now ends when the trace ends, when a run's task or
agent span ends with an error, or at forceFlush or shutdown once the
trace has no open span, at the end of its last span. Roots stay in a map
capped at 10,000 traces, evicted least recently active first, so late
spans still nest under them. This replaces the open-span counting and
the one-minute idle timers.

Agent and tool spans are named invoke_agent <name> and execute_tool
<name>, like the other integrations. The integration now requires
@openai/agents 0.22.
@aikins01
aikins01 merged commit a52410a into main Nov 21, 2026
9 of 10 checks passed
@aikins01
aikins01 deleted the feat/openai-agents branch November 21, 2026 12:34
@aikins01 aikins01 mentioned this pull request Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant