Original URL: https://getagenttools.com/blog/codex-hooks/

# Codex hooks: hooks.json, PreToolUse and what "Hook failed" means

30 Oct 2026Marek.

- codex
- coding-agents
- guardrails

Codex hooks are scripts Codex runs at fixed points of a session. They live in `hooks.json` or in `[hooks]` tables in `config.toml`. The one that can stop a tool call is `PreToolUse`, the same name Claude Code uses. On `codex-cli 0.163.0` hooks are on by default. `codex features list` shows `hooks stable true`.

 

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "/usr/bin/python3 \"$(git rev-parse --show-toplevel)/.codex/hooks/block_date.py\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}
```

  

- **Where.** `~/.codex/hooks.json` for you, `<repo>/.codex/hooks.json` for one project. Inline `[hooks]` in either `config.toml` works too.
- **Trust.** A new or changed hook is skipped until you trust it in `/hooks`. Project hooks also need a trusted project.
- **Blocking.** Exit 2 with a reason on stderr blocks. So does `permissionDecision: "deny"` in JSON.
- **No ask.** Codex parses `"ask"` but does not support it yet. The hook run fails and the call goes ahead.
- **My test.** A project hook blocked `date` inside `codex exec` on 25 October 2026. It took four runs to get it loaded.

 

How the Claude Code version works, with my PreToolUse guardrail, is in my [PreToolUse](https://getagenttools.com/blog/pretooluse-hook/) post. This one is the Codex side.

 

## Where does Codex read hooks.json?

 

Next to every active config layer. The [Codex hooks page](https://learn.chatgpt.com/docs/hooks) names four places as the most useful. They are `~/.codex/hooks.json`, `~/.codex/config.toml`, `<repo>/.codex/hooks.json` and `<repo>/.codex/config.toml`. Layers do not replace each other. Codex loads every matching hook from all of them.

 

Keep one format per layer. If a layer has both `hooks.json` and inline `[hooks]`, Codex merges them and warns at startup. The inline form uses arrays of tables, so the same hook reads like this:

 

```toml
[[hooks.PreToolUse]]
matcher = "^Bash$"

