Quick Start
The most basic usage involves creating aCodex client, starting a thread, and running a turn:
Core SDK Exports
The SDK exports the following main classes and types:Main Classes
class
Main client for interacting with the Codex agent. Used to create and resume threads.
class
Represents a conversation with the agent. Supports multiple consecutive turns.
Type Exports
Creating a Codex Client
TheCodex class accepts optional configuration:
string
Custom API base URL. Defaults to the OpenAI API endpoint.
string
OpenAI API key. Can also be set via
OPENAI_API_KEY environment variable.string
Path to the
codex CLI binary. Used when codex is not in your PATH.Record<string, string>
Environment variables passed to the Codex CLI process. When provided, the SDK will not inherit from
process.env.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
string
Working directory for the agent. Defaults to the current directory.
'read-only' | 'workspace-write' | 'danger-full-access'
File system access level for the agent.
string
Model to use for this thread (e.g.,
"gpt-4", "gpt-4-turbo").boolean
Skip the Git repository check. Defaults to
false.'minimal' | 'low' | 'medium' | 'high' | 'xhigh'
Amount of reasoning effort the model should apply.
boolean
Enable network access for the agent.
'disabled' | 'cached' | 'live'
Web search configuration for the agent.
'never' | 'on-request' | 'on-failure' | 'untrusted'
When to request user approval for actions.
string[]
Additional directories to include in the agent’s context.
Running a Turn
Buffered Execution
Userun() to execute a turn and wait for the complete result:
Turn object contains:
string
The agent’s final text response (or JSON if using structured output).
ThreadItem[]
Array of all items produced during the turn (commands, file changes, tool calls, etc.).
Usage | null
Token usage statistics for the turn.
Streaming Events
UserunStreamed() 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 thezod-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 anAbortSignal: