← Blog

How-to5 min read

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.

Claude Code's own terminal interface running unmodified inside a Mirasim paneClaude Code's own terminal interface running unmodified inside a Mirasim pane

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 CodeCodex
Where they goThe hooks key of a settings.jsonhooks.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 runsRuns once configuredYou review and trust each hook first
Block an actionExit code 2, reason on stderrExit code 2, or a deny decision in JSON

Claude Code hooks

settings.json: a notification when Claude needs you (macOS), and Prettier after every edit
{
  "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 hooks object; matcher narrows it, for example to the Edit and Write tools. If the file already has hooks, 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 /hooks to 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.
EventWhen it fires
SessionStartA session begins or resumes
UserPromptSubmitYou send a prompt, before Claude reads it
PreToolUseBefore a tool call runs; can block it
PermissionRequestA tool call needs a permission decision
PostToolUseAfter a tool call succeeds
NotificationClaude Code sends a notification, such as waiting for you
StopClaude finishes responding

Block edits to protected files

.claude/hooks/protect-files.sh
#!/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
.claude/settings.json: run it before every Edit or Write
{
  "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

.codex/hooks.json in a trusted project
{
  "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.json for you, and from .codex/hooks.json in 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 with hooks = false under [features] in config.toml.

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.

Questions

My Claude Code hook doesn't fire
Open /hooks and 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 /hooks and 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.

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