CODEX

Codex hooks, a practical guide to hooks.json, events and trust

How Codex hooks work, where hooks.json lives, every event name, the trust step, input and output format, working examples and the current limits.

Short answer

Codex hooks are commands (or MCP tool calls) that Codex runs at fixed points of a session, such as PreToolUse, PermissionRequest, PostToolUse and Stop. You declare them in ~/.codex/hooks.json, a trusted project's .codex/hooks.json or a [hooks] table in config.toml, and Codex only runs a non-managed hook after you review and trust it with /hooks.

Codex hooks let you run your own code at specific moments of a Codex session: before a tool runs, when Codex needs a permission decision, after a tool returns, when you submit a prompt, when the agent stops. Codex runs them itself, so they happen every time, not when the model remembers to.

This guide covers where hooks are configured, the full list of events, how input and output work, the trust step that every non-managed hook goes through, three examples, and the limits the official documentation spells out. If you already know Claude Code hooks, the shape will feel familiar, but the details differ.

Are Codex hooks still experimental?

Codex hooks are enabled by default in current versions; the official hooks page does not mark them experimental. The feature flag is features.hooks, and you can turn hooks off with:

[features]
hooks = false

The older name features.codex_hooks still works as a deprecated alias. Administrators can force hooks off with [features].hooks = false in requirements.toml. If you read an older tutorial that tells you to enable codex_hooks, that step is no longer needed.

Where do Codex hooks live?

Codex loads hooks from several layers and merges them. Each layer can use a hooks.json file or an inline [hooks] table in its config.toml:

ScopeLocation
System (macOS and Linux)/etc/codex/hooks.json
User$CODEX_HOME/hooks.json, by default ~/.codex/hooks.json
Trusted project.codex/hooks.json in the project
Pluginhooks/hooks.json in the plugin root, or a manifest hooks entry
Managed[hooks] in requirements.toml

Project hooks only load from trusted projects. If one layer declares hooks in both hooks.json and config.toml, Codex loads both and prints a warning at startup, so pick one format per layer.

What hook events does Codex support?

Codex currently documents fourteen events:

EventWhen it fires
SessionStartA session starts, resumes, is cleared, compacted or forked
SessionEndThe session ends
UserPromptSubmitYou submit a prompt
PreToolUseBefore a tool call runs
PermissionRequestCodex needs a permission decision for a tool call
PostToolUseAfter a tool call returns
PreModelRequestBefore a request goes to the model
PostModelRequestAfter the model responds
PreCompact and PostCompactAround context compaction
SubagentStart and SubagentStopWhen a subagent starts or stops
StopThe agent finishes its turn
InterruptYou interrupt the agent

There is no separate Notification event as in Claude Code. For "tell me when Codex is done or waiting", Codex has the notify setting and built-in TUI notifications, covered in the Codex notifications guide. Hooks on Stop and PermissionRequest are the other route.

What does a hooks.json file look like?

A hook has three levels: an event, a matcher group, and one or more handlers. This user-level file runs a policy script before every shell command:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "python3 ~/.codex/hooks/check_bash.py",
            "statusMessage": "Checking shell command",
            "timeout": 30
          }
        ]
      }
    ]
  }
}

Handler fields include type, command, commandWindows, timeout (in seconds), statusMessage, async and additionalContextLimit. Two handler types run today: command and mcp_tool (which also takes server, tool and input). prompt and agent handlers are parsed but skipped, so do not rely on them.

The matcher is a regular expression. "*", an empty string, or no matcher at all matches everything. Tool events (PreToolUse, PermissionRequest, PostToolUse) match on the tool name; SessionStart matches on its source (startup, resume, clear, compact, fork); PreCompact and PostCompact on manual or auto; subagent events on the agent type. UserPromptSubmit, Stop and Interrupt ignore the matcher.

What input does a Codex hook receive?

A command hook receives the event as JSON on stdin. Every event includes session_id, transcript_path, cwd, hook_event_name and model. Turn-scoped events add turn_id, most tool and session events add permission_mode, and tool events add tool_name, tool_use_id and tool_input. PostToolUse also gets tool_response.

How does a Codex hook allow or block something?

A hook answers with its exit code and, optionally, JSON on stdout. Exit 0 means success, and Codex parses stdout as JSON if it is JSON. Exit 2 blocks, with the reason read from stderr.

For PreToolUse, the JSON form looks like this:

{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "deny",
    "permissionDecisionReason": "Force pushes are not allowed from this machine."
  }
}

PreToolUse can deny, or return "allow" together with updatedInput to rewrite the call. "ask" is not supported there. PermissionRequest returns hookSpecificOutput.decision.behavior set to "allow" or "deny"; any deny wins, and with no decision the normal prompt continues. PostToolUse can return "decision": "block" to replace the tool result with feedback, but it cannot undo a command that already ran. Stop can block with a reason, which becomes a new prompt and keeps the agent working.

What is the Codex hook trust step?

