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

Claude Code Hooks — Automate Pre/Post Task Actions

Complete guide to Claude Code hooks for automating actions before and after tool calls. Set up linting, testing, notifications, and custom validation with shell commands.

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 Pre/Post Task Actions
Commande avec revue préalable
npx -y tokrepo@latest install ba645a85-3df0-40ab-8c75-15aec5c8a2af --target codex

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

TL;DR
Claude Code hooks run custom scripts before or after tool calls for linting, testing, and validation.
§01

What it is

Claude Code hooks let you define custom scripts that run automatically before or after specific tool calls. When Claude Code writes a file, you can trigger ESLint to fix it. When Claude Code finishes a task, you can run your test suite. Hooks are configured in ~/.claude/settings.json and apply to all projects or per-project in .claude/settings.json.

Hooks are designed for developers who want to enforce code quality standards, automate repetitive checks, and integrate external tools into their Claude Code workflow without manual intervention.

§02

How it saves time or tokens

Without hooks, you manually run linters, formatters, and tests after Claude Code modifies files. Hooks automate this entirely. Every file write triggers your configured checks, catching issues immediately rather than at the end of a long coding session.

Hooks also reduce token waste. If a linter catches an error right after the file is written, Claude Code can fix it in the next turn instead of generating more code on top of a broken file.

§03

How to use

  1. Open your Claude Code settings file:
# Global: ~/.claude/settings.json
# Per-project: .claude/settings.json
  1. Add hook configurations:
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "hooks": ["eslint --fix $CLAUDE_FILE_PATH"]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": ["prettier --write $CLAUDE_FILE_PATH"]
      }
    ]
  }
}
  1. Claude Code will now run these hooks automatically when the matched tool is used.
§04

Example

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": ["echo 'About to run a bash command'"]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          "eslint --fix $CLAUDE_FILE_PATH",
          "prettier --write $CLAUDE_FILE_PATH"
        ]
      },
      {
        "matcher": "Bash",
        "hooks": ["echo 'Bash command completed'"]
      }
    ],
    "PostTask": [
      {
        "matcher": ".*",
        "hooks": ["npm test 2>&1 | tail -20"]
      }
    ]
  }
}

This configuration lints and formats after every file write, logs bash command execution, and runs the test suite after each completed task.

§05

Related on TokRepo

§06

Common pitfalls

  • Hook scripts must be available on the system PATH. If ESLint is installed locally in node_modules, use npx eslint instead of bare eslint in the hook command.
  • Long-running hooks block Claude Code's execution. Keep hooks fast (under 5 seconds). Move heavy operations like full test suites to PostTask rather than PostToolUse.
  • The $CLAUDE_FILE_PATH variable is only available for file-related tools like Write. Using it with non-file tools like Bash will produce an empty string or an error.

Questions fréquentes

What hook events are available in Claude Code?+

Claude Code supports PreToolUse (before a tool call), PostToolUse (after a tool call), and PostTask (after an entire task completes). Each event type can have multiple hooks with different matchers. The matcher field specifies which tool triggers the hook.

Can I use hooks for notifications?+

Yes. You can configure PostTask hooks to send notifications via curl to Slack, Discord, or email services. For example, a hook like 'curl -X POST https://hooks.slack.com/... -d ...' sends a message when a task completes.

Do hooks run in the background or block execution?+

Hooks run synchronously and block Claude Code until they complete. This is by design -- it ensures that lint errors are caught before Claude Code proceeds. For long-running operations, consider using background processes or moving them to PostTask.

Can I disable hooks for a specific session?+

You can override hooks by using a per-project .claude/settings.json with an empty hooks configuration. There is no built-in flag to skip hooks for a single session, but you can temporarily rename the settings file.

How do I debug hook failures?+

Hook output (stdout and stderr) is displayed in the Claude Code terminal. If a hook fails, the error message appears inline. Test your hook commands manually in the terminal first to ensure they work before adding them to the configuration.

Sources citées (3)
🙏

Source et remerciements

Fil de discussion

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

Actifs similaires