DEV Community

Sangmin Lee
Sangmin Lee

Posted on • Originally published at claudeguide.io

Claude Code Hooks: Automate Workflows with Pre and Post Tool Execution

Originally published at claudeguide.io/claude-code-hooks-guide

Claude Code Hooks: Automate Workflows with Pre and Post Tool Execution

Claude Code hooks are shell commands that run automatically before or after Claude executes a tool. They let you enforce code quality standards, run formatters, trigger tests, log activity, and block unsafe operations — all without Claude needing to remember to do any of it. Hooks fire at the system level, making them reliable in every session without prompting or reminders in 2026.


What hooks are and how they work

Every time Claude Code calls a tool — Write, Edit, Bash, Read, Grep, Glob — two hook points fire in sequence:

  • PreToolUse: executes before the tool runs. If the hook exits with a non-zero code, the tool call is blocked entirely.
  • PostToolUse: executes after the tool completes. Receives the result, can trigger side effects like formatting or logging.

This architecture means hooks are not suggestions to Claude. They are commands your shell runs unconditionally whenever Claude attempts a matched tool call. You can use PreToolUse as a hard gate (no rm -rf without approval), or PostToolUse as a side-effect trigger (run prettier after every file write).


Where to configure hooks

Hooks live in a settings.json file. There are two locations, and both can be active at once:

Project-level: .claude/settings.json in your project root. Apply to everyone working in that repository. Commit this file to share hooks across a team.

Global (user-level): ~/.claude/settings.json in your home directory. Apply to all Claude Code sessions on your machine, regardless of project.

When both files exist, hooks from both files are active. There is no override — both sets run. Use project-level hooks for project-specific enforcement (test suite, linter config, logging path) and global hooks for universal policies (security checks, audit logging).


Hook configuration structure

The hooks key at the top level of settings.json contains an object with PreToolUse and PostToolUse arrays. Each entry has a matcher (the tool name to match) and a nested hooks array with one or more command objects.


json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "echo 'About to run bash command'"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "prettier --write $CLAUDE_TOOL_INPUT_FILE_PATH 2

[-

*30-day money-back guarantee. Instant download.*

---

## Related guides

- [Claude Code Project Setup: CLAUDE.md and Settings That Save Time](/claude-code-first-project-setup)
- [Claude Code Slash Commands Reference](/claude-code-slash-commands-reference)
- [Claude Code with Large Codebases: Strategies That Work](/claude-code-large-codebase)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)