How to Let Codex Run a Command Without Asking
To let Codex run one command outside its sandbox without asking you each time, add prefix_rule(pattern = ["npm", "test"], decision = "allow") to ~/.codex/rules/default.rules and restart Codex. The file, the three decisions, how Codex treats chained shell commands, and how to test a rule with codex execpolicy check, read in OpenAI's docs on Nov 17, 2026.
To let Codex run a command outside its sandbox without asking you every time, add this line to ~/.codex/rules/default.rules and restart Codex: prefix_rule(pattern = ["npm", "test"], decision = "allow") (Rules, OpenAI docs, read Nov 17, 2026). From then on any command that starts with npm test, such as npm test -- --watch=false, runs with no approval prompt, while everything else still asks. OpenAI calls rules "experimental and may change". When you add a command to the allow list from the CLI's approval prompt, Codex writes the same kind of rule into the same file for you.
Rules sit on top of the approval modes OpenAI showed in its Codex CLI demo of October 2025: read only, auto and full access, with auto the default, which lets Codex read and change files in the current project directory but nothing else on your computer. A command that needs to go beyond that is what a rule can let through.
The blunt alternative is to stop asking altogether. Guided Resolution's April 2026 video starts Codex with --full-auto (reading, writing and running local actions in the sandbox without prompts), or sets sandbox_mode = "danger-full-access" and approval_policy = "never" in config.toml, and warns that this is risky and only for an agent you trust. A rule is the narrow version: one command allowed, everything else still asks.
Next: the sandbox and approval presets these rules sit on top of, a hook that checks every command before it runs or where CODEX_HOME moves the rules folder.
The rule, field by field
A rule is one call to prefix_rule() in a .rules file written in Starlark, a safe subset of Python syntax. A fuller version of the line above, ours, with the optional checks OpenAI's page describes:
prefix_rule(
pattern = ["npm", "test"],
decision = "allow",
justification = "Running the test suite is safe",
match = ["npm test", "npm test -- --watch=false"],
not_match = ["npm install"],
)
| Field | Required | What it does |
|---|---|---|
pattern |
Yes | The start of the command, one list item per argument; an item can itself be a list of alternatives, such as ["view", "list"] |
decision |
No, default "allow" |
allow runs it without asking, prompt asks every time, forbidden blocks it without asking |
justification |
No | A reason Codex may show in the prompt or the refusal |
match, not_match |
No | Example commands Codex checks when it loads the file, so a wrong rule fails at start-up instead of later |
The pattern is matched against the command's argument list as an exact prefix. OpenAI's own example shows the cost of that: a rule for gh pr view does not match gh pr --repo openai/codex view 7888, because the flag sits between the words.
Three rules worth copying
Each of these is ours, built from the fields above:
- Allow read-only Git commands:
prefix_rule(pattern = ["git", ["status", "diff", "log"]], decision = "allow"). One rule covers three commands, because the second item is a list of alternatives. - Ask before every push:
prefix_rule(pattern = ["git", "push"], decision = "prompt"). - Block a tool and say what to use instead:
prefix_rule(pattern = ["grep"], decision = "forbidden", justification = "Use rg instead of grep."). OpenAI's page suggests naming the alternative in aforbiddenrule's justification.
When several rules match one command, the strictest wins: forbidden beats prompt, and prompt beats allow.
Chained commands are checked one by one
A rule cannot be used to slip something past it. When Codex is asked to run a shell string such as bash -lc "git add . && rm -rf /", it splits a plain chain (words joined by &&, ||, ; or |) into its commands and checks each, so allowing git add does not allow the rm -rf / behind it. A script with redirection, $(...), variables, wildcards or control flow is not split: the whole bash -lc "<script>" call is checked as one command, and a prefix rule for one of its parts does not apply.
Test a rule before you trust it
Codex has a checker that prints the decision as JSON:
codex execpolicy check --pretty --rules ~/.codex/rules/default.rules -- npm test -- --watch=false
It reports the strictest decision and every rule that matched, with their justifications. Pass --rules more than once to combine files.
Where rule files are read from
~/.codex/rules/for you, on every project. Under a differentCODEX_HOME, the folder moves with it.<repo>/.codex/rules/for one project, read only once you have trusted that project's.codex/folder.- Team Config locations, and admin rules in
requirements.toml, which can enforce restrictive rules you cannot override.
Codex reads them at start-up, which is why a new rule needs a restart. Rules apply to Codex on your computer: OpenAI's page warns that Work Cloud with local computer access does not read local rule files, and an admin sets its command rules in Agent Security instead. With Smart approvals on, the default, Codex may suggest a prefix_rule itself when it asks to leave the sandbox; OpenAI advises reviewing the suggested prefix before accepting it, since a short prefix such as ["npm"] allows every npm command.
We think a rule that allows a whole tool (["npm"], ["python"]) is the most common mistake here, because it allows that tool's install, publish and script commands too; allow the exact subcommand you mean.
Sources
- Rules, OpenAI docs (learn.chatgpt.com), read Nov 17, 2026
- How To Allow Or Approve All Permissions In Codex, Guided Resolution, Apr 1, 2026 (video)
- Using OpenAI Codex CLI with GPT-5-Codex, OpenAI, Oct 14, 2025 (video)
Moments
Using OpenAI Codex CLI with GPT-5-Codex
OpenAI
How To Allow Or Approve All Permissions In Codex
Guided Resolution
See also
-
CodexCodex is OpenAI's coding agent: it reads a codebase, edits files, runs commands and tests, reviews pull requests and works in the cloud. As of Nov 6, 2026 it lives inside the ChatGPT desktop app, in the Codex CLI, in IDE extensions, in Codex Cloud and on mobile, and every ChatGPT plan includes some of it, Free and Go too.
-
Codex CLICodex CLI is OpenAI's open-source coding agent for the terminal: install it with one command, run codex in a project folder, sign in with ChatGPT or an API key, and it reads, edits and runs your code inside a sandbox. This page covers its commands, models, approval modes, config.toml, codex exec, resume and MCP as of Nov 6, 2026. -
How to Add a Hook in CodexTo add a hook in Codex, put a hooks.json in ~/.codex (or a project's .codex folder) naming an event such as PreToolUse, a matcher such as Bash and a command to run, then trust it with /hooks. A worked hook that blocks rm -rf with exit code 2, the 14 events, and the defaults, read in OpenAI's docs on Nov 17, 2026. -
Codex Environment VariablesOpenAI's docs list 12 public environment variables for Codex, read Nov 17, 2026, plus OPENAI_API_KEY for image batches. CODEX_HOME moves its whole folder (default ~/.codex), CODEX_API_KEY gives codex exec an API key, CODEX_NON_INTERACTIVE=1 installs without prompts and RUST_LOG=debug turns on logging. Each one with a line to copy. -
AGENTS.mdAGENTS.md is a plain Markdown file of instructions for AI coding agents, a "README for agents", that Codex reads before doing any work. Codex loads a global ~/.codex/AGENTS.md and then every AGENTS.md from the repository root down to the working folder, closest last, up to 32 KiB; /init writes a first one. The open format is now stewarded by the Agentic AI Foundation.
-
Codex AppThe Codex app is now the Codex view of the ChatGPT desktop app for macOS, Windows and Linux, where Codex runs parallel chats, worktrees, Computer Use, Appshots, Goal mode, Record and Replay and remote control from a phone. It replaced the standalone Codex app OpenAI launched on Feb 2, 2026.