Skip to Content
Launching the open-source gateway

Codex SDK (tracing)

The OpenAI Codex SDK  (@openai/codex-sdk) lets TypeScript applications start Codex threads and run coding-agent turns programmatically. Use @respan/instrumentation-codex-sdk to send Codex turn activity through the Respan tracing pipeline.

Set up Respan

Create an account at platform.respan.ai  and grab an API key .

Paste this into your coding agent:

Help me set up Respan tracing with the Codex SDK integration. Follow https://www.respan.ai/docs/integrations/codex-sdk.md

Setup

Install packages

Terminal
npm install @openai/codex-sdk @respan/respan @respan/tracing @respan/instrumentation-codex-sdk

Set environment variables

Terminal
export CODEX_API_KEY="YOUR_CODEX_OR_OPENAI_API_KEY" export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

Initialize and run

TypeScript
import * as CodexSDKModule from "@openai/codex-sdk"; import { Codex } from "@openai/codex-sdk"; import { Respan } from "@respan/respan"; import { CodexSDKInstrumentor } from "@respan/instrumentation-codex-sdk"; const respan = new Respan({ apiKey: process.env.RESPAN_API_KEY, baseURL: process.env.RESPAN_BASE_URL, appName: "codex-sdk-tracing", instrumentations: [ new CodexSDKInstrumentor({ sdkModule: CodexSDKModule, workflowName: "codex-sdk-tracing", agentName: "codex-agent", }), ], }); await respan.initialize(); const codex = new Codex({ apiKey: process.env.CODEX_API_KEY ?? process.env.OPENAI_API_KEY, }); const thread = codex.startThread({ workingDirectory: process.cwd(), skipGitRepoCheck: true, sandboxMode: "read-only", approvalPolicy: "never", }); const result = await respan.withWorkflow({ name: "codex-sdk-basic-turn" }, async () => await thread.run("Reply with one short sentence that explains tracing.") ); console.log(result.finalResponse); await respan.shutdown();

Pass sdkModule when your app imports @openai/codex-sdk directly. This ensures the instrumentor patches the same SDK module instance your application uses.

View your trace

Open the Traces page  to see Codex SDK turns with agent messages, reasoning, command execution, MCP tool calls, file changes, and errors.

Captured data

DataDescription
Buffered turnsThread.run() calls and final responses.
Streamed turnsThread.runStreamed() events and completed agent messages.
ReasoningReasoning summaries emitted by Codex.
Command executionShell command events and results.
MCP tool callsMCP tool invocation events from Codex turns.
Web searchWeb search events when enabled in Codex options.
File changesFile change events produced by Codex.
Structured outputOutput schema usage and final structured responses.
FailuresFailed turns and stream errors with backend-visible error fields.

Attributes

Use Respan attributes to group Codex runs by user, thread, or workflow.

TypeScript
await respan.propagateAttributes( { customer_identifier: "user_123", thread_identifier: "codex-thread-abc", metadata: { feature: "code-review", environment: "production", }, }, async () => { await respan.withWorkflow({ name: "codex-review" }, async () => { await thread.run("Review this change for correctness."); }); } );
AttributeTypeDescription
customer_identifierstringIdentifies the end user in Respan analytics.
thread_identifierstringGroups related turns into a conversation.
trace_group_identifierstringGroups related spans into a named trace group.
metadataobjectCustom key-value pairs attached to spans.

Gateway

See Codex SDK gateway setup  to route Codex SDK model calls through the Respan gateway.