Cette page est affichée en anglais. Une traduction française est en cours.
SkillsApr 7, 2026·2 min de lecture

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.

Prêt pour agents

Installation avec revue préalable

Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.

Needs Confirmation · 66/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Claude Code Hooks — Automate Your AI Workflow
Commande avec revue préalable
npx -y tokrepo@latest install 587e4918-987f-4705-b4dd-e7abb7c7c9be --target codex

Dry-run d'abord, confirmez les écritures, puis lancez cette commande.

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

Questions fréquentes

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.

Sources citées (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
🙏

Source et remerciements

Built into Claude Code by Anthropic.

Documentation: docs.anthropic.com/en/docs/claude-code/hooks

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires