Comparison
Claude Code vs Codex: which to run in production
Both agents run unattended in CI and scripts. This comparison covers what changes when nobody is watching: how each one runs headless, what its sandbox actually contains, how permissions are set, what runs from the repository before you trust it, and where you cap spend.
Checked against each vendor's documentation on .
The short answer
Benchmarks and model quality shift from month to month, so they are not the comparison here. For a production pipeline the lasting differences are in the controls each agent gives you.
Claude Code gives you more levers inside the agent: a per-run dollar cap (--max-budget-usd) and turn limit (--max-turns), permission rules down to a command prefix such as Bash(git diff *), and hooks. Its operating-system sandbox is off by default and wraps shell commands only.
Codex starts locked down: codex exec runs in a read-only sandbox until you widen it, the sandbox covers every command the agent spawns, and its GitHub Action removes sudo and keeps the API key behind a proxy by default. We found no per-run spend cap in its non-interactive docs, so the budget has to be enforced outside the agent.
Neither one is a governance layer on its own. Whichever you pick, run each session in a disposable sandbox with credentials scoped to that session, and enforce the budget before the next model call rather than on a dashboard afterwards.
Side by side
| Feature | Claude Code | Codex |
|---|---|---|
| Made by | Anthropic | OpenAI |
| Headless command | claude -p "<task>" | codex exec "<task>" |
| Machine-readable output | --output-format json or stream-json; --json-schema for a structured result | --json streams JSON Lines events; --output-schema with -o writes a structured result |
| Default permissions when unattended | Manual mode: calls that need approval are denied unless --allowedTools, a permissions.allow rule or a hook allows them | Read-only sandbox; widen with --sandbox workspace-write or danger-full-access |
| OS sandbox | Off by default (sandbox.enabled); Seatbelt on macOS, bubblewrap on Linux and WSL2; wraps shell commands only | On by default; Seatbelt on macOS, bubblewrap on Linux and WSL2, a native sandbox on Windows; applies to every spawned command |
| Command-level rules | Permission rules per tool and command prefix; PermissionRequest and other hooks | execpolicy .rules files that allow, prompt for or forbid command prefixes |
| Project instructions | CLAUDE.md; reads AGENTS.md when the repo has no CLAUDE.md | AGENTS.md (or AGENTS.override.md), merged root to working directory, 32 KiB by default |
| Same result on every runner | --bare skips hooks, skills, plugins, MCP servers, auto memory and CLAUDE.md | --ignore-user-config and --ignore-rules; project config loads only for trusted projects |
| Spend and length limits | --max-budget-usd (checked against a client-side estimate; spend can pass it) and --max-turns | No per-run cap in the codex exec docs we checked; enforce one at your gateway or platform |
| Cost per run | JSON output includes total_cost_usd, an estimate | turn.completed events report input, cached and output tokens |
| GitHub Action | anthropics/claude-code-action: @claude mentions, or a prompt on any event | openai/codex-action@v1: runs codex exec, drops sudo and proxies the API key by default |
| Resume a run | --continue, or --resume <session_id> | codex exec resume --last, or resume <SESSION_ID> |
| Pricing | Changes often: check claude.com/pricing and Anthropic's API pricing | Changes often: check chatgpt.com/pricing and OpenAI's API pricing |
Running headless
Both agents have a one-shot mode for scripts and CI. Claude Code's is claude -p (print mode), which is also the CLI face of Anthropic's Agent SDK. Codex's is codex exec, which streams progress to stderr and prints only the final message to stdout, so it pipes cleanly into the next step.
Both can return a result that matches a JSON Schema, which is what you want when a later job reads the agent's answer: Claude Code with --output-format json --json-schema '<schema>' (the result arrives in structured_output), Codex with --output-schema ./schema.json -o result.json. Check the field is present before you use it: Claude Code's docs note a run that ends without valid structured output simply has no structured_output field.
Exit behaviour differs in ways that matter to a pipeline. Claude Code exits non-zero when a run fails, but reports failures inside the run, such as missing authentication, as the result on stdout. Codex refuses to run outside a Git repository unless you pass --skip-git-repo-check, and --ephemeral stops it writing session files to disk.
claude --bare -p "Run the tests and fix failures" \
--allowedTools "Read,Edit,Bash(npm test *)" \
--max-turns 30 --max-budget-usd 5 --output-format json
codex exec --sandbox workspace-write --json \
"Run the tests and fix failures"What the sandbox actually covers
This is the difference teams most often get wrong. Codex turns its sandbox on by default, and the boundary applies to every command the agent spawns: git, package managers and test runners inherit it. workspace-write lets it edit the workspace and run routine commands inside it; danger-full-access removes the filesystem and network boundary altogether, and OpenAI's docs say to use it only in a controlled environment such as an isolated CI runner or container.
Claude Code's sandbox is off until you enable it with /sandbox or sandbox.enabled. Once on, it wraps Bash, PowerShell and Monitor commands and the processes they start. Its own file and web tools (Read, Edit, Write, WebFetch), hooks and local MCP servers run outside it and are governed by permission rules instead. By default sandboxed commands can still read most of the machine, including files such as ~/.ssh and ~/.aws/credentials, until you add denyRead entries; the network goes through a proxy whose allowed domains start empty.
Neither sandbox is a substitute for running the session somewhere disposable. On a shared runner, put each session in its own container or VM, with no credentials in it that the task does not need, and control egress at that layer too.
Permissions when nobody can answer a prompt
In a non-interactive Claude Code run, a tool call that needs approval is denied unless something approves it. You pre-approve with --allowedTools or permissions.allow rules, which match down to a command prefix: Bash(git diff *) allows any command starting with git diff, and the space before * matters. --permission-mode dontAsk denies everything that would otherwise prompt, and --permission-prompts none tells Claude not to retry what was denied.
Codex combines a sandbox mode with an approval policy. never means it never stops to ask; on-request asks when it needs to cross the sandbox boundary, and approval requests can be routed to an automatic reviewer agent (--approve-for-me, or approvals_reviewer = "auto_review"). The old --full-auto flag is no longer supported: scripts that used it should switch to --sandbox workspace-write.
What runs from the repository before you trust it
An agent that reviews pull requests reads configuration from code someone else wrote, so it matters what that configuration can do. Without --bare, a claude -p session runs the hooks in the project's .claude/settings.json and connects the servers in its .mcp.json, even in a folder you have never trusted, with no trust dialog. Anthropic recommends --bare for scripted runs and says it will become the default for -p.
Local codex exec runs load project configuration and project-defined MCP servers only for projects you have marked trusted (--trust saves that), and OpenAI notes that project-defined MCP servers can run commands outside the sandbox. In both cases, the safe default for untrusted pull requests is to load nothing from the repository and pass the configuration you want on the command line.
Credentials in CI
OpenAI's guidance is explicit: do not set OPENAI_API_KEY or CODEX_API_KEY as a job-level environment variable in a workflow that checks out or runs repository code, because build scripts, tests and dependency hooks in the same job can read it. The Codex GitHub Action starts a Responses API proxy so the key stays out of the agent's environment, and its default safety-strategy (drop-sudo) removes sudo before Codex runs. Its auto-fix example generates a patch in a job with read-only repository permissions and opens the pull request from a second job that never sees the API key.
The Claude Code GitHub Action stores its credential as a repository secret, ANTHROPIC_API_KEY for an API key or CLAUDE_CODE_OAUTH_TOKEN for a subscription token, and acts through the Claude GitHub App, whose permissions you grant on install. The same split-job pattern applies: keep the job that runs the agent on read-only repository permissions and give write access only to the step that publishes its result.
Capping spend
Claude Code's --max-budget-usd stops a print-mode run once its estimated spend reaches the cap, counts subagents' spend toward it, and stops background subagents when it is reached. Anthropic's docs are clear that the estimate is client-side, can differ from your bill, and that spend can pass the cap, so leave headroom. --max-turns bounds the loop itself.
For Codex, the token counts in each turn.completed event let you meter a run as it goes, but we found no flag in the non-interactive docs that stops one at a dollar figure. Check the CLI reference for the version you pin. Either way, a cap inside one agent's process does not cover parallel sessions, retries or a second agent, so the budget that holds is the one enforced where every session's model calls pass.
Instruction files and configuration in Git
Codex reads AGENTS.md, layered from ~/.codex down through the repository to the working directory, with closer files taking precedence and a 32 KiB combined limit by default. Claude Code reads CLAUDE.md, and reads a repository's AGENTS.md instead when there is no CLAUDE.md, so one AGENTS.md can serve both agents. Settings live elsewhere: .claude/settings.json for Claude Code, config.toml for Codex.
Treat all of it as code that changes what an agent may do: review it in pull requests, version it, and promote it between environments the way you would any other deploy configuration.
Which to choose
- Choose Claude Code when you want limits inside the agent: a dollar cap and turn limit per run, command-prefix permission rules, and hooks that can approve, deny or log each call.
- Choose Codex when you want the agent locked down before you configure anything: a read-only default, a sandbox around every spawned command, and a GitHub Action built to keep the API key away from repository code.
- Run both when different teams or tasks prefer different models. Keep each agent's definition in the repository, give every session its own sandbox and short-lived credentials, and log every tool call, diff and token against the person or trigger that started it, so switching agents does not change how you govern them.
Managed platforms take that last option further. Ellipsis, for example, runs Claude Code and Codex agents defined as YAML files in your repository, each session in its own cloud sandbox with credentials minted for that session and revoked when it ends.
Sources
Product details change between releases. Each claim above comes from these pages; check them for the version you run.
- Run Claude Code programmatically (Claude Code docs)
- Configure the sandboxed Bash tool (Claude Code docs)
- CLI reference (Claude Code docs)
- Claude Code GitHub Actions (Claude Code docs)
- Non-interactive mode (Codex docs)
- Sandbox (Codex docs)
- Codex GitHub Action (Codex docs)
- Custom instructions with AGENTS.md (Codex docs)





