Skip to main content

Quick Start

The most basic usage involves creating a Codex client, starting a thread, and running a turn:

Core SDK Exports

The SDK exports the following main classes and types:

Main Classes

Codex
class
Main client for interacting with the Codex agent. Used to create and resume threads.
Thread
class
Represents a conversation with the agent. Supports multiple consecutive turns.

Type Exports

Creating a Codex Client

The Codex class accepts optional configuration:
baseUrl
string
Custom API base URL. Defaults to the OpenAI API endpoint.
apiKey
string
OpenAI API key. Can also be set via OPENAI_API_KEY environment variable.
codexPathOverride
string
Path to the codex CLI binary. Used when codex is not in your PATH.
env
Record<string, string>
Environment variables passed to the Codex CLI process. When provided, the SDK will not inherit from process.env.
config
CodexConfigObject
Additional --config overrides passed to the CLI. The SDK flattens this object into dotted paths.

Starting a Thread

Create a new conversation thread with optional configuration:

Thread Options

workingDirectory
string
Working directory for the agent. Defaults to the current directory.
sandboxMode
'read-only' | 'workspace-write' | 'danger-full-access'
File system access level for the agent.
model
string
Model to use for this thread (e.g., "gpt-4", "gpt-4-turbo").
skipGitRepoCheck
boolean
Skip the Git repository check. Defaults to false.
modelReasoningEffort
'minimal' | 'low' | 'medium' | 'high' | 'xhigh'
Amount of reasoning effort the model should apply.
networkAccessEnabled
boolean
Enable network access for the agent.
webSearchMode
'disabled' | 'cached' | 'live'
Web search configuration for the agent.
approvalPolicy
'never' | 'on-request' | 'on-failure' | 'untrusted'
When to request user approval for actions.
additionalDirectories
string[]
Additional directories to include in the agent’s context.

Running a Turn

Buffered Execution

Use run() to execute a turn and wait for the complete result:
The returned Turn object contains:
finalResponse
string
The agent’s final text response (or JSON if using structured output).
items
ThreadItem[]
Array of all items produced during the turn (commands, file changes, tool calls, etc.).
usage
Usage | null
Token usage statistics for the turn.

Streaming Events

Use runStreamed() to receive real-time events as the agent works:

Structured Output

Provide a JSON schema to get structured responses:

Using Zod Schemas

You can also use Zod schemas with the zod-to-json-schema package:

Attaching Images

Provide images alongside text prompts:
Image entries must use the local_image type with an absolute or relative path to the image file.

Resuming Threads

Threads are persisted in ~/.codex/sessions. Resume a previous conversation:

Working with Thread Items

Each turn produces various item types representing the agent’s work:

Token Usage

Access detailed token usage after each turn:

Aborting a Turn

Cancel a turn using an AbortSignal:

Error Handling

Handle errors gracefully:

Advanced Example

Here’s a complete example combining multiple features: