Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/v1/runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ When the model requests the lookup, the turn goes through four steps.
3. **Python runs the tool.** The SDK dispatches the call to `delivery_date` on your worker. Codex adds the returned value, `Friday`, to the conversation and sends the next request. A turn can include several tool calls.
4. **Python receives the result.** When the model finishes, the [result](ref/codex/result.md) includes the reply, the items produced during the turn, usage, and a checkpoint. For a live UI, use `agent.stream(prompt)` to receive [events](ref/codex/events.md) as the turn progresses.

In a real application, check that the current user can access the order inside the tool. For a tool that changes an address, you can use `@function_tool(needs_approval=True)` and provide an `approval_handler` on the agent. The [approval handler](ref/codex/approvals.md) decides whether to allow that invocation.
In a real application, check that the current user can access the order inside the tool. For a tool that changes an address, you can use `@function_tool(needs_approval=True)` and provide an `approval_handler` on the agent. The [approval handler](ref/codex/approvals.md) decides whether to allow that invocation. Return `False` to deny one call before the tool runs and let the agent continue. See the [tool approval example on the development branch](https://github.com/openai/openai-agents-python/blob/v1-alpha/native/codex/README.md#transform-and-approve-python-tool-calls) for argument normalization and single-call denial.

For the design behind the loop, see [Unrolling the Codex agent loop](https://openai.com/index/unrolling-the-codex-agent-loop/) and [Unlocking the Codex harness](https://openai.com/index/unlocking-the-codex-harness/). The latter describes the App Server, another way to embed the same core. The Python SDK calls the core in process.

Expand All @@ -57,12 +57,18 @@ A new session starts without built-in shell or file tools. Pass an [environment]
| `LocalEnvironment(workspace_directory="./repo")` | On the worker, in the workspace you select. |
| `Sandbox(...)` | On the Codex execution server you connect to. |

Import `LocalEnvironment` and `Sandbox` from `openai_agents.codex`. In all three cases, Python function tools run on your worker with your application's permissions. A sandbox for commands does not move Python functions into that sandbox.
Import `LocalEnvironment` and `Sandbox` from `openai_agents.codex`. In all three cases, Python function tools run on your worker with your application's permissions. Your approval handler also runs on the worker. If your application starts a stdio MCP server, choosing a command sandbox does not move that server into the sandbox.

With `LocalEnvironment`, Codex uses a workspace-write permission profile that limits ordinary writes to the selected workspace and permitted temporary paths. Commands can generally read paths outside the workspace. Commands inherit a limited set of core environment variables rather than the worker's full environment.

A linked Git worktree can need writes to Git metadata outside the workspace. Use a disposable repository copy or an executor configured for the required paths. Your application must enforce permissions inside Python functions and choose any additional host isolation it needs. The workspace setting alone does not guarantee containment of the worker.

### Configure concurrent sessions

The SDK creates a separate temporary Codex home for each open session. You do not need to change `CODEX_HOME` for each session. The embedded runtime ignores the inherited Codex home and project configuration files. Configure agents through SDK arguments, and use [session storage](#what-survives-the-turn) to retain conversation history after a session closes.

`OPENAI_API_KEY` and `OPENAI_BASE_URL` are process-wide environment variables. Set them before opening sessions, and leave them unchanged while sessions are running. For simultaneous sessions that need different credentials or endpoints, use separate worker processes.

### Investigate a failing test

Make a disposable copy of your repository at `./repo-copy`. Prepare the test dependencies in that copy. Set `OPENAI_API_KEY` and `OPENAI_MODEL` as in the [quickstart](quickstart.md#4-run-the-tutor), then save this script as `investigate.py` outside the copy. The agent can change files in `./repo-copy` and make billable model requests.
Expand Down
8 changes: 6 additions & 2 deletions native/codex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ During a turn, `session.snapshot()` returns the previous checkpoint. Use `await

Sessions share a Tokio runtime and keep separate histories and callbacks. A native crash can terminate the Python worker. Start workers before opening a session, or use the `spawn` multiprocessing context.

See [configure concurrent sessions](../../docs/v1/runtime.md#configure-concurrent-sessions) for temporary Codex homes, process-wide credentials, and endpoint settings.

## Choose where to run commands

New sessions start without shell or file access. To grant access to the worker's current directory, pass `environment=LocalEnvironment()`. On resume by ID, supply the executor again or pass `environment=None` to disable it.
Expand Down Expand Up @@ -243,7 +245,9 @@ Branch examples can also use `PreModelRequest` and `PostModelRequest` around log

For Python tools, request approval with `needs_approval=True` and provide a handler that returns a boolean. The handler runs on the Python worker, even when command hooks run on a remote executor.

This branch example normalizes the report name before the approval handler reviews it.
Return `False` from `approval_handler` to deny one call and let the agent continue. On this branch, the handler reviews the arguments after `input_transform`. Raising an exception in `input_transform` stops the turn with `ToolError`.

In this example, the handler approves `" public "` after normalization and rejects `"internal"` before `read_report` runs.

```python
import os
Expand Down Expand Up @@ -271,7 +275,7 @@ def review(request: ApprovalRequest) -> bool:
agent = Agent(model=os.environ["OPENAI_MODEL"], tools=[read_report], approval_handler=review)
```

A handler can be synchronous or async. Returning `False` rejects the call and lets the model continue. Without transforms or `policy_timeout`, a handler exception or non-boolean return stops the turn with `RunError(code="approval_error")`. Interrupting a session cancels pending async handlers.
A handler can be synchronous or async. For MCP tools on this branch, pass `needs_approval=True` to `openai_agents.codex.extensions.mcp.mcp_tools(...)` and check the returned tool name in your approval handler. Without transforms or `policy_timeout`, a handler exception or non-boolean return stops the turn with `RunError(code="approval_error")`. Interrupting a session cancels pending async handlers.

On this branch, `input_transform` replaces arguments before review and execution. `output_transform` filters returned text before Codex receives it. The original model call remains in history and events.

Expand Down
12 changes: 10 additions & 2 deletions src/openai_agents/codex/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ class FunctionTool:
type: Literal["function"] = field(default="function", init=False)

input_transform: InputTransform | None = None
"""Replace decoded arguments before approval and execution; raise to reject."""
"""Replace decoded arguments before approval and execution.

Raising stops the turn with ToolError.
To reject one call and continue, set needs_approval=True and return False from
the agent's approval handler.
"""

output_transform: OutputTransform | None = None
"""Replace the handler's text before returning it to the engine; raise to reject."""
"""Replace the handler's text before returning it to the engine.

Raising stops the turn with ToolError. The handler has already run.
"""

policy_timeout: float | None = None
"""Seconds for input transformation plus approval, and separately for output transformation.
Expand Down
Loading