Codex
Python Agents SDK backed by the embedded Codex engine.
Agent
dataclass
Reusable configuration for an agent across turns and sessions.
Source code in src/openai_agents/codex/agent.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
name
class-attribute
instance-attribute
A human-readable name for the agent.
instructions
class-attribute
instance-attribute
Instructions to append to Codex's base instructions.
tools
class-attribute
instance-attribute
Tools to make available alongside the harness's default tools.
reasoning
class-attribute
instance-attribute
reasoning: Reasoning | None = None
Overrides for the model's reasoning settings.
hooks
class-attribute
instance-attribute
hooks: list[Hook] = field(default_factory=list)
Codex command hooks to run on the selected executor.
approval_policy
class-attribute
instance-attribute
When Codex asks for review. Defaults to on-request with a reviewer, never otherwise.
guardian
class-attribute
instance-attribute
Use Codex Guardian for native actions eligible for automatic review.
approval_handler
class-attribute
instance-attribute
Decide application approval requests. Return True to approve, False to reject.
subagents
class-attribute
instance-attribute
Set True to enable Codex collaboration, False to disable it, or use the model default.
session
session(
*,
environment: Environment | EllipsisType = ...,
workspace: str | PathLike[str] | None = None,
sandbox: Sandbox | None = None,
state: ConversationState | None = None,
storage: SessionStorage | None = None,
session_id: str | None = None,
) -> Session
Open a conversation for multiple turns with async with.
Source code in src/openai_agents/codex/agent.py
run
async
run(
input: str | None = None,
*,
text: str | None = None,
environment: Environment | EllipsisType = ...,
workspace: str | PathLike[str] | None = None,
sandbox: Sandbox | None = None,
state: ConversationState | None = None,
storage: SessionStorage | None = None,
session_id: str | None = None,
store: ConversationStore | None = None,
conversation_id: str | None = None,
) -> RunResult
Run one turn in a fresh session from an explicit snapshot or a stored ID.
Source code in src/openai_agents/codex/agent.py
stream
async
stream(
input: str | None = None,
*,
text: str | None = None,
environment: Environment | EllipsisType = ...,
workspace: str | PathLike[str] | None = None,
sandbox: Sandbox | None = None,
state: ConversationState | None = None,
storage: SessionStorage | None = None,
session_id: str | None = None,
store: ConversationStore | None = None,
conversation_id: str | None = None,
) -> AsyncGenerator[RunEvent, None]
Yield live events, then the result after shutdown and any store commit.
Source code in src/openai_agents/codex/agent.py
Reasoning
dataclass
Settings for the model's reasoning effort and summary.
Source code in src/openai_agents/codex/agent.py
Runner
Run a turn with a Codex agent.
Source code in src/openai_agents/codex/agent.py
run
async
staticmethod
run(
agent: Agent,
input: str | None = None,
*,
text: str | None = None,
environment: Environment | EllipsisType = ...,
workspace: str | PathLike[str] | None = None,
sandbox: Sandbox | None = None,
state: ConversationState | None = None,
storage: SessionStorage | None = None,
session_id: str | None = None,
store: ConversationStore | None = None,
conversation_id: str | None = None,
) -> RunResult
Run the agent once, using the same session options as Agent.run.
Source code in src/openai_agents/codex/agent.py
ApprovalRequest
dataclass
One proposed action. details contains the original Codex request payload.
Source code in src/openai_agents/codex/approvals.py
Engine
An embedded Codex engine. Open with async with Engine() and run one turn at a time.
Source code in src/openai_agents/codex/engine.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
run
async
run(
agent: Agent,
input: str | None = None,
*,
text: str | None = None,
environment: Environment | EllipsisType = ...,
workspace: str | PathLike[str] | None = None,
sandbox: Sandbox | None = None,
state: ConversationState | None = None,
store: ConversationStore | None = None,
conversation_id: str | None = None,
) -> RunResult
Run one turn from an explicit snapshot or a conversation in a store.
Source code in src/openai_agents/codex/engine.py
stream
async
stream(
agent: Agent,
input: str | None = None,
*,
text: str | None = None,
environment: Environment | EllipsisType = ...,
workspace: str | PathLike[str] | None = None,
sandbox: Sandbox | None = None,
state: ConversationState | None = None,
store: ConversationStore | None = None,
conversation_id: str | None = None,
) -> AsyncGenerator[RunEvent, None]
Yield live events followed by a RunResult. Closing early shuts down this engine.
Source code in src/openai_agents/codex/engine.py
LocalEnvironment
dataclass
Run commands in a directory on the machine running the harness.
Source code in src/openai_agents/codex/environment.py
EngineError
RunError
Bases: EngineError
A failed run with the items and usage observed before the failure.
Source code in src/openai_agents/codex/errors.py
ToolError
Bases: RunError
A Python handler failed. The original exception is available as cause.
Source code in src/openai_agents/codex/errors.py
Hook
dataclass
A native Codex command hook on the agent's executor. Timeout is in seconds.
Source code in src/openai_agents/codex/hooks.py
ContextCompactionItem
dataclass
Bases: _Item
Codex compacted the conversation history.
Source code in src/openai_agents/codex/items.py
UnknownItem
dataclass
Bases: _Item
An additional Codex item, accessible through raw.
Source code in src/openai_agents/codex/items.py
ConversationState
Bases: dict[str, Any]
Conversation history through a completed or interrupted turn, ready to resume.
Source code in src/openai_agents/codex/result.py
codex_revision
property
Codex commit that produced this snapshot, or None for older snapshots.
to_json
from_json
classmethod
from_json(data: str | bytes) -> ConversationState
Load a snapshot. The engine validates the history when resuming.
Source code in src/openai_agents/codex/result.py
RunResult
dataclass
Output of one completed turn.
Source code in src/openai_agents/codex/result.py
final_output
instance-attribute
The final assistant message, if the turn produced one.
new_items
instance-attribute
Completed items from this turn, in stream order.
state
class-attribute
instance-attribute
state: ConversationState = field(repr=False)
Snapshot through the end of this turn, ready to pass to another run.
Sandbox
dataclass
An executor address. Supply it each time you open or restore a conversation.
Source code in src/openai_agents/codex/sandbox/__init__.py
Session
One live conversation. Open with async with agent.session() as session.
Source code in src/openai_agents/codex/session.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
resume
classmethod
resume(
session_id: str,
*,
storage: SessionStorage,
environment: Environment | EllipsisType = ...,
) -> Session
Resume without an executor. Use agent.session() for current tools and permissions.
Source code in src/openai_agents/codex/session.py
snapshot
snapshot() -> ConversationState | None
Copy the last completed or interrupted state, or the initial state before any turns.
run
async
run(
input: str | None = None, *, text: str | None = None
) -> RunResult
stream
async
Yield one turn's events and final result. Closing the iterator stops that turn.
Source code in src/openai_agents/codex/session.py
interrupt
async
Stop the active turn and wait until its snapshot is ready. Idle calls do nothing.
cancel
async
close
async
Stop active work and shut down the session. Safe to call more than once.
SQLiteSessionStorage
Persist snapshots in a local database. Workers share the same file path.
Source code in src/openai_agents/codex/sqlite.py
ConversationStore
Bases: Protocol
Source code in src/openai_agents/codex/store.py
lock
lock(
conversation_id: str,
) -> AbstractAsyncContextManager[StoredConversation]
Acquire exclusive access to the ID before reading its last committed state.
Acquire even for an ID that has no snapshot yet. Yield a private copy. On normal exit, commit the new state if set, then release the lock. On exceptional exit, discard changes and release the lock. A worker that loses the lock must be unable to commit its snapshot.
Source code in src/openai_agents/codex/store.py
InMemorySessionStorage
Share snapshots by ID for the lifetime of this object.
Source code in src/openai_agents/codex/store.py
StoredConversation
dataclass
TurnStream
Bases: AsyncGenerator[RunEvent, None]
Stream one turn, retaining its final result after delivery.
Source code in src/openai_agents/codex/stream.py
get_final_result
async
get_final_result() -> RunResult
Consume any remaining events and return the completed turn.
Source code in src/openai_agents/codex/stream.py
FunctionTool
dataclass
Declaration of a function the model can ask the application to call.
Source code in src/openai_agents/codex/tool.py
parameters
instance-attribute
JSON Schema describing the function's arguments.
handler
class-attribute
instance-attribute
Application callback that receives the decoded arguments and returns text.
WebSearchTool
dataclass
TokenUsage
dataclass
Provider counts. Cached and reasoning tokens are subsets of input and output.
Source code in src/openai_agents/codex/usage.py
Usage
dataclass
Source code in src/openai_agents/codex/usage.py
responses
instance-attribute
One entry per completed model response in this turn, in arrival order.
thread
class-attribute
instance-attribute
thread: TokenUsage | None = None
All reported tokens in the conversation through the completed turn.
codex_version
local_sandbox
async
local_sandbox(
cwd: str | PathLike[str] = ".", *, id: str | None = None
) -> AsyncIterator[Sandbox]
Run a private exec-server on a kernel-assigned loopback port for this block.
Source code in src/openai_agents/codex/sandbox/__init__.py
function_tool
function_tool(
func: _Function,
*,
name_override: str | None = None,
description_override: str | None = None,
needs_approval: bool = False,
) -> FunctionTool
function_tool(
*,
name_override: str | None = None,
description_override: str | None = None,
needs_approval: bool = False,
) -> Callable[[_Function], FunctionTool]
function_tool(
func: _Function | None = None,
*,
name_override: str | None = None,
description_override: str | None = None,
needs_approval: bool = False,
) -> FunctionTool | Callable[[_Function], FunctionTool]
Turn a sync or async function into a tool using its signature and docstring.