[[hooks.PreToolUse.hooks]]
type = "command"
command = '/usr/bin/python3 "$(git rev-parse --show-toplevel)/.codex/hooks/block_date.py"'
timeout = 10
```

 

The docs advise a path built from the git root. Hooks run in the session's working directory, and Codex can start in a subfolder.

 

The events cover a whole session, from `SessionStart` to `SessionEnd`. Only a few can block. `PreToolUse` is the one that stops a tool.

 

## Why is my Codex hook not running?

 

Usually trust. The docs say it plainly. "Non-managed hooks must be reviewed and trusted before they run." Codex stores trust against the hook's hash. Edit the hook and it is skipped again until you review it in `/hooks`.

 

A project hook has a second gate. It loads only when the project's `.codex/` layer is trusted. In an untrusted folder only user and system hooks load.

 

This is where my test went. I built a throwaway git repo with the hook above and a ten-line Python script. The script blocks any Bash command containing `date` with exit 2. Then I ran `codex exec -s read-only` and asked for `date`.

 

Run one: `date` ran, with no `hook: PreToolUse` line and no trust warning. Run two added `--dangerously-bypass-hook-trust`. Same result. Per the docs, the folder itself was still untrusted. Run three tried project trust as a dotted `-c` key. Still nothing. Run four passed the `projects` table inline with `-c`. The hook loaded. Output trimmed:

 

```text
hook: PreToolUse
ERROR codex_core::tools::router: error=Command blocked by PreToolUse hook: Blocked by test hook: date is not allowed here.. Command: date
hook: PreToolUse Blocked
```

 

The model reported the block. In normal use you trust the folder once and review the hook in `/hooks`. The flag is for automation that "already vets hook sources", per `codex exec --help`.

 

## How does the Codex PreToolUse hook block a command?

 

It reads one JSON object on stdin. My log showed what Codex sent for `date`: `tool_name` `"Bash"`, `tool_input.command` `"date"` and `permission_mode` `"bypassPermissions"`. That last value surprised me. The run header said approval `never` and sandbox `read-only`. In the source, `hook_runtime.rs` maps approval `never` to `bypassPermissions`. The field tracks approvals, not the sandbox.

 

Three ways to say no. Exit 2 with the reason on stderr. Or exit 0 with this:

 

```json
{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "deny",
    "permissionDecisionReason": "Destructive command blocked by hook."
  }
}
```

 

The older `{"decision": "block", "reason": "..."}` still works. A hook can also rewrite a call with `updatedInput`. That only works next to `permissionDecision: "allow"`.

 

Shell commands match as `Bash`. `apply_patch` matches as `Edit` or `Write` too. MCP tools match by name. Hosted tools such as web search never reach the hook. The docs call tool hooks "a useful guardrail, not a complete enforcement boundary". I agree.

 

## Why does a Codex PreToolUse hook fail?

 

Codex does not print the exact words "PreToolUse hook failed". The TUI shows `Hook failed`. `codex exec` prints `hook: PreToolUse Failed`. Either way, your hook ran and Codex could not use its answer. The tool call usually went ahead anyway. I took both strings from the source, not from a failed run of mine.

 

In `pre_tool_use.rs` at tag `rust-v0.163.0`, a run counts as failed when:

 

- the exit code is anything but 0 or 2, shown as `hook exited with code N`
- exit 2 comes with an empty stderr
- stdout looks like JSON but does not parse
- the JSON uses a field Codex does not support yet, like `"ask"`

 

The case that worries me most is the quiet one. In the source a timeout ends as `hook timed out after Ns`. A hook that cannot start gets a spawn error. Both mark the run Failed, never Blocked. Failure is open. A Python traceback exits 1. Per the source, the command runs.

 

## Codex hooks vs Claude Code hooks

 

|  | Codex CLI 0.163.0 | Claude Code |
| --- | --- | --- |
| Config | `hooks.json` or `[hooks]` in `config.toml` | `hooks` in settings files |
| `permissionDecision` | `deny`, or `allow` with `updatedInput` | `allow`, `deny`, `ask`, `defer` |
| Exit 2 | Blocks, stderr is the reason | Blocks, stderr is the reason |
| Hook crashes | Hook failed, call proceeds | Non-blocking error, unless `onFailure: "block"` |
| Default timeout | 600 seconds for most hooks | 600 seconds for command hooks |

 

The field names match. A deny script written for Claude Code mostly ports. The gaps are `ask` and a fail-closed switch. The [Claude Code hooks reference](https://code.claude.com/docs/en/hooks) has `onFailure: "block"`. I found no such setting on the Codex hooks page or in the hooks crate.

 

## Frequently asked questions

 

### Are Codex hooks enabled by default?

 

Yes. To turn them off, set `hooks = false` under `[features]` in `config.toml`. The old key `codex_hooks` still works as a deprecated alias.

 

### What happened to plugin\_hooks?

 

On 0.163.0 `codex features list` shows `plugin_hooks` as `removed`. That is the feature toggle. The docs still describe hooks bundled in plugins, under the same trust review. I have not tested a plugin hook.

 

### Can a Codex hook ask me before a command runs?

 

Not yet. `"ask"` is parsed but unsupported. The run is marked failed and the call continues. For a prompt, look at `PermissionRequest`, which can allow or deny an approval Codex was about to show.

 

### Where is the Codex hooks documentation?

 

On the [hooks page](https://learn.chatgpt.com/docs/hooks) of the Codex docs. The page calls itself the release behavior reference.

 

## The verdict

 

I use Codex every day as a worker next to Claude Code. A hook in Codex is real enforcement for Bash, patches and MCP calls, as long as it runs. Trust is the part that bit me. Three silent runs came before my project hook fired. Write the deny path with exit 2. Test it with a canary command. Assume a crash lets the command through. The setup that keeps commands contained is in my [Codex permissions post](https://getagenttools.com/blog/codex-dangerously-skip-permissions/). For per-project instructions, see [Codex skills](https://getagenttools.com/blog/codex-skills/).

 

Sources, read on 25 October 2026: the Codex [hooks page](https://learn.chatgpt.com/docs/hooks), the Claude Code [hooks reference](https://code.claude.com/docs/en/hooks), local `codex features list` and `codex exec --help` for `codex-cli 0.163.0`. Code references are from [openai/codex](https://github.com/openai/codex) at tag `rust-v0.163.0`, mainly `codex-rs/hooks/src/events/pre_tool_use.rs`. The four test runs are my own.

Put this into practice

[See the tool list](https://getagenttools.com/tools/)[Train your team](https://getagenttools.com/#training)
