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

Claude Code Hooks — Automate Your AI Workflow

Built-in automation system for Claude Code. Run shell commands on events like file edits, tool calls, and notifications. Lint on save, auto-test, and more.

Agent 就绪

先审查再安装

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

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Claude Code Hooks — Automate Your AI Workflow
先审查命令
npx -y tokrepo@latest install 587e4918-987f-4705-b4dd-e7abb7c7c9be --target codex

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

TL;DR
Claude Code Hooks run shell commands automatically on events like file edits and tool calls in your AI workflow.
§01

What it is

Claude Code Hooks are built-in automation triggers in Claude Code that run shell commands in response to specific events: file edits, tool calls, notifications, and agent stops. They let you enforce coding standards, run tests, validate changes, and integrate with external tools automatically.

Hooks target developers using Claude Code who want automated quality checks integrated directly into their AI-assisted workflow without relying on Claude to remember to run them.

§02

How it saves time or tokens

Without hooks, you need to manually remind Claude to run linters, formatters, or tests after each edit. Hooks make this automatic and deterministic. The PostToolUse event fires after every edit or write, so ESLint, Prettier, or your test suite runs without consuming additional tokens on reminder prompts. PreToolUse hooks can block operations before they execute, preventing bad patterns proactively.

§03

How to use

  1. Add hooks to your .claude/settings.json:
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "eslint --fix $CLAUDE_FILE_PATH"
      }
    ]
  }
}
  1. Every file edit by Claude Code now auto-runs ESLint.
  1. Add multiple hooks for different events:
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "prettier --write $CLAUDE_FILE_PATH"
      }
    ],
    "Stop": [
      {
        "matcher": ".*",
        "command": "npm test"
      }
    ]
  }
}
§04

Example

A comprehensive hook configuration for a TypeScript project:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "command": "echo 'Bash command about to run'"
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "eslint --fix $CLAUDE_FILE_PATH && prettier --write $CLAUDE_FILE_PATH"
      }
    ],
    "Stop": [
      {
        "matcher": ".*",
        "command": "npx tsc --noEmit"
      }
    ]
  }
}
§05

Related on TokRepo

§06

Common pitfalls

  • Hook commands that take more than a few seconds block Claude from proceeding; keep hooks fast or run them asynchronously
  • The $CLAUDE_FILE_PATH variable is only available for file-related tool events; using it in Stop hooks causes empty path errors
  • Hooks run in a shell subprocess; ensure the commands are available in your PATH and not aliased only in your interactive shell

常见问题

What events can trigger Claude Code Hooks?+

Five event types are available: PreToolUse fires before a tool executes, PostToolUse fires after completion, Notification fires when Claude sends a notification, Stop fires when Claude finishes a turn, and SubagentStop fires when a subagent completes.

Can hooks block a tool from executing?+

Yes. PreToolUse hooks run before the tool executes. If the hook command returns a non-zero exit code, the tool execution is blocked. This lets you prevent operations that violate your coding standards before they happen.

How do I target specific tools with matchers?+

The matcher field accepts a regex pattern. Use 'Edit|Write' to match file modification tools, 'Bash' for shell commands, or '.*' to match all tools. The pattern is matched against the tool name, not the tool arguments.

Do hooks consume Claude tokens?+

No. Hooks are shell commands that run outside the Claude conversation. They do not generate API calls or consume tokens. The hook output may be shown to Claude for context, but the hook execution itself is free.

Can I use hooks in team settings?+

Yes. Place the settings.json in the project-level .claude/settings.json directory and commit it to version control. All team members using Claude Code on the project will inherit the same hook configuration.

引用来源 (3)
  • Anthropic Docs— Claude Code supports hooks for event-driven automation
  • Anthropic— Claude Code is Anthropic's official CLI for Claude
  • ESLint Docs— ESLint is a pluggable JavaScript linter
🙏

来源与感谢

Built into Claude Code by Anthropic

讨论

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

相关资产