Codex Hooks: How to Block a Tool Call Before It Runs OpenAI's Codex hooks framework, documented as of 2 Dec 2026, runs scripts or MCP tools at 14 session events, and since the rust-v0.173.0 release on 2 Dec 2026 a hook with onFailure set to block stops a tool call when its PreToolUse check itself fails. Hooks are enabled by default, only command and mcp_tool handlers execute while prompt and agent handlers are parsed but skipped, and new or changed hooks must be reviewed and trusted in /hooks before they run unless an admin policy manages them. The change matters to developers running Codex agents because a failing guard now fails closed rather than letting the tool call proceed. 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 https://github.com/openai/codex/releases/tag/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? what-are-codex-hooks A Codex hook is a rule that says “when this event fires, run this handler”. OpenAI’s hooks docs https://learn.chatgpt.com/docs/hooks 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 https://munderdiffl.in/blog/what-is-codex/ . 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? 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 https://learn.chatgpt.com/docs/hooks | | SubagentStart | A subagent starts | Add context; can’t stop it | Docs https://learn.chatgpt.com/docs/hooks | | UserPromptSubmit | Before your prompt is sent | Add context or block the prompt | Docs https://learn.chatgpt.com/docs/hooks | | PreToolUse | Before a tool call | Deny it, rewrite its input, add context | Docs https://learn.chatgpt.com/docs/hooks | | PermissionRequest | Codex is about to ask for approval | Allow or deny | Docs https://learn.chatgpt.com/docs/hooks | | PostToolUse | After a tool produces output | Replace the result with feedback; can’t undo it | Docs https://learn.chatgpt.com/docs/hooks | | PreCompact , PostCompact | Before and after compaction | Stop with continue: false | Docs https://learn.chatgpt.com/docs/hooks | | PreModelRequest , PostModelRequest | Each model request attempt | Log only | Docs https://learn.chatgpt.com/docs/hooks | | SubagentStop | A subagent finishes | Keep the subagent going | Docs https://learn.chatgpt.com/docs/hooks | | Stop | A turn stops | Keep Codex going with your reason as a new prompt | Docs https://learn.chatgpt.com/docs/hooks | | Interrupt | You interrupt a turn | Record it; can’t prevent it | Docs https://learn.chatgpt.com/docs/hooks | | SessionEnd | The main thread ends | Advisory only | Docs https://learn.chatgpt.com/docs/hooks | Where do Codex hooks go? 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.json on macOS and Linux, %ProgramData%\OpenAI\Codex\hooks.json on Windows - User: $CODEX HOME/hooks.json , which is ~/.codex/hooks.json on macOS and Linux when CODEX HOME is 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? 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 https://github.com/openai/codex/releases/tag/rust-v0.173.0 , 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 https://github.com/openai/codex/pull/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 https://github.com/openai/codex/blob/rust-v0.173.0/codex-rs/hooks/src/events/pre tool use.rs , 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? 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: bash /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? 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 https://code.claude.com/docs/en/hooks , both checked 2 Dec 2026: | | Codex | Claude Code | |---|---|---| | Config | hooks.json or hooks in config.toml docs https://learn.chatgpt.com/docs/hooks | hooks in settings.json files reference https://code.claude.com/docs/en/hooks | | Events listed | 14 docs https://learn.chatgpt.com/docs/hooks | 34 reference https://code.claude.com/docs/en/hooks | | Handlers that run | command , mcp tool docs https://learn.chatgpt.com/docs/hooks | command , http , mcp tool , prompt , agent reference https://code.claude.com/docs/en/hooks | | Block when a hook fails | onFailure , since rust-v0.173.0 notes https://github.com/openai/codex/releases/tag/rust-v0.173.0 | onFailure , since v2.1.295; v2.1.328 for mcp tool reference https://code.claude.com/docs/en/hooks | Our Claude Code hooks guide https://munderdiffl.in/blog/claude-code-hooks-explained/ covers the other side, and how to install Codex CLI https://munderdiffl.in/blog/how-to-install-codex-cli/ gets Codex ready. Where does Munder Difflin fit? where-does-munder-difflin-fit We think it fits if you run Codex next to Claude Code or Gemini CLI. Munder Difflin https://harnessmd.com/download 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 https://github.com/HarnessMD/munder-difflin , read 2 Dec 2026, says of engines: “Each agent picks its own, so one office can mix them.” The install guide https://munderdiffl.in/blog/how-to-install-and-use-munder-difflin/ 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.