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
description: Run Codex CLI in your workspace with AgentAPI integration
4
+
description: Install and configure the Codex CLI in your workspace.
5
5
verified: true
6
-
tags: [agent, codex, ai, openai, tasks, aibridge]
6
+
tags: [agent, codex, ai, openai, ai-gateway]
7
7
---
8
8
9
9
# Codex CLI
10
10
11
-
Run Codex CLI in your workspace to access OpenAI's models through the Codex interface, with custom pre/post install scripts. This module integrates with [AgentAPI](https://github.com/coder/agentapi) for Coder Tasks compatibility.
11
+
Install and configure the [Codex CLI](https://github.com/openai/codex) in your workspace.
> If upgrading from v4.x.x of this module: v5 is a major refactor that drops support for [Coder Tasks](https://coder.com/docs/ai-coder/tasks) and [Boundary](https://coder.com/docs/ai-coder/agent-firewall). v5 also assumes npm is pre-installed; it no longer bootstraps Node.js. Keep using v4.x.x if you depend on them. See the [PR description](https://github.com/coder/registry/pull/879) for a full migration guide.
[AI Bridge](https://coder.com/docs/ai-coder/ai-bridge) is a Premium Coder feature that provides centralized LLM proxy management. To use AI Bridge, set `enable_aibridge = true`. Requires Coder version 2.30+
46
-
47
-
For tasks integration with AI Bridge, add `enable_aibridge = true` to the [Usage with Tasks](#usage-with-tasks) example below.
- Configures Codex to use the aibridge model_provider with `base_url` pointing to `${data.coder_workspace.me.access_url}/api/v2/aibridge/openai/v1` and `env_key` pointing to the workspace owner's session token
This allows Codex to route API requests through Coder's AI Bridge instead of directly to OpenAI's API.
76
-
Template build will fail if `openai_api_key` is provided alongside `enable_aibridge = true`.
57
+
> [!NOTE]
58
+
> The `coder_app` command re-executes on every pane reconnect. This works for interactive `codex` (which stays alive), but one-shot commands like `codex exec` will re-run each time. For one-shot prompts, use a `coder_script` (runs once at startup) and a `coder_app` that attaches to the existing session (e.g. via tmux/screen).
77
59
78
-
### Usage with Tasks
60
+
### Usage with AI Gateway
79
61
80
-
This example shows how to configure Codex with Coder tasks.
62
+
[AI Gateway](https://coder.com/docs/ai-coder/ai-gateway) is a Premium Coder feature that provides centralized LLM proxy management. Requires Coder >= 2.30.0.
This example shows how to configure the Codex module to run the agent behind a process-level boundary that restricts its network access.
74
+
When `enable_ai_gateway = true`, the module configures Codex to use the `aigateway` model provider in `config.toml` with the workspace owner's session token for authentication.
106
75
107
-
By default, when `enable_boundary = true`, the module uses `coder boundary` subcommand (provided by Coder) without requiring any installation.
> `enable_ai_gateway = true` is mutually exclusive with `openai_api_key`. Setting both fails at plan time.
119
78
120
79
> [!NOTE]
121
-
> For developers: The module also supports installing boundary from a release version (`use_boundary_directly = true`) or compiling from source (`compile_boundary_from_source = true`). These are escape hatches for development and testing purposes.
80
+
> If you provide a custom `base_config_toml`, the module writes it verbatim and does not inject `model_provider = "aigateway"` automatically. Add it to your config yourself:
81
+
>
82
+
> ```toml
83
+
> model_provider = "aigateway"
84
+
> ```
122
85
123
86
### Advanced Configuration
124
87
125
-
This example shows additional configuration options for custom models, MCP servers, and base configuration.
> This module configures Codex with a `workspace-write` sandbox that allows AI tasks to read/write files in the specified workdir. While the sandbox provides security boundaries, Codex can still modify files within the workspace. Use this module _only_ in trusted environments and be aware of the security implications.
157
-
158
-
## How it Works
159
-
160
-
-**Install**: The module installs Codex CLI and sets up the environment
161
-
-**System Prompt**: If `codex_system_prompt` is set, writes the prompt to `AGENTS.md` in the `~/.codex/` directory
162
-
-**Start**: Launches Codex CLI in the specified directory, wrapped by AgentAPI
163
-
-**Configuration**: Sets `OPENAI_API_KEY` environment variable and passes `--model` flag to Codex CLI (if variables provided)
164
-
-**Session Continuity**: When `continue = true` (default), the module automatically tracks task sessions in `~/.codex-module/.codex-task-session`. On workspace restart, it resumes the existing session with full conversation history. Set `continue = false` to always start fresh sessions.
165
-
166
-
## State Persistence
113
+
### Serialize a downstream `coder_script` after the install pipeline
167
114
168
-
AgentAPI can save and restore its conversation state to disk across workspace restarts. This complements `continue` (which resumes the Codex CLI session) by also preserving the AgentAPI-level context. Enabled by default, requires agentapi >= v0.12.0 (older versions skip it with a warning).
169
-
170
-
To disable:
115
+
The module exposes the `scripts` output: an ordered list of `coder exp sync` names for the scripts this module creates (pre_install, install, post_install). Scripts that were not configured are absent.
When no custom `base_config_toml` is provided, the module uses these secure defaults:
184
-
185
-
```toml
186
-
sandbox_mode = "workspace-write"
187
-
approval_policy = "never"
188
-
preferred_auth_method = "apikey"
189
-
190
-
[sandbox_workspace_write]
191
-
network_access = true
192
-
```
193
-
194
-
> [!NOTE]
195
-
> If no custom configuration is provided, the module uses secure defaults. The Coder MCP server is always included automatically. For containerized workspaces (Docker/Kubernetes), you may need `sandbox_mode = "danger-full-access"` to avoid permission issues. For advanced options, see [Codex config docs](https://github.com/openai/codex/blob/main/codex-rs/config.md).
143
+
When no custom `base_config_toml` is provided, the module uses a minimal default with `preferred_auth_method = "apikey"`. For advanced options, see [Codex config docs](https://developers.openai.com/codex/config-advanced).
196
144
197
145
## Troubleshooting
198
146
199
-
- Check installation and startup logs in `~/.codex-module/`
200
-
- Ensure your OpenAI API key has access to the specified model
147
+
Check the log files in `~/.coder-modules/coder-labs/codex/logs/` for detailed information.
201
148
202
-
> [!IMPORTANT]
203
-
> To use tasks with Codex CLI, ensure you have the `openai_api_key` variable set. [Tasks Template Example](https://registry.coder.com/templates/coder-labs/tasks-docker).
204
-
> The module automatically configures Codex with your API key and model preferences.
205
-
> workdir is a required variable for the module to function correctly.
0 commit comments