cd /news/ai-agents/codex-hooks-how-to-block-a-tool-call… · home › topics › ai-agents › article
[ARTICLE · art-180430] src=munderdiffl.in ↗ pub= topic=ai-agents verified=true sentiment=· neutral

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.

key takeaway

Set onFailure to block on PreToolUse hooks in Codex rust-v0.173.0 or later so a failing guard denies the tool call instead of letting it run, and trust new or changed hooks in /hooks before they execute.

by read8 min views1 publishedDec 2, 2026
Codex Hooks: How to Block a Tool Call Before It Runs
Image: Munderdiffl (auto-discovered)

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.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 whenCODEX_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? # #

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.

── more in #ai-agents 4 stories · sorted by recency
── more on @openai 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/codex-hooks-how-to-b…] indexed:0 read:8min 2026-12-02 · —