Codex Hooks: How to Block a Tool Call Before It Runs
Codex hooks run your scripts or MCP tools at 14 events. Since rust-v0.173.0, onFailure block stops a tool when its PreToolUse check fails.
Codex hooks are scripts or MCP tools that Codex runs at fixed points in a session, such as PreToolUse before a tool call, and a hook there can deny the call. Since rust-v0.173.0, released 2 Dec 2026, a hook with onFailure set to block also stops the tool when the check itself fails. Checked 2 Dec 2026.
What are Codex hooks? #
A Codex hook is a rule that says “when this event fires, run this handler”. OpenAI’s hooks docs call hooks “an extensibility framework for Codex” that let you “run scripts or MCP tools during the agentic loop”. Only command and mcp_tool handlers run; prompt and agent handlers “are parsed but skipped”. New to Codex? Start with what Codex is.
Hooks are on by default, and matching command hooks for one event start at the same time. Hooks that no admin policy manages “must be reviewed and trusted before they run”, so a new or changed hook is skipped until you trust it in /hooks.
Which events can a Codex hook use? #
The hooks docs list 14 events, checked 2 Dec 2026. For tool events, the matcher filters on the tool name.
| Event | When it runs | What it can do | Source | |---|---|---|---| | SessionStart | A session starts or resumes | Add context | Docs | | SubagentStart | A subagent starts | Add context; can’t stop it | Docs | | UserPromptSubmit | Before your prompt is sent | Add context or block the prompt | Docs | | PreToolUse | Before a tool call | Deny it, rewrite its input, add context | Docs | | PermissionRequest | Codex is about to ask for approval | Allow or deny | Docs | | PostToolUse | After a tool produces output | Replace the result with feedback; can’t undo it | Docs | | PreCompact ,PostCompact | Before and after compaction | Stop with continue: false | Docs | | PreModelRequest ,PostModelRequest | Each model request attempt | Log only | Docs | | SubagentStop | A subagent finishes | Keep the subagent going | Docs | | Stop | A turn stops | Keep Codex going with your reason as a new prompt | Docs | | Interrupt | You interrupt a turn | Record it; can’t prevent it | Docs | | SessionEnd | The main thread ends | Advisory only | Docs |
Where do Codex hooks go? #
Codex reads a hooks.json file, or the same hooks inline under [hooks] in config.toml, in each of these places:
- System:
/etc/codex/hooks.jsonon macOS and Linux,%ProgramData%\OpenAI\Codex\hooks.jsonon Windows - User:
$CODEX_HOME/hooks.json, which is~/.codex/hooks.jsonon macOS and Linux whenCODEX_HOMEis unset - Project:
.codex/hooks.json, loaded only from trusted project layers
Enabled plugins can also bring hooks, by default from hooks/hooks.json in the plugin root. If one layer has both hooks.json and [hooks] in config.toml, Codex loads both and warns at startup. To turn hooks off, set hooks = false under [features].
Our example: this config.toml entry follows the inline TOML in the hooks docs, plus onFailure from the rust-v0.173.0 notes, spelled as in that tag’s source. It runs a guard before every Bash command:
[[hooks.PreToolUse]]
matcher = "^Bash$"
[[hooks.PreToolUse.hooks]]
type = "command"
command = '/bin/bash "$(git rev-parse --show-toplevel)/.codex/hooks/check_secrets.sh"'
timeout = 30
statusMessage = "Checking Bash command"
onFailure = "block"
timeout is in seconds, 600 by default for most hooks. The git root path follows the docs’ advice.
How do you block a tool call with a Codex hook? #
A PreToolUse hook blocks a call in one of three documented ways: print JSON with hookSpecificOutput.permissionDecision set to "deny" and a permissionDecisionReason, print the older decision: "block" shape with a reason, or exit with code 2 and write the reason to stderr. The hook gets tool_name and tool_input on stdin; for Bash and apply_patch, the command is in tool_input.command.
By default a hook that fails does not stop the action. The docs page, checked 2 Dec 2026, says MCP hook errors “don’t block the operation” and does not mention onFailure. The rust-v0.173.0 release notes, published 2 Dec 2026 at 02:16 UTC, say: “Supported synchronous command and MCP tool hooks can set onFailure to block. A failed PreToolUse check then prevents the tool from running.”
Pull request #62831, merged 20 Nov 2026, fills in the rest. The default is continue, which keeps the old behaviour, and “a failed PostToolUse check withholds the result of a tool that already ran”. In the rust-v0.173.0 source, a PreToolUse command hook that exits with any code other than 0 or 2 is marked failed. Checks that passed behave as before. Codex does not load a hook with onFailure = "block" that is async or runs on SessionEnd, PreModelRequest or PostModelRequest, and warns “onFailure:block requires a synchronous hook that supports blocking”.
What does a Codex hook that guards secrets look like? #
Our example: this is the check_secrets.sh script that the config above points to. It reads the event JSON on stdin and blocks any Bash command that mentions a .env file:
#!/bin/bash
set -e
cmd=$(jq -r '.tool_input.command')
case "$cmd" in
*.env*)
echo "Blocked: this command touches a .env file." >&2
exit 2 ;;
esac
exit 0
For git status the script exits 0 and the command runs. For cat .env it exits 2, so Codex blocks the call with the stderr line as the reason. set -e makes the script exit with an error if jq is missing, and onFailure = "block" turns that failure into a block instead of a pass. To cover file edits too, widen the matcher to ^(Bash|apply_patch)$, since apply_patch also puts its patch in tool_input.command.
Our view: matching text in a command is easy to get around. The docs make a similar point: “Treat tool hooks as a useful guardrail, not a complete enforcement boundary”.
How do Codex hooks compare with Claude Code hooks? #
Both use events such as PreToolUse and Stop, three levels of event, matcher group and handler, and exit code 2 to block. The differences, from the Codex docs and the Claude Code hooks reference, both checked 2 Dec 2026:
| | Codex | Claude Code | |---|---|---| | Config | hooks.json or[hooks] inconfig.toml (docs ) | hooks insettings.json files (reference ) | | Events listed | 14 ( docs ) | 34 ( reference ) | | Handlers that run | command ,mcp_tool (docs ) | command ,http ,mcp_tool ,prompt ,agent (reference ) | | Block when a hook fails | onFailure , since rust-v0.173.0 (notes ) | onFailure , since v2.1.295; v2.1.328 formcp_tool (reference ) |
Our Claude Code hooks guide covers the other side, and how to install Codex CLI gets Codex ready.
Where does Munder Difflin fit? #
We think it fits if you run Codex next to Claude Code or Gemini CLI. Munder Difflin is free and open source: a desktop app that runs a team of AI agents for all kinds of knowledge work, coding included, on your own computer. Its README, read 2 Dec 2026, says of engines: “Each agent picks its own, so one office can mix them.” The install guide covers setup.
FAQ
What are Codex hooks?
Scripts or MCP tools that Codex runs at fixed points in a session, such as before a tool call or when a turn stops. OpenAI's hooks docs, checked 2 Dec 2026, list 14 events, from SessionStart to SessionEnd. Command and mcp_tool handlers run; prompt and agent handlers are parsed but skipped.
Where do I put Codex hooks?
In a hooks.json file, or inline under [hooks] in config.toml. The user file is $CODEX_HOME/hooks.json, which is ~/.codex/hooks.json on macOS and Linux when CODEX_HOME is unset. A trusted project uses .codex/hooks.json, and the system file on macOS and Linux is /etc/codex/hooks.json. A new or changed hook is skipped until you trust it in /hooks.
How do I block a command with a Codex PreToolUse hook?
Print JSON with hookSpecificOutput.permissionDecision set to "deny" and a permissionDecisionReason, or exit with code 2 and write the reason to stderr. Codex also accepts the older shape with decision set to "block" and a reason.
What does onFailure block do in Codex?
It makes a hook that fails stop the action instead of letting it go ahead. The rust-v0.173.0 release notes, published 2 Dec 2026, say supported synchronous command and MCP tool hooks can set it, and a failed PreToolUse check then prevents the tool from running. The default is continue. The hooks docs page did not mention onFailure when we checked it on 2 Dec 2026.
How are Codex hooks different from Claude Code hooks?
Both use events such as PreToolUse and Stop, the same three levels of event, matcher group and handler, and exit code 2 to block. Codex reads hooks.json or config.toml and runs command and mcp_tool handlers. Claude Code reads settings.json files and also runs http, prompt and agent handlers. Both now offer onFailure with a block value.
原文来源:munderdiffl.in