Recreate OpenAI’s Codex CLI from scratch using HUD
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.
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
When you register tools named 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.
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().
Connect to HUD Hub for full cloud execution and telemetry:
Copy
Ask AI
import hudfrom hud.agents.openai import OpenAIAgentfrom hud.settings import settingsfrom openai import AsyncOpenAI# Connect to HUD Hub environmentenv = hud.Environment()env.connect_hub("codex_environment_sandbox")# Define a scenario for evaluation@env.scenario("coding_task")async def coding_task(task: str): yield f"Complete this task: {task}" yield 1.0 # Reward on completion# Use HUD Gateway for inference (full telemetry)model_client = AsyncOpenAI( base_url=settings.hud_gateway_url, api_key=settings.api_key,)agent = OpenAIAgent.create( model="gpt-5.1", model_client=model_client, validate_api_key=False,)async with hud.eval(env("coding_task", task="Create hello.py"), name="codex-hub") as ctx: await agent.run(ctx, max_steps=20)
The first request may take a few seconds while the environment spins up in the
cloud. Subsequent requests will be faster.
Here’s what makes your HUD Codex identical to the official Codex CLI. The OpenAIAgent automatically detects shell and apply_patch tools and converts them to OpenAI’s native types:
Copy
Ask AI
# What you register:@env.tool()async def shell(commands: list[str], ...): ...# What the model sees (same as official Codex):{"type": "shell"} # Native tool, not a function!
The conversion happens automatically:
Copy
Ask AI
# In hud/agents/openai.pydef _to_openai_tool(self, tool): if tool.name == "shell": return FunctionShellToolParam(type="shell") if tool.name == "apply_patch": return ApplyPatchToolParam(type="apply_patch") # ... regular function tools
This means:
Same model behavior - GPT-5.1 sees native shell and apply_patch tools, exactly like Codex CLI
Same response format - Responses include shell_call and apply_patch_call output types
Same tool execution - Your tools receive the exact same parameters Codex would
The result? Your agent behaves identically to OpenAI’s Codex CLI.
# Local mode - tools run on your machineuv run python examples/06_codex_coding_agent.py --local# Local mode with persistent output directoryuv run python examples/06_codex_coding_agent.py --local --work-dir ./codex_output# Hub mode - full cloud execution (default)uv run python examples/06_codex_coding_agent.py# Custom taskuv run python examples/06_codex_coding_agent.py --local \ --task "Create a Python script that prints the Fibonacci sequence up to 10 numbers"# Verbose outputuv run python examples/06_codex_coding_agent.py --local --verbose