Skip to content
LinkedInX

How to Use Claude Code Hooks: Real Examples of Adding Custom Processing Before and After Tool Execution

About This Article

Claude Code hooks are a mechanism that lets you configure processing to run automatically immediately before or after the AI uses a tool. This article documents real examples of hook configurations I use and how they changed my workflow.


What Are Hooks?

When Claude Code is working, the AI uses “tools” such as editing files or running commands. Hooks let you insert additional processing automatically before or after those tool executions.

Even without a technical background, the concept is approachable: imagine “a backup is created automatically just before a file is edited” or “a notification arrives when a specific command runs.” That is what hooks do.

Before hooks, I either ran these processes manually or forgot and dealt with the consequences later.


Hook Examples I Use in Practice

1. Pre-Edit Backup

I have a hook that copies a file to a backup location before the AI overwrites it.

There was a time when the AI changed an important configuration file and the result differed from what I expected. The file was tracked by Git so I was eventually able to revert, but the revert took time. Since automating backups with a hook, the effort needed to check the state before an AI change has decreased.

I have a hook that sends a notification when npm run build or any command related to production deployment is about to run.

This project has a rule that npm run build must not run without explicit user approval. By using a hook to surface “this command is about to run,” I added a confirmation checkpoint that helps prevent unintended executions.

3. Automatic Validation After File Edits

I have a hook that runs a frontmatter validation script automatically after a Markdown file is edited.

Previously the rule was “run validation manually after writing an article,” but I forgot sometimes. Since the hook runs automatically after each file edit, validation no longer gets skipped.


Configuration Overview

Hooks are defined in settings.json. The configuration structure has two parts: “which event to respond to” and “what to run.”

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "bash /path/to/backup-script.sh"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "npm run validate:frontmatter"
          }
        ]
      }
    ]
  }
}

PreToolUse fires before tool execution and PostToolUse fires after. The matcher field specifies which tool triggers the hook.


Before and After

Before hooks, I ran verification steps manually after the AI finished working. I sometimes forgot to take backups, or discovered validation issues only when I ran the check much later.

After hooks, a verification process runs automatically every time the AI acts, so fewer manual confirmation steps are needed. For operations that change multiple files at once in particular, hooks now catch problems that I would otherwise have missed.

Adding hooks changes the structure from “the AI moves freely” to “a safety check runs every time the AI moves.” Thinking about it as a design question rather than a question of trust in the AI makes it easier to choose the right hooks for each use case.


Summary

  • Claude Code hooks insert automatic processing before and after tool execution
  • Practical uses: automating backups, notifications, and validation
  • Configure with PreToolUse / PostToolUse in the hooks section of settings.json
  • Replacing manual verification steps with hooks reduces the chance of skipping them