Skills2026年4月6日·1 分钟阅读

Claude Code Hooks — Custom Automation Recipes

Collection of ready-to-use Claude Code hook recipes for automating code formatting, testing, notifications, and security checks. Copy-paste into settings.json. Community-maintained.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Claude Code Hooks — Custom Automation Recipes
先审查命令
npx -y tokrepo@latest install 36711c75-fc63-49ad-9013-4a7cf5fae8a1 --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
Claude Code hooks are shell commands that auto-run before or after tool use for formatting, testing, and security checks.
§01

What it is

Claude Code Hooks are shell commands that execute automatically before or after specific tool uses within Claude Code sessions. They work like git hooks but for your AI coding assistant. This collection provides ready-to-use recipes for auto-formatting code, running tests, sending notifications, scanning for security issues, and enforcing workflow policies.

The collection targets developers using Claude Code who want to enforce code quality standards and automate repetitive tasks around AI-generated code. Each recipe is a JSON snippet you paste into your settings.json.

§02

How it saves time or tokens

Without hooks, you need to manually run formatters, linters, and tests after every file Claude Code writes or edits. Hooks automate this entire class of tasks. A PostToolUse hook on Write and Edit operations means every generated file is automatically formatted, and pre-commit quality checks happen without manual intervention.

The token estimate for this workflow is approximately 2,600 tokens. Hooks run as shell commands outside the LLM context, so they consume zero tokens.

§03

How to use

  1. Open Claude Code settings:
claude config

Or edit ~/.claude/settings.json directly.

  1. Add a hook to auto-format every file Claude writes:
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null || true"
      }
    ]
  }
}
  1. Claude Code now auto-formats every file it creates or modifies.
§04

Example

Multiple hook recipes for different automation needs:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "command": "echo 'Running: '$CLAUDE_COMMAND >> ~/.claude/audit.log"
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || true"
      }
    ],
    "Stop": [
      {
        "command": "terminal-notifier -message 'Claude Code finished' -title 'Done' 2>/dev/null || true"
      }
    ]
  }
}

Hook event types:

PreToolUse   -- Before a tool runs (can block execution)
PostToolUse  -- After a tool completes
Notification -- When Claude sends a notification
Stop         -- When Claude finishes a response
§05

Related on TokRepo

§06

Common pitfalls

  • Hook commands run synchronously. A slow command (e.g., running a full test suite on every file save) blocks Claude Code until it completes. Keep hook commands fast or run them asynchronously with &.
  • The $CLAUDE_FILE_PATH variable is only available for Write and Edit tool matchers. Other tools have different environment variables. Check the Claude Code documentation for the full variable list.
  • Hooks that exit with a non-zero status on PreToolUse can block the tool from running. Always add || true to PostToolUse hooks to prevent false failures from interrupting the workflow.

常见问题

What hook events are available?+

Four events: PreToolUse (before a tool runs, can block), PostToolUse (after a tool completes), Notification (on notifications), and Stop (when Claude finishes responding). Each accepts a matcher pattern and shell command.

Can hooks block tool execution?+

Yes. PreToolUse hooks that exit with a non-zero status prevent the tool from running. This is useful for security policies like blocking writes to protected directories. PostToolUse hooks do not block.

Do hooks consume tokens?+

No. Hooks run as shell commands in a separate process. They do not add to the LLM context or consume API tokens. The token cost is only for the hook configuration in settings.json.

Where do I configure hooks?+

Hooks go in ~/.claude/settings.json under the hooks key. You can also set project-level hooks in .claude/settings.json within your project directory for team-shared automation.

Can I use hooks for notifications?+

Yes. The Stop event fires when Claude finishes a response. Use it to trigger desktop notifications, Slack messages, or any shell command. The example uses terminal-notifier on macOS.

引用来源 (3)
🙏

来源与感谢

Community-maintained collection based on the official Claude Code docs. Contributions welcome — share your hook recipes!

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产