Codex hooks are commands (or MCP tool calls) that the Codex CLI runs automatically at fixed points in a session, such as before a tool runs, when an approval is about to be requested, or when a turn ends. You define them in ~/.codex/hooks.json or inline under [hooks] in ~/.codex/config.toml. Each hook receives JSON about the event on stdin and can reply with JSON on stdout, which lets it log, notify, block an action, approve a request or keep Codex going. Before a hook you added can run, you have to trust it once with /hooks.
01Where hooks live
Codex looks for hooks in several places and runs matching hooks from all of them:
~/.codex/hooks.json (%USERPROFILE%\.codex\hooks.json on Windows, or $CODEX_HOME/hooks.json).codex/hooks.json in the repository, loaded only for trusted projects/etc/codex/hooks.json on macOS and Linux, %ProgramData%\OpenAI\Codex\hooks.json on Windows[hooks] tables in any of the matching config.toml filesHooks are on by default. To turn them all off, set hooks = false under [features] in config.toml.
If the same command is registered in two places, it can run twice. Pick one place per hook, and prefer one format (JSON or TOML) per folder: Codex warns at startup when a folder declares hooks both ways.
02Trust: why a new hook doesn't run
Codex does not run a hook just because it is in a file. Each hook you add must be reviewed and trusted first. Codex stores a hash of the exact hook definition, so a new hook, or one whose command or timeout you changed, is skipped until you trust it again.
When hooks need review, Codex prints a warning at startup. Type /hooks to see every hook source, review new or changed entries, trust them, or disable one. Hooks installed by an administrator (system, MDM or requirements.toml) are trusted by policy.
For automation that already vets its hooks, codex --dangerously-bypass-hook-trust runs enabled hooks without stored trust for that one run. As the name says, keep it out of everyday use.
03The shape of a hook
A hook has three levels: the event, a matcher group, and one or more handlers.
{
"hooks": {
"PermissionRequest": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "python3 ~/.codex/hooks/approval.py",
"timeout": 30,
"statusMessage": "Checking approval request"
}
]
}
]
}
}
matcheris a regular expression. For tool events it matches the tool name, such asBash,apply_patch(also matched byEditorWrite) or an MCP tool likemcp__github__create_issue. Leave it out, or use"*", to match everything.StopandUserPromptSubmitignore it.timeoutis in seconds. The default is 600.SessionEndandInterrupthooks default to 1 second and allow at most 3.async: trueruns the command in the background so Codex doesn't wait for it. A background hook cannot block or approve anything.commandWindowsgives a different command on Windows.- Commands run with the session's working folder as their current directory. Several matching hooks for one event run at the same time.
The same thing in config.toml:
[[hooks.PermissionRequest]]
matcher = "Bash"
[[hooks.PermissionRequest.hooks]]
type = "command"
command = "python3 ~/.codex/hooks/approval.py"
timeout = 30
04Input and output
Every hook gets one JSON object on stdin. All events include session_id, cwd, hook_event_name, model and transcript_path; events inside a turn add turn_id, and tool events add tool_name and tool_input. Bash commands and patches put their text in tool_input.command.
To reply, a hook exits 0 and prints JSON. Exit 0 with no output means "carry on". The useful replies:
PreToolUsecan deny a tool call withpermissionDecision: "deny"and a reason, or rewrite it withupdatedInput.PermissionRequestcan returnallowordenyinstead of you.Stopcan return{"decision": "block", "reason": "..."}to make Codex continue, using the reason as the next prompt.UserPromptSubmitcan block a prompt.- Most events accept
systemMessage, shown as a warning in the terminal.
Exit code 2 with a reason on stderr also blocks on the events that support blocking. Watch the stdout rules: Stop treats plain text as invalid, PreToolUse and PermissionRequest ignore plain text, and SessionStart and UserPromptSubmit add plain text to the model's context. A notification hook should print nothing at all.
05Every event and what it is for
SessionStartA session starts, resumes, is cleared, compacted or forkedLoad context, start a logUserPromptSubmitYou send a promptCheck or extend the promptPreToolUseBefore a shell command, patch, MCP call or other local toolBlock risky commands, watch progressPermissionRequestCodex is about to ask for approvalApproval alerts, policy, remote approvalPostToolUseAfter a tool produced outputReview output, run formatters or checksPreCompact, PostCompactAround compaction of the chatSave notes before context shrinksSubagentStart, SubagentStopA subagent starts or stopsTrack parallel agentsStopA turn ends"Finished" alerts, final checks, continue automaticallyInterruptYou interrupt an active turnRecord the interruption, clean upPreModelRequest, PostModelRequestAround each model requestLogging onlySessionEndThe session endsShort clean-up (3 seconds at most)There is no Notification event in Codex. For "Codex needs me", use PermissionRequest (approvals) and a PreToolUse hook on the request_user_input tool (questions).
06Hooks for notifications
The two events most people start with are:
Stop, for a "Codex has finished" alert. See the Stop hook guide.PermissionRequest, for "Codex wants approval", and for answering that approval from somewhere else. See the PermissionRequest guide.
If all you need is "finished", the older notify setting is simpler and needs no trust step; see the notify guide. Ready-made setups for each system are in how to get a notification when Codex finishes.
Notifier for Codex is built on exactly these hooks. Installing codex-notifier adds six entries (SessionStart, UserPromptSubmit, PreToolUse, PermissionRequest, Stop, SessionEnd) to ~/.codex/hooks.json and writes the same trust hashes Codex would, so they run on the next session without a visit to /hooks. codex-notifier doctor reports a missing or stale trust entry, and codex-notifier uninstall removes exactly what it added. It works on Mac, Windows and Linux and sends the moments that need you to your phone.
07FAQ
Should I use hooks.json or config.toml?
Either works and they behave the same. hooks.json is easier for tools to edit and is what most examples use. Inline TOML keeps everything in one file. Don't declare hooks both ways in the same folder.
Why does Codex keep asking me to review my hook?
Trust is tied to the exact definition. Any change to the command, matcher or timeout makes a new hash, so Codex asks again. That is deliberate: a hook can run any command on your machine.
Do project hooks run in every repository?
Only in projects you have trusted. Codex ignores a project's .codex/ folder, including its hooks, while the project is untrusted. Your user hooks in ~/.codex/hooks.json still run.
Can a hook approve a command for me?
Yes. A PermissionRequest hook can return allow or deny. If several hooks answer, any deny wins. If none answers, Codex shows its normal prompt.
Do hooks run in codex exec?
Yes, hooks are part of Codex itself, so they run in non-interactive codex exec runs too.