You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/agents.md
+42-1Lines changed: 42 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -211,7 +211,48 @@ agent = Agent[UserContext](
211
211
212
212
## Lifecycle events (hooks)
213
213
214
-
Sometimes, you want to observe the lifecycle of an agent. For example, you may want to log events, or pre-fetch data when certain events occur. You can hook into the agent lifecycle with the `hooks` property. Subclass the [`AgentHooks`][agents.lifecycle.AgentHooks] class, and override the methods you're interested in.
214
+
Sometimes, you want to observe the lifecycle of an agent. For example, you may want to log events, pre-fetch data, or record usage when certain events occur.
215
+
216
+
There are two hook scopes:
217
+
218
+
-[`RunHooks`][agents.lifecycle.RunHooks] observe the entire `Runner.run(...)` invocation, including handoffs to other agents.
219
+
-[`AgentHooks`][agents.lifecycle.AgentHooks] are attached to a specific agent instance via `agent.hooks`.
220
+
221
+
The callback context also changes depending on the event:
222
+
223
+
- Agent start/end hooks receive [`AgentHookContext`][agents.run_context.AgentHookContext], which wraps your original context and carries the shared run usage state.
224
+
- LLM, tool, and handoff hooks receive [`RunContextWrapper`][agents.run_context.RunContextWrapper].
225
+
226
+
Typical hook timing:
227
+
228
+
-`on_agent_start` / `on_agent_end`: when a specific agent begins or finishes producing a final output.
229
+
-`on_llm_start` / `on_llm_end`: immediately around each model call.
230
+
-`on_tool_start` / `on_tool_end`: around each local tool invocation.
231
+
-`on_handoff`: when control moves from one agent to another.
232
+
233
+
Use `RunHooks` when you want a single observer for the whole workflow, and `AgentHooks` when one agent needs custom side effects.
Copy file name to clipboardExpand all lines: docs/tools.md
+24-13Lines changed: 24 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -673,10 +673,9 @@ Disabled tools are completely hidden from the LLM at runtime, making this useful
673
673
674
674
## Experimental: Codex tool
675
675
676
-
The `codex_tool` wraps the Codex CLI so an agent can run workspace-scoped tasks (shell, file edits, MCP tools)
677
-
during a tool call. This surface is experimental and may change.
678
-
By default, the tool name is `codex`. If you set a custom name, it must be `codex` or start with `codex_`.
679
-
When an agent includes multiple Codex tools, each must use a unique name (including vs non-Codex tools).
676
+
The `codex_tool` wraps the Codex CLI so an agent can run workspace-scoped tasks (shell, file edits, MCP tools) during a tool call. This surface is experimental and may change.
677
+
678
+
Use it when you want the main agent to delegate a bounded workspace task to Codex without leaving the current run. By default, the tool name is `codex`. If you set a custom name, it must be `codex` or start with `codex_`. When an agent includes multiple Codex tools, each must use a unique name.
680
679
681
680
```python
682
681
from agents import Agent
@@ -705,21 +704,33 @@ agent = Agent(
705
704
)
706
705
```
707
706
708
-
What to know:
707
+
Start with these option groups:
708
+
709
+
- Execution surface: `sandbox_mode` and `working_directory` define where Codex can operate. Pair them together, and set `skip_git_repo_check=True` when the working directory is not inside a Git repository.
710
+
- Thread defaults: `default_thread_options=ThreadOptions(...)` configures the model, reasoning effort, approval policy, additional directories, network access, and web search mode. Prefer `web_search_mode` over the legacy `web_search_enabled`.
711
+
- Turn defaults: `default_turn_options=TurnOptions(...)` configures per-turn behavior such as `idle_timeout_seconds` and the optional cancellation `signal`.
712
+
- Tool I/O: tool calls must include at least one `inputs` item with `{ "type": "text", "text": ... }` or `{ "type": "local_image", "path": ... }`. `output_schema` lets you require structured Codex responses.
713
+
714
+
Thread reuse and persistence are separate controls:
715
+
716
+
-`persist_session=True` reuses one Codex thread for repeated calls to the same tool instance.
717
+
-`use_run_context_thread_id=True` stores and reuses the thread ID in run context across runs that share the same mutable context object.
718
+
- Thread ID precedence is: per-call `thread_id`, then run-context thread ID (if enabled), then the configured `thread_id` option.
719
+
- The default run-context key is `codex_thread_id` for `name="codex"` and `codex_thread_id_<suffix>` for `name="codex_<suffix>"`. Override it with `run_context_thread_id_key`.
720
+
721
+
Runtime configuration:
709
722
710
723
- Auth: set `CODEX_API_KEY` (preferred) or `OPENAI_API_KEY`, or pass `codex_options={"api_key": "..."}`.
711
724
- Runtime: `codex_options.base_url` overrides the CLI base URL.
712
725
- Binary resolution: set `codex_options.codex_path_override` (or `CODEX_PATH`) to pin the CLI path. Otherwise the SDK resolves `codex` from `PATH`, then falls back to the bundled vendor binary.
713
726
- Environment: `codex_options.env` fully controls the subprocess environment. When it is provided, the subprocess does not inherit `os.environ`.
714
727
- Stream limits: `codex_options.codex_subprocess_stream_limit_bytes` (or `OPENAI_AGENTS_CODEX_SUBPROCESS_STREAM_LIMIT_BYTES`) controls stdout/stderr reader limits. Valid range is `65536` to `67108864`; default is `8388608`.
715
-
- Inputs: tool calls must include at least one item in `inputs` with `{ "type": "text", "text": ... }` or `{ "type": "local_image", "path": ... }`.
716
-
- Thread defaults: configure `default_thread_options` for `model_reasoning_effort`, `web_search_mode` (preferred over legacy `web_search_enabled`), `approval_policy`, and `additional_directories`.
717
-
- Turn defaults: configure `default_turn_options` for `idle_timeout_seconds` and cancellation `signal`.
718
-
- Safety: pair `sandbox_mode` with `working_directory`; set `skip_git_repo_check=True` outside Git repos.
719
-
- Run-context thread persistence: `use_run_context_thread_id=True` stores and reuses `thread_id` in run context, across runs that share that context. This requires a mutable run context (for example, `dict` or a writable object field).
720
-
- Run-context key defaults: the stored key defaults to `codex_thread_id` for `name="codex"`, or `codex_thread_id_<suffix>` for `name="codex_<suffix>"`. Set `run_context_thread_id_key` to override.
721
-
- Thread ID precedence: per-call `thread_id` input takes priority, then run-context `thread_id` (if enabled), then the configured `thread_id` option.
722
728
- Streaming: `on_stream` receives thread/turn lifecycle events and item events (`reasoning`, `command_execution`, `mcp_tool_call`, `file_change`, `web_search`, `todo_list`, and `error` item updates).
723
729
- Outputs: results include `response`, `usage`, and `thread_id`; usage is added to `RunContextWrapper.usage`.
724
-
- Structure: `output_schema` enforces structured Codex responses when you need typed outputs.
730
+
731
+
Reference:
732
+
733
+
-[Codex tool API reference](ref/extensions/experimental/codex/codex_tool.md)
0 commit comments