How to Add a Hook in Codex
To 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.
To add a hook in Codex, create ~/.codex/hooks.json with {"hooks": {"PreToolUse": [{"matcher": "Bash", "hooks": [{"type": "command", "command": "python3 ~/.codex/hooks/guard.py"}]}]}}, then open /hooks in the CLI and trust it: Codex runs no new or changed hook until you do (Hooks, OpenAI docs, read Nov 17, 2026). That hook runs guard.py before every shell command Codex wants to run, hands it the command as JSON on standard input, and blocks the command if the script exits with code 2. Hooks are on by default; a hook may also sit in config.toml as an inline [hooks] table, or in a project's own .codex/ folder once you have trusted that project.
Next: rules, the simpler way to allow or block a command, the file that tells Codex how to work in a project or bringing hooks over from Claude Code.
A worked hook: block rm -rf
The script, ours, written to the input and output OpenAI's page describes. Save it as ~/.codex/hooks/guard.py:
import json, sys
event = json.load(sys.stdin)
command = event.get("tool_input", {}).get("command", "")
if "rm -rf" in command:
print("Blocked by guard.py: use a narrower delete.", file=sys.stderr)
sys.exit(2)
sys.exit(0)
What each part does:
- Every command hook gets one JSON object on standard input. For
PreToolUse, the shell command is intool_input.command, and the tool's name intool_name(Bashfor shell commands). - Exit code
2with a reason on standard error blocks the call. Plain text on standard output is ignored. - Instead of exiting with 2, a hook may print JSON:
{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "..."}}denies the call, and"permissionDecision": "allow"with anupdatedInputholding a newcommandrewrites it. "permissionDecision": "ask"is parsed but not supported yet: OpenAI's page says Codex marks such a run as failed and lets the tool call go ahead, so a hook that means to stop something must deny it.
Pretty Printed's September 2026 tutorial shows the exit code at work with an even blunter hook: a UserPromptSubmit script, listed and switched on under /hooks, that prints "I cannot allow you to do that" to standard error and exits with 2, so every prompt is blocked before the model sees it. With the exit line removed, the same message is printed and nothing is blocked: only the code 2 tells Codex to stop.
A string test like this one is a seat belt, not a lock: rm -r -f passes it. For a fixed list of commands, a rule with decision = "forbidden" is stricter, because rules check each command of a chain separately.
The 14 events
| When | Events |
|---|---|
| During a turn | PreToolUse, PermissionRequest, PostToolUse, PreCompact, PostCompact, UserPromptSubmit, SubagentStop, Stop |
| For each model request | PreModelRequest, PostModelRequest |
| When you interrupt a turn | Interrupt |
| When a session or subagent starts | SessionStart, SubagentStart |
| When the main thread ends | SessionEnd |
That is our count of OpenAI's table, read Nov 17, 2026: 14 events. Interrupt and SessionEnd do not run for subagents. PreToolUse sees shell commands, file edits made through apply_patch (a matcher of apply_patch, Edit or Write catches them), MCP tool calls and other local tools.
The event names follow Claude Code's, which makes moving hooks across easy but not complete: Ben Holmes, who switched in May 2026, found that Codex supports skills, MCP servers and hooks with small differences in how they are written, and that not every Claude Code hook has a Codex event, though the fundamental ones such as PreToolUse do.
The defaults that catch people out
| Setting | Default | Note |
|---|---|---|
timeout |
600 seconds | In seconds; SessionEnd and Interrupt default to 1 second and allow at most 3 |
| Several hooks on one event | All run, at the same time | One hook cannot stop another from starting |
| Several hook files | All load | A higher config layer adds to a lower one, it does not replace it |
| Working directory | The session's | So a project hook should find its script from the Git root, as in $(git rev-parse --show-toplevel)/.codex/hooks/check.py |
| Trust | Required | Trust is stored against the hook's hash, so editing a hook asks again |
| Windows | command |
Add commandWindows for a Windows-only command |
Other fields: statusMessage shows a line while the hook runs, async: true runs a command hook in the background, and type: "mcp_tool" calls a tool on an MCP server you have already connected instead of a script. Handlers of type prompt and agent are read but skipped.
The same hook in config.toml
The hook can live in config.toml instead of hooks.json: OpenAI's page gives the TOML form as an array of tables named hooks.PreToolUse, holding matcher = "^Bash$", with a nested array hooks.PreToolUse.hooks holding type = "command", the command and any timeout or statusMessage, the same three levels as the JSON (event, matcher group, handlers). Its Hooks page has the full example to copy.
Keep one form per folder: if one config layer holds both a hooks.json and an inline [hooks] table, Codex merges them and warns at start-up.
Turning hooks off
Put hooks = false under [features] in config.toml (the old key codex_hooks still works). For a one-off run whose hooks you have checked yourself, --dangerously-bypass-hook-trust runs enabled hooks without stored trust. Hooks an admin sets through requirements.toml, MDM or the cloud are marked managed, trusted by policy, and cannot be turned off from /hooks.
Sources
- Hooks, OpenAI docs (learn.chatgpt.com), read Nov 17, 2026
- How to Build Custom Hooks for Codex and Claude Code, Pretty Printed, Sep 3, 2026 (video)
- Switching from Claude Code to Codex, Ben Holmes, May 13, 2026 (video)
Moments
How to Build Custom Hooks for Codex and Claude Code
Pretty Printed
Switching from Claude Code to Codex
Ben Holmes
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.
-
How to Let Codex Run a Command Without AskingTo 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. -
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.
-
How to Switch from Claude Code to CodexTo move a Claude Code or Cursor setup into Codex, type /import in an idle Codex CLI session or open Settings > Import in the ChatGPT desktop app, pick the source and tick what to bring; the CLI takes up to 50 chats from the last 30 days and the old setup is left untouched. What lands where, as OpenAI's docs gave it on Nov 12, 2026.
-
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. -
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.