Custom GPTs retire: Dec 11, 2026Date announced, hour not (as of Nov 7, 2026). FedRAMP workspaces keep them until Feb 11, 2027; move a GPT to a plugin before then.

17Days23Hours13Minutes18Seconds

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.

Hands typing on a dark laptop with the OpenAI logo beside a cup of coffee, with the words Codex CLI Updates
Hands typing on a dark laptop with the OpenAI logo beside a cup of coffee, with the words Codex CLI Updates OpenAI

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 in tool_input.command, and the tool's name in tool_name (Bash for shell commands).
  • Exit code 2 with 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 an updatedInput holding a new command rewrites 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

Moments

How to Build Custom Hooks for Codex and Claude Code

Pretty Printed

  1. Pretty Printed's September 2026 tutorial shows the exit code at work with an even blunter hook: a UserPromptSubmit…

Switching from Claude Code to Codex

Ben Holmes

  1. The event names follow Claude Code's, which makes moving hooks across easy but not complete: Ben Holmes, who switched…

See also

About this page

Last updated Nov 22, 2026 by the Vi.ki team. Page text CC BY-SA 4.0.

Edit History

Hotkeys

/
Jump to the search box
? or Ctrl + /
Open this list
Esc
Close this list, the photo viewer, or the account menu

Video

Space or K
Play or pause
← →
Back or forward 5 seconds
J L
Back or forward 10 seconds
0 to 9
Go to 0% to 90% of the video
, .
Previous or next frame, while paused
M
Mute or unmute
C
Captions on or off
F
Full screen on or off
A D
Previous or next moment

Photo viewer

← → or A D
Previous or next photo
↑ ↓ or W S
Previous or next photo
Home End
First or last photo
I
Show or hide the photo's info
Z
Clickless on or off: a pointer over a thumbnail shows it
N
Compare with the last photo: back to the one shown before this, and N again comes back
Q
Move the thumbnails to the next edge: top, bottom, left and round
, or Ctrl + ,
Open or close the viewer's settings
V
Clickless pan: a zoomed photo follows the pointer, rather than being dragged
Shift
Browse on or off
B
Show or hide the button bar
T
Show or hide the thumbnails
1
100% zoom on or off
0
Rate the photo as no opinion; again takes the answer back
+ -
Rate the photo a step higher or lower
F
Full screen: the photo alone. F again puts everything back.
Back to top