This guide shows you how to build your own Codex - a 1:1 recreation of OpenAI’s Codex CLI using the HUD SDK. The implementation matches Codex’s behavior exactly because HUD’s tools conform to the same OpenAI Responses API specifications.Documentation Index
Fetch the complete documentation index at: https://docs.hud.ai/llms.txt
Use this file to discover all available pages before exploring further.
Example Code
The complete working example - your own Codex in ~100 lines of Python.
Why Build Your Own Codex?
OpenAI’s Codex CLI is a coding agent that uses two native tools:shell and apply_patch. With HUD, you can:
- Customize behavior - Add logging, approval flows, or custom security policies
- Traces - Get detailed trajectories, with every tool call and model response recorded on hud.ai
- Local or cloud - Run on your machine, in Docker, or on HUD Cloud
- Evaluate systematically - Run your Codex against benchmarks and track improvements
How It Works
HUD’s tool implementations match OpenAI’s specifications exactly:| OpenAI Codex Tool | HUD Implementation | Spec Conformance |
|---|---|---|
shell | hud.tools.coding.ShellTool | ShellAction → ShellResult with stdout, stderr, outcome |
apply_patch | hud.tools.coding.ApplyPatchTool | V4A diff format, create_file/update_file/delete_file |
shell or apply_patch, the OpenAIAgent automatically converts them to OpenAI’s native tool types - the model sees the exact same interface as the official Codex CLI.
Two Execution Modes
Just like OpenAI’s Codex CLI can run locally or connect to cloud services, your HUD Codex supports both:| Mode | Like Codex CLI… | API Keys Required |
|---|---|---|
Local (--local) | Running codex on your machine | OPENAI_API_KEY |
| Hub (default) | Running in a sandboxed cloud environment | HUD_API_KEY |
HUD_API_KEY is set.
Build Your Codex
Local Mode
shell and apply_patch tools for OpenAI models.
Hub Mode (Cloud Execution)
Prerequisites: You must create the
codex_environment_sandbox environment
in hud.ai first before using hub mode. Go to
hud.ai → New → Environment → Import from
hud-evals/codex_environment_sandbox.
Once deployed, your environment will be accessible via connect_hub().The first request may take a few seconds while the environment spins up in the
cloud. Subsequent requests will be faster.
Tool Specifications
Shell Tool
TheShellTool provides a persistent bash session for executing commands.
Features:
- Auto-restart on error (session automatically restarts if needed)
- Dynamic timeout via
timeout_msparameter - Persistent environment (exported variables, working directory)
- Concurrent command execution support
Apply Patch Tool
TheApplyPatchTool creates, updates, and deletes files using OpenAI’s V4A diff format.
Operations:
| Operation | Description | Diff Required |
|---|---|---|
create_file | Create a new file | Yes |
update_file | Modify existing file | Yes |
delete_file | Remove a file | No |
Automatic native tool conversion
Here’s what makes your HUD Codex identical to the official Codex CLI. TheOpenAIAgent automatically detects shell and apply_patch tools and converts them to OpenAI’s native types:
- Same model behavior - GPT-5.3-codex sees native
shellandapply_patchtools, exactly like Codex CLI - Same response format - Responses include
shell_callandapply_patch_calloutput types - Same tool execution - Your tools receive the exact same parameters Codex would
Complete Example
Here’s a full runnable script:CLI Usage
Setting Up API Keys
Create a.env file in your project root:
- HUD_API_KEY: hud.ai/project/api-keys
- OPENAI_API_KEY: platform.openai.com/api-keys
Running the Example
CLI Options
| Flag | Default | Description |
|---|---|---|
--local | Off | Run locally (tools on your machine, OpenAI direct) |
--task | Hello World script | The coding task to complete |
--model | gpt-5.3-codex | Codex-capable model (gpt-5.4, gpt-5.3-codex) |
--work-dir | Temp directory | Working directory (local mode only) |
--max-steps | 20 | Maximum agent steps |
--verbose | Off | Enable verbose output |
Security Considerations
Comparison with Official Codex CLI
| Feature | OpenAI Codex CLI | Your HUD Codex |
|---|---|---|
| Shell execution | shell native tool | ShellTool (same spec) |
| File editing | apply_patch with V4A diff | ApplyPatchTool (same spec) |
| Persistent bash session | Yes | Yes |
| Auto-restart on error | Yes | Yes |
| Custom approval flows | Limited | Full control |
| Observability | Basic logs | Full traces on hud.ai |
| Cloud execution | No | Yes (Hub mode) |
| Benchmarking | No | Built-in with hud.eval |
See Also
- OpenAI Codex CLI - The official Codex CLI that this recreates
- Codex-capable models - OpenAI models that support native shell and apply_patch tools
- Tools Reference - Complete tool documentation
- OpenAI Agent - Agent configuration options
- Environments as Data - Running agents safely