Vibe Coding
Back to Claude Code

Claude Code / Reference

Claude Code hook

Hooks can execute commands before and after events in Claude Code for formatting, inspection, notification, and automation.

Claude Code hook key concepts infographic
Claude Code hook key concepts infographic

What are hooks

Hooks allow custom logic to be run before and after Claude Code's tool calls, permission requests, prompt handling, or session events. It's suitable for mechanizing formatting, testing, notifications, and security blocking.

Hooks don't just automate script execution; they can also influence Claude's next actions through exit codes, JSON output, and additionalContext.

Common events

The official Hooks documentation covers events, configuration schema, JSON input and output, exit codes, asynchronous Hooks, HTTP Hooks, Prompt Hooks and MCP tool Hooks.

Hook typePurpose
PreToolUseCheck or block before running the tool.
PostToolUseFormat, test, and add context after the tool is run.
PermissionRequestCustomized permission judgment or audit.
Prompt hooksProcess or supplement user prompts.
HTTP hooksCall external services to handle events.

Output should be restrained

Hook output may go into context. Only write the results that Claude needs to know to the additionalContext; large amounts of logs should go to the debug log or external system.

{
  "hookSpecificOutput": {
    "additionalContext": "Lint passed for edited files."
  }
}

Block dangerous actions

For high-risk commands such as deletion, deployment, production database, and key reading, blocking with PreToolUse or permission rules is more reliable than relying on verbal reminders.

Complete usage points

Supplement the core concepts, operation sequences, permission boundaries and verification requirements that are easily compressed and missed in official documents, making it easier for English readers to learn completely by page.

Hooks Supplement

Hooks can run logic before and after Claude Code's tool calls, permission requests, prompt handling, or session events. They are suitable for mechanizing formatting, testing, security blocking, notifications and context addition.

  • PreToolUse: Check before command to block dangerous actions.
  • PostToolUse: Format, test, and supplement results after command.
  • PermissionRequest: Customized permission judgment and auditing.
  • Prompt hooks: handle or supplement user prompts.
  • HTTP hooks: Send events to external services.

Hook output should be restrained. Only conclusions that Claude needs to know next are put into the additionalContext; extensive logging should be written to a file or external system, otherwise it will pollute the context window.

Study Checklist

Put the content on this page into real tasks and use the five dimensions of entry, context, permissions, verification and team rules to check whether you have truly mastered it.

Study Checklist

After reading this page, do not just remember the concept name. You should be able to place "Claude Code hook" back into a real Claude Code engineering workflow: where the task starts, what context the system loads, which actions need approval, how the result is verified, and how to roll back when it fails.

If this is a configuration or reference page, be specific about where the configuration is placed, whether it will be submitted, whether it contains sensitive information, whether it will extend the default permissions, and how to troubleshoot the settings that actually took effect in the event of a failure.

  • Be able to describe in your own words the specific problem this page solves, rather than just reciting the title.
  • Able to write a minimal example task with goals, scope, prohibitions, and acceptance criteria.
  • Be able to determine which information should be put into the current prompt and which should be captured as project rules or configurations.
  • Be able to explain which long-term rules should go into CLAUDE.md, and which runtime behavior should be handled by settings, permissions, Hooks, Skills and MCP.
  • Ability to check diffs, command output, test results, screenshots or PR notes after a task is completed instead of just trusting the natural language summary.

If this page is used for team training, ask learners to complete a small task with Claude Code: read and explain first, submit a plan, make the smallest useful change, and close with real verification commands plus human diff review.