|
| 1 | +--- |
| 2 | +display_name: Codex CLI |
| 3 | +icon: ../../../../.icons/openai.svg |
| 4 | +description: Run Codex CLI in your workspace with AgentAPI integration |
| 5 | +verified: true |
| 6 | +tags: [agent, codex, ai, openai, tasks] |
| 7 | +--- |
| 8 | + |
| 9 | +# Codex CLI |
| 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. |
| 12 | + |
| 13 | +```tf |
| 14 | +module "codex" { |
| 15 | + source = "registry.coder.com/coder-labs/codex/coder" |
| 16 | + version = "1.0.0" |
| 17 | + agent_id = coder_agent.example.id |
| 18 | + openai_api_key = var.openai_api_key |
| 19 | + folder = "/home/coder/project" |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- You must add the [Coder Login](https://registry.coder.com/modules/coder/coder-login) module to your template |
| 26 | +- OpenAI API key for Codex access |
| 27 | + |
| 28 | +## Usage Example |
| 29 | + |
| 30 | +- Simple usage Example: |
| 31 | + |
| 32 | +```tf |
| 33 | +module "codex" { |
| 34 | + count = data.coder_workspace.me.start_count |
| 35 | + source = "registry.coder.com/coder-labs/codex/coder" |
| 36 | + version = "1.0.0" |
| 37 | + agent_id = coder_agent.example.id |
| 38 | + openai_api_key = "..." |
| 39 | + codex_model = "o4-mini" |
| 40 | + install_codex = true |
| 41 | + codex_version = "latest" |
| 42 | + folder = "/home/coder/project" |
| 43 | + codex_system_prompt = "You are a helpful coding assistant. Start every response with `Codex says:`" |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +- Example usage with Tasks: |
| 48 | + |
| 49 | +```tf |
| 50 | +# This |
| 51 | +data "coder_parameter" "ai_prompt" { |
| 52 | + type = "string" |
| 53 | + name = "AI Prompt" |
| 54 | + default = "" |
| 55 | + description = "Initial prompt for the Codex CLI" |
| 56 | + mutable = true |
| 57 | +} |
| 58 | +
|
| 59 | +module "coder-login" { |
| 60 | + count = data.coder_workspace.me.start_count |
| 61 | + source = "registry.coder.com/coder/coder-login/coder" |
| 62 | + version = "1.0.31" |
| 63 | + agent_id = coder_agent.example.id |
| 64 | +} |
| 65 | +
|
| 66 | +module "codex" { |
| 67 | + source = "registry.coder.com/coder-labs/codex/coder" |
| 68 | + agent_id = coder_agent.example.id |
| 69 | + openai_api_key = "..." |
| 70 | + ai_prompt = data.coder_parameter.ai_prompt.value |
| 71 | + folder = "/home/coder/project" |
| 72 | + approval_policy = "never" # Full auto mode |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +> [!WARNING] |
| 77 | +> **Security Notice**: This module configures Codex with a `workspace-write` sandbox that allows AI tasks to read/write files in the specified folder. While the sandbox provides security boundaries, Codex can still modify files within the workspace. Use this module in trusted environments and be aware of the security implications. |
| 78 | +
|
| 79 | +## How it Works |
| 80 | + |
| 81 | +- **Install**: The module installs Codex CLI and sets up the environment |
| 82 | +- **System Prompt**: If `codex_system_prompt` and `folder` are set, creates the directory (if needed) and writes the prompt to `AGENTS.md` |
| 83 | +- **Start**: Launches Codex CLI in the specified directory, wrapped by AgentAPI |
| 84 | +- **Configuration**: Sets `OPENAI_API_KEY` environment variable and passes `--model` flag to Codex CLI (if variables provided) |
| 85 | + |
| 86 | +## Sandbox Configuration |
| 87 | + |
| 88 | +The module automatically configures Codex with a secure sandbox that allows AI tasks to work effectively: |
| 89 | + |
| 90 | +- **Sandbox Mode**: `workspace-write` - Allows Codex to read/write files in the specified `folder` |
| 91 | +- **Approval Policy**: `on-request` - Codex asks for permission before performing potentially risky operations |
| 92 | +- **Network Access**: Enabled within the workspace for package installation and API calls |
| 93 | + |
| 94 | +### Customizing Sandbox Behavior |
| 95 | + |
| 96 | +You can customize the sandbox behavior using dedicated variables: |
| 97 | + |
| 98 | +#### **Using Dedicated Variables (Recommended)** |
| 99 | + |
| 100 | +For most use cases, use the dedicated sandbox variables: |
| 101 | + |
| 102 | +```tf |
| 103 | +module "codex" { |
| 104 | + source = "registry.coder.com/coder-labs/codex/coder" |
| 105 | + # ... other variables ... |
| 106 | +
|
| 107 | + # Containerized environments (fixes Landlock errors) |
| 108 | + sandbox_mode = "danger-full-access" |
| 109 | +
|
| 110 | + # Or for read-only mode |
| 111 | + # sandbox_mode = "read-only" |
| 112 | +
|
| 113 | + # Or for full auto mode |
| 114 | + # approval_policy = "never" |
| 115 | +
|
| 116 | + # Or disable network access |
| 117 | + # network_access = false |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +#### **Using extra_codex_settings_toml (Advanced)** |
| 122 | + |
| 123 | +For advanced configuration or when you need to override multiple settings: |
| 124 | + |
| 125 | +```tf |
| 126 | +module "codex" { |
| 127 | + source = "registry.coder.com/coder-labs/codex/coder" |
| 128 | + # ... other variables ... |
| 129 | +
|
| 130 | + extra_codex_settings_toml = <<-EOT |
| 131 | + # Any custom Codex configuration |
| 132 | + model = "gpt-4" |
| 133 | + disable_response_storage = true |
| 134 | + EOT |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +> [!NOTE] |
| 139 | +> The dedicated variables (`sandbox_mode`, `approval_policy`, `network_access`) are the recommended way to configure sandbox behavior. Use `extra_codex_settings_toml` only for advanced configuration that isn't covered by the dedicated variables. |
| 140 | +
|
| 141 | +## Troubleshooting |
| 142 | + |
| 143 | +- Check installation and startup logs in `~/.codex-module/` |
| 144 | +- Ensure your OpenAI API key has access to the specified model |
| 145 | + |
| 146 | +> [!IMPORTANT] |
| 147 | +> To use tasks with Codex CLI, ensure you have the `openai_api_key` variable set, and **you create a `coder_parameter` named `"AI Prompt"` and pass its value to the codex module's `ai_prompt` variable**. [Tasks Template Example](https://registry.coder.com/templates/coder-labs/tasks-docker). |
| 148 | +> The module automatically configures Codex with your API key and model preferences. |
| 149 | +> folder is a required variable for the module to function correctly. |
| 150 | +
|
| 151 | +## References |
| 152 | + |
| 153 | +- [OpenAI API Documentation](https://platform.openai.com/docs) |
| 154 | +- [AgentAPI Documentation](https://github.com/coder/agentapi) |
| 155 | +- [Coder AI Agents Guide](https://coder.com/docs/tutorials/ai-agents) |
0 commit comments