Hooks in Claude Code and Codex: run your own commands when the agent acts
What hooks are in Claude Code and Codex, where they go, when they fire, and examples: a notification, formatting after edits, blocking edits to protected files.


The short answer
A hook is a command you register for a moment in the agent's work: before it runs a tool, after it edits a file, when it needs you, when it finishes. The agent runs it every time that moment comes, so the result doesn't depend on the model choosing to do it. People use hooks to format code after edits, block risky commands, send a notification, or add context at the start of a session.
| Claude Code | Codex | |
|---|---|---|
| Where they go | The hooks key of a settings.json | hooks.json, or [hooks] in config.toml |
| For one project | .claude/settings.json | .codex/hooks.json (trusted projects only) |
| Browse them | /hooks | /hooks |
| Before a hook runs | Runs once configured | You review and trust each hook first |
| Block an action | Exit code 2, reason on stderr | Exit code 2, or a deny decision in JSON |
Claude Code hooks
{
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'" }
]
}
],
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\"" }
]
}
]
}
}- Each event name is a key inside one
hooksobject;matchernarrows it, for example to the Edit and Write tools. If the file already hashooks, add the event next to the others instead of replacing the object. - Put it in the settings.json in the .claude folder in your home folder for every project, in the project's .claude/settings.json to share with the team, or in .claude/settings.local.json for yourself in one project. Plugins, skills and subagents can carry hooks too.
- Type
/hooksto see every configured hook by event. Claude Code picks up edits to the files while it runs. You can also describe the hook you want and ask Claude to write it. - To turn them all off, set
"disableAllHooks": true. Hooks from managed settings still run.
| Event | When it fires |
|---|---|
| SessionStart | A session begins or resumes |
| UserPromptSubmit | You send a prompt, before Claude reads it |
| PreToolUse | Before a tool call runs; can block it |
| PermissionRequest | A tool call needs a permission decision |
| PostToolUse | After a tool call succeeds |
| Notification | Claude Code sends a notification, such as waiting for you |
| Stop | Claude finishes responding |
Block edits to protected files
#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
for pattern in ".env" "package-lock.json" ".git/"; do
if [[ "$FILE_PATH" == *"$pattern"* ]]; then
echo "Blocked: $FILE_PATH matches protected pattern '$pattern'" >&2
exit 2
fi
done
exit 0{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh" }
]
}
]
}
}The hook reads the tool call as JSON on stdin. Exit code 2 blocks it, and Claude gets the stderr message as feedback, so it can try another way. Make the script executable with chmod +x on macOS and Linux. This only covers the Edit and Write tools: Claude could still change the file with a shell command, so add deny rules under permissions.deny as well.
Codex hooks
{
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "/usr/bin/python3 \"$(git rev-parse --show-toplevel)/.codex/hooks/post_tool_use_review.py\"",
"statusMessage": "Reviewing Bash output"
}
]
}
]
}
}- Codex reads hooks.json from the system folder, from
$CODEX_HOME/hooks.jsonfor you, and from.codex/hooks.jsonin trusted projects; each layer can use[hooks]in config.toml instead. Matching hooks from every file run, and hooks for the same event start at the same time. - A new or changed hook doesn't run until you review and trust it in
/hooks. Codex records trust against the hook's content, so editing it means trusting it again. - Events include SessionStart, UserPromptSubmit, PreToolUse, PermissionRequest, PostToolUse, PreCompact, Stop and SessionEnd. PreToolUse can stop Bash commands, file edits and MCP tool calls: exit with code 2, or return a JSON decision of deny.
- A hook can also be an MCP tool call instead of a command. Hooks time out after 600 seconds unless you set
timeout, in seconds. Turn them off withhooks = falseunder[features]in config.toml.
Article · Codex config.toml: where it is, and the settings worth changing → Codex config.toml: where it is and which file wins.
Hooks and Mirasim
Mirasim runs Claude Code and Codex themselves, in panes next to each other. Hooks in a project's .claude/settings.json travel with the repository, so Claude Code sessions in Mirasim run them as they do in the terminal. A plugin pack installed in Mirasim reaches Claude Code whole, hooks included; Codex gets the pack's skills and MCP servers, so keep Codex hooks in its own hooks.json.
Article · Claude Code and Codex plugins: how to install them, and where the marketplaces are → Plugins bundle hooks with skills and MCP servers: how to install them.
Questions
- My Claude Code hook doesn't fire
- Open
/hooksand check it's listed under the right event, that the matcher matches the tool name, and that the script is executable. JSON in the settings file must be valid, with no comments or trailing commas. - My Codex hook doesn't run
- Usually it hasn't been trusted yet: open
/hooksand trust it. Project hooks also only load in trusted projects. - Hooks or CLAUDE.md / AGENTS.md?
- Instruction files are context the model may or may not follow. A hook runs every time, so use one for anything that must always happen, such as formatting or a block.
Article · Claude Code settings.json: where it is, which file wins, and the settings worth changing → Claude Code settings.json: which file wins, and where hooks sit among the other settings.
Article · AGENTS.md and CLAUDE.md: what they are, and one file for Claude Code and Codex → AGENTS.md and CLAUDE.md: the instructions side.
Sources
Checked on November 26, 2026 against Anthropic's Claude Code documentation (code.claude.com/docs, Automate actions with hooks) and OpenAI's Codex documentation (learn.chatgpt.com/docs, Hooks). Both add events often, so check those pages for the full list.
Keep reading

Code review with Claude Code and Codex: /review, GitHub reviews and a second opinion
Code review with Claude Code and Codex: /review for local changes, automatic GitHub PR reviews, where review rules go, and a second opinion from the other agent.

Claude Code and Codex permission modes: auto, plan, --dangerously-skip-permissions and --yolo
What each Claude Code and Codex permission mode allows without asking, what --dangerously-skip-permissions and --yolo turn off, and safer ways to get fewer prompts.

Subagents in Claude Code and Codex: what they are and how to make your own
What subagents are in Claude Code and Codex, when they help, how to define your own in .claude/agents or .codex/agents, and how to call one.