Skip to main content
Tips

practitioner tip

Automate Your Workflow with Claude Code Hooks

AIErudit angle: Hooks make AI-assisted workflows deterministic — unlike CLAUDE.md guidance the model can ignore, a hook always fires when its condition is met. This tip shows the three handler types (command, prompt, agent) and lifecycle events so you can auto-format, block destructive commands, and enforce test coverage without relying on the model's goodwill. It is a delivery-reliability skill for productionizing agents.

hooksautomationclaude-codetips
Tip visual

Published: Feb 23, 2026 Updated: 2026-04-28

Original: https://wmedia.es/en/tips/claude-code-hooks-automate-workflow

Quick Tip: Claude Code Hooks — Automate Your Workflow

Hooks are commands that run automatically at specific points in Claude Code's lifecycle. Unlike CLAUDE.md instructions (which Claude may ignore), hooks are deterministic: if the condition is met, they always execute. Three handler types cover everything from simple validations to AI-powered verification.

For a complete guide with 5 essential hooks and advanced patterns, see the full hooks article.

Result:

// ~/.claude/settings.json
{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Write|Edit",
      "hooks": [{
        "type": "command",
        "command": "npx prettier --write \"$CLAUDE_FILE_PATH\""
      }]
    }]
  }
}

The 3 hook types

1. Command — Run a script

The most common. Receives context via stdin as JSON, and the exit code decides the outcome: 0 passes, 2 blocks.

{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Bash",
      "hooks": [{
        "type": "command",
        "command": "echo $CLAUDE_TOOL_INPUT | jq -e '.command | test(\"rm -rf|--force|DROP TABLE\") | not'"
      }]
    }]
  }
}

This hook blocks destructive commands like rm -rf, --force, or DROP TABLE before they execute.

2. Prompt — Let the LLM decide

For subjective decisions a script can't handle. Claude evaluates context with a prompt in a single pass.

{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Write|Edit",
      "hooks": [{
        "type": "prompt",
        "prompt": "Check that the code follows the project style guide. Block if it introduces console.log statements in production code. $ARGUMENTS"
      }]
    }]
  }
}

3. Agent — Subagent with tools

For complex verification. Spawns a subagent with access to Read, Grep, and Glob that can inspect files before deciding.

{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Write|Edit",
      "hooks": [{
        "type": "agent",
        "prompt": "Verify that every new function has a corresponding test file. Check the test directory for matching test files. Block if tests are missing. $ARGUMENTS"
      }]
    }]
  }
}

Quick reference

EventWhen it firesTypical use
PreToolUseBefore a tool runsBlock dangerous actions
PostToolUseAfter a successful toolFormat, lint, type-check
NotificationWhen Claude sends a notificationCustom desktop alerts
StopWhen Claude finishes its responseFinal validation, auto-commit
PreCompactBefore context compressionRe-inject critical instructions
SessionStartWhen a session beginsEnvironment setup
TypeAI involvedBest for
commandNoBinary validations (pass/fail)
promptYes (single pass)Subjective decisions
agentYes (multi-turn)Complex verification with tools

Configuration

Hooks are defined in settings.json. Three scope levels:

  • ~/.claude/settings.json — All projects
  • .claude/settings.json — Project-level (shareable with the team)
  • .claude/settings.local.json — Project-level (private)

The fastest way to create one: type /hooks inside Claude Code.

Related: The 6 extension mechanisms · Skills: custom slash commands · Permissions system

Official docs: Hooks · Hooks Guide

Part of the 10 habits to save tokens in Claude Code.