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
Main client for interacting with the Codex agent. Used to create and resume threads.
Represents a conversation with the agent. Supports multiple consecutive turns.
Type Exports
Creating a Codex Client
TheCodex class accepts optional configuration:
Custom API base URL. Defaults to the OpenAI API endpoint.
OpenAI API key. Can also be set via
OPENAI_API_KEY environment variable.Path to the
codex CLI binary. Used when codex is not in your PATH.Environment variables passed to the Codex CLI process. When provided, the SDK will not inherit from
process.env.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
Working directory for the agent. Defaults to the current directory.
File system access level for the agent.
Model to use for this thread (e.g.,
"gpt-4", "gpt-4-turbo").Skip the Git repository check. Defaults to
false.Amount of reasoning effort the model should apply.
Enable network access for the agent.
Web search configuration for the agent.
When to request user approval for actions.
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:
The agent’s final text response (or JSON if using structured output).
Array of all items produced during the turn (commands, file changes, tool calls, etc.).
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: