Skip to main content
The OpenAI Codex SDK (@openai/codex-sdk) is a TypeScript SDK for running Codex coding agents programmatically. Braintrust traces Codex threads, agent turns, and tool calls.
This page documents the @openai/codex-sdk SDK. To use the Codex CLI with Braintrust’s MCP server instead, see Codex.

Setup

Install Braintrust alongside the Codex SDK, then set your API keys. Requires @openai/codex-sdk v0.128.0 or later.
1

Install packages

2

Set environment variables

.env

Auto-instrumentation

To trace Codex runs without modifying your application code, initialize Braintrust normally, then run your app with Braintrust’s import hook to patch the Codex SDK at runtime.
1

Initialize Braintrust and run a thread

2

Run with the import hook

The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To trace the Codex SDK manually, wrap the imported module yourself with wrapOpenAICodexSDK().

What Braintrust traces

Braintrust patches the Codex SDK’s Thread.run and Thread.runStreamed and captures:
  • Codex thread spans (OpenAI Codex), with the prompt as input and the agent’s final response as output.
  • LLM turn spans (OpenAI Codex LLM), with reasoning and message output assembled from the stream events.
  • Tool call spans (tool: command_execution, tool: web_search, tool: file_change, and tool: <name> for MCP tools), with the tool input and result.
  • Thread metadata, including the model, sandbox mode, working directory, reasoning effort, web search mode, approval policy, and other openai_codex.* fields.
  • Token usage metrics (prompt, cached prompt, completion, reasoning, and total).
  • Errors captured on every span.

Resources