Codex does not run a non-managed hook until you have reviewed and trusted it. Trust is tied to a hash of the hook, so if the command changes, Codex skips it until you trust it again. You manage this with the /hooks command in the CLI, which lets you inspect hooks, trust new or changed ones, and disable non-managed ones.

This is a real protection. A repository you clone could ship a .codex/hooks.json that runs a script on every tool call; without trust, it does nothing. For automation that already vets its hook sources, --dangerously-bypass-hook-trust skips persisted trust for one invocation. Managed hooks from requirements.toml are trusted by policy and users cannot disable them.

Example: block force pushes before they run

A PreToolUse hook can stop a class of commands outright. Save this as ~/.codex/hooks/check_bash.py and point the earlier hooks.json at it:

import json
import sys

event = json.load(sys.stdin)
command = json.dumps(event.get("tool_input", {}))

if "push" in command and ("--force" in command or " -f" in command):
    print("Force pushes are blocked by a local hook.", file=sys.stderr)
    sys.exit(2)

sys.exit(0)

Exit 2 blocks the call and Codex reads the reason from stderr. A deny like this is a guardrail on top of your sandbox and approval policy, not a replacement for them.

Example: log every command in the background

A background hook can record activity without slowing the agent:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "*",
        "hooks": [
          {
            "type": "command",
            "command": "python3 ~/.codex/hooks/log_tool.py",
            "async": true,
            "timeout": 120
          }
        ]
      }
    ]
  }
}

With "async": true the hook cannot block or modify the operation. Up to eight background hooks run at once per session.

Example: get a macOS notification when Codex stops

A Stop hook can show a notification with the built-in osascript command:

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e 'display notification \"Codex finished its turn\" with title \"Codex\"'"
          }
        ]
      }
    ]
  }
}

The hook prints nothing on stdout, which matters: plain text on stdout is invalid for Stop.

What are the limits of Codex hooks?

The official docs are direct about the limits:

  • PreToolUse covers shell, unified exec, apply_patch, MCP tools and other local function tools, but not hosted tools such as web search. write_stdin does not re-run it, and some specialized paths can opt out. The docs call tool hooks "a useful guardrail, not a complete enforcement boundary".
  • Matching command hooks for the same event run concurrently, so one cannot stop another from starting.
  • The default timeout is 600 seconds. SessionEnd and Interrupt default to 1 second, with a maximum of 3.
  • Output over about 2,500 tokens is saved to disk and the model sees a preview.
  • Interrupt and SessionEnd do not run for subagents.

For security, combine hooks with the sandbox rather than trusting hooks alone. The coding agent security checklist lists the other layers worth having.

Answering Codex permission requests through a hook

Allowbar is a macOS menu bar app built on exactly this mechanism. It adds its own entry to ~/.codex/hooks.json, leaves your other hooks untouched, and goes through the same trust step as any hook: Codex asks you to trust it the first time, and Allowbar never skips that. Hook events go to a small local server on your Mac; nothing is sent to a cloud service.

When Codex asks for a permission, a panel slides down from the top of the screen with the project, the tool and the exact command, and you answer Allow, Block, or Terminal. If you do not answer before the countdown ring runs out, the question goes back to the terminal. Allowbar never approves on its own and has no rules that approve for you; a missing answer is never treated as approval. It also lists every Codex and Claude Code session with its state and keeps an activity log on your Mac.

Allowbar is Mac only and supports Claude Code and Codex. It is in private beta: join the waitlist, the launch price is $9.99 instead of $29.90.

Sources

Frequently asked questions

Where is the Codex hooks.json file?

The user-level file is ~/.codex/hooks.json (or $CODEX_HOME/hooks.json). A trusted project can have its own .codex/hooks.json, and system-wide hooks live in /etc/codex/hooks.json on macOS and Linux. Each layer can also use an inline [hooks] table in config.toml.

Why is my Codex hook not running?

The most common reason is trust: non-managed hooks only run after you trust them with /hooks, and editing a hook changes its hash, so it needs trusting again. Also check that the project is trusted (project hooks are skipped otherwise) and that features.hooks has not been set to false.

Can a Codex hook approve a command automatically?

A PermissionRequest hook can return "allow", and a PreToolUse hook can return "allow" with updatedInput. Doing so takes the human out of that decision, so keep it to narrow, well-understood cases. Deny decisions win over allows when several hooks answer.

What is the difference between Codex hooks and Claude Code hooks?

Both use a similar event, matcher and handler structure, and share names like PreToolUse, PermissionRequest and Stop. Codex has fewer events, supports command and mcp_tool handlers only, stores hooks in hooks.json or config.toml, and requires an explicit hash-based trust step. Claude Code keeps hooks in settings.json and also supports http, prompt and agent handlers.

Do Codex hooks see web searches?

No. PreToolUse does not cover hosted tools such as web search. It covers shell commands, apply_patch, MCP tools and other local function tools.

ALLOWBAR FOR MAC

Answer your agents from the menu bar.

Permission requests from Claude Code and Codex slide down from the top of your screen. Allow, Block or send back to the terminal. Allowbar never approves anything on its own.

Launch price $9.99 instead of $29.90, one-time purchase, lifetime license.