Yes, Codex supports hooks: Codex hooks are commands that Codex runs at fixed points in a session, such as before a tool call (PreToolUse) or when a turn ends (Stop). They are on by default, live in a hooks.json file or a [hooks] table in config.toml, and a new or changed hook must be reviewed and trusted in /hooks before it runs (OpenAI Codex hooks docs).
Applies to: Codex CLI 0.166.0 (release of October 29, 2026) and OpenAI’s Codex hooks documentation. Last checked: November 2, 2026.
The smallest useful setup is a user-level ~/.codex/hooks.json. This PreToolUse hook from the official docs runs a policy script before every shell command:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "/usr/bin/python3 \"$(git rev-parse --show-toplevel)/.codex/hooks/pre_tool_use_policy.py\"",
"statusMessage": "Checking Bash command"
}
]
}
]
}
}
OpenAI recommends resolving repo-local scripts from the git root, as above, because Codex may be started from a subdirectory (hooks docs).
On this page
What the docs say vs what the 0.166.0 source contains
OpenAI’s hooks page says: “Use this page as the release behavior reference”, and warns that the main branch schemas “may include hook fields that are not in the current release” (hooks docs). Checked against the code at the rust-v0.166.0 tag on November 2, 2026, five points differ:
| Topic | Official hooks page | Source at rust-v0.166.0 |
|---|---|---|
| Events | 11 events, listed in the table below | 14 names in HOOK_EVENT_NAMES: adds PreModelRequest, PostModelRequest and Interrupt (hooks/src/lib.rs) |
async | “parsed, but asynchronous command hooks aren’t supported yet” | The flag is read, and turned off for SessionEnd (discovery.rs) |
| Handler types | Only type: "command" handlers “run today”; prompt and agent are parsed but skipped | Also defines an mcp_tool handler, rejected on SessionEnd; prompt and agent are skipped with “not supported yet” (discovery.rs) |
SessionStart sources | startup, resume, clear, compact | Also fork (session_start.rs) |
| Short timeouts | SessionEnd: 1 second by default, up to 3 | SessionEnd and Interrupt both get 1 second by default, capped at 3 (discovery.rs) |
Build against the documented column. Everything in the right-hand column is undocumented and can change without notice.
PreModelRequest and PostModelRequest
These two event names are in the 0.166.0 source but not on the hooks page. The source file opens with “Observation-only lifecycle hooks. Output is logged, never interpreted as control flow”, so they cannot block or steer a model request. Their input carries session_id, turn_id, cwd, transcript_path, model and, inside a subagent, agent_id and agent_type. PostModelRequest adds status (completed, failed or cancelled), error and token_usage (model_request.rs). They take no matcher.
Which hook events does Codex document?
| Event | When it runs | matcher filters | What a hook can do |
|---|---|---|---|
SessionStart | A session starts | source: startup, resume, clear, compact | Add developer context |
SubagentStart | A subagent starts | agent_type | Add context for the subagent |
UserPromptSubmit | You submit a prompt | Not supported | Block the prompt, add context |
PreToolUse | Before a supported tool runs | Tool name | Deny, rewrite the input, add context |
PermissionRequest | Codex is about to ask for approval | Tool name | Allow, deny, or leave it to the normal prompt |
PostToolUse | After a supported tool produces output | Tool name | Replace the result with feedback, add context |
PreCompact / PostCompact | Before / after compaction | manual or auto | Stop with continue: false |
SubagentStop | A subagent stops | agent_type | Ask for another pass |
Stop | A turn ends | Not supported | Ask Codex to keep working |
SessionEnd | The main thread ends (not subagents) | reason, currently only other | Side effects only |
All rows come from the hooks docs. Two runtime rules matter when several hooks match: matching hooks from multiple files all run, and “multiple matching command hooks for the same event are launched concurrently, so one hook can’t prevent another matching hook from starting.”
Where do Codex hooks go?
Codex discovers hooks next to every active config layer, as hooks.json or as inline [hooks] tables in config.toml. Higher layers do not replace lower ones: all matching hooks load. If one layer has both forms, Codex merges them and warns at startup (hooks docs).
| Layer | hooks.json | config.toml |
|---|---|---|
| User | ~/.codex/hooks.json | [hooks] in ~/.codex/config.toml |
| Project | <repo>/.codex/hooks.json | [hooks] in <repo>/.codex/config.toml |
| Managed | — | [hooks] in requirements.toml, with managed_dir / windows_managed_dir |
| Plugin | hooks/hooks.json in the plugin root, or the hooks entry of .codex-plugin/plugin.json | — |
Project hooks load only when the project’s .codex/ layer is trusted; in an untrusted repository Codex still loads user and system hooks. Plugin hook commands get PLUGIN_ROOT and PLUGIN_DATA, plus CLAUDE_PLUGIN_ROOT and CLAUDE_PLUGIN_DATA for compatibility (hooks docs).
The same PreToolUse hook as inline TOML, from the docs:
[[hooks.PreToolUse]]
matcher = "^Bash$"
[[hooks.PreToolUse.hooks]]
type = "command"
command = '/usr/bin/python3 "$(git rev-parse --show-toplevel)/.codex/hooks/pre_tool_use_policy.py"'
timeout = 30
statusMessage = "Checking Bash command"
In hooks.json, events must sit inside the top-level "hooks" object; the file may also carry an optional "description", which does not change which hooks run.
Handler keys
| Key | Meaning |
|---|---|
type | command is the only type that runs |
command | The shell command; it runs with the session cwd as its working directory |
commandWindows | Windows-only override; command_windows also works in TOML |
timeout | Seconds; 600 if omitted, except SessionEnd (1 by default, up to 3) |
statusMessage | Optional text shown while the hook runs |
Source: hooks docs. To turn hooks off, set hooks = false under [features] in config.toml; codex_hooks still works as a deprecated alias.
How do matchers and tool names work?
A matcher is a regex string; "*", "" or no matcher matches everything. In the source, a value made only of ASCII letters, digits, _ and | is read as exact names, so Bash does not match BashOutput (matcher.rs).
| Tool path | Matched as | tool_name on stdin |
|---|---|---|
| Shell commands and unified exec | Bash | Bash |
| File edits | apply_patch, Edit or Write | apply_patch |
| MCP tools | e.g. mcp__filesystem__read_file | The MCP name |
| Other local function tools | The function name, e.g. update_plan; spawn_agent also matches Agent | The function name |
Hosted tools such as WebSearch | Not hooked | — |
From the hooks docs. The alias only selects the hook: a script that checks tool_name == "Write" never matches, because stdin keeps apply_patch. OpenAI’s own caveat: “Treat tool hooks as a useful guardrail, not a complete enforcement boundary.”
What JSON does a hook receive and return?
Every command hook gets one JSON object on stdin with session_id, transcript_path, cwd, hook_event_name and model. Turn-scoped events add turn_id, and most events add permission_mode (default, acceptEdits, plan, dontAsk or bypassPermissions). The docs warn that the transcript format “isn’t a stable interface for hooks” (hooks docs).
| Exit / output | Result |
|---|---|
0, no output | Success; Codex continues normally |
0, plain text | Added as developer context on SessionStart, SubagentStart and UserPromptSubmit; ignored on tool and compaction events; invalid on Stop and SubagentStop |
0, JSON | Parsed as the hook’s decision (shapes below) |
2, reason on stderr | Blocks the tool call or prompt, gives PostToolUse feedback, or becomes the continuation prompt on Stop / SubagentStop |
Deny a tool call from PreToolUse:
{
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": "Destructive command blocked by hook."
}
}
Rewrite it instead with permissionDecision: "allow" plus updatedInput; for Bash and apply_patch, updatedInput must include a string command. On PreToolUse, permissionDecision: "ask", the legacy decision: "approve", continue: false, stopReason and suppressOutput are parsed but not supported: Codex marks the hook run as failed and continues the tool call. A PermissionRequest hook returns decision.behavior allow or deny; if several hooks decide, any deny wins (hooks docs).
Additional context is capped at roughly 2,500 tokens per message; longer text is saved under <temp_dir>/hook_outputs/<session_id>/ and the model gets a preview with the path, so the docs advise against returning secrets in hook output.
A Stop hook that keeps Codex working
The Stop hook answers the “run the tests once more before you finish” case. The docs register it like this:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "/usr/bin/python3 \"$(git rev-parse --show-toplevel)/.codex/hooks/stop_continue.py\"",
"timeout": 30
}
]
}
]
}
}
The script receives turn_id, stop_hook_active (“whether this turn was already continued by Stop”) and last_assistant_message. To continue, it prints:
{
"decision": "block",
"reason": "Run one more pass over the failing tests."
}
This does not reject the turn: Codex creates a new continuation prompt with reason as its text. Three rules from the docs shape the script (hooks docs):
- Exit
0with JSON, or no output at all. Plain text onStopis invalid. - Read
stop_hook_activefirst. If it is alreadytrue, exit0without output unless you really want another pass; otherwise every pass can trigger the next one. - If any matching
Stophook returnscontinue: false, that wins over continuation requests from otherStophooks.
Why a hook needs review before it runs
Codex records trust against each hook’s current hash, so new or changed non-managed hooks are skipped until you trust them in /hooks; at startup it prints a warning that tells you to open /hooks. Managed hooks from system, MDM, cloud or requirements.toml sources are trusted by policy and cannot be disabled from the user browser (hooks docs).
For CI, the docs name a flag: “For one-off automation that already vets hook sources outside Codex, pass --dangerously-bypass-hook-trust to run enabled hooks without requiring persisted hook trust for that invocation.” It runs every enabled hook, including any a repository adds, without review. The documented alternatives are to trust the hooks once through /hooks, or, for a team, to ship them as managed hooks in requirements.toml, where allow_managed_hooks_only = true skips user, project, session and plugin hooks. For non-interactive runs in general, see codex exec, and for the other dangerously flag, Codex sandbox and approval modes.
If you also use Claude Code, the Claude Code hooks guide covers its version of the same file format; for Codex setup itself, start with what Codex is.





