AGENT0S
HomeLibraryAgentic
FeedbackLearn AI
LIVE
Agent0s · AI Intelligence Library
Share FeedbackUpdated daily · 7am PST
Library/hook
hookintermediateClaude Code

Automate Development with Claude Code Hooks

This technique allows developers to automatically run custom actions at specific moments in the AI's workflow, such as before it runs a command or after it finishes a task. For example, it can be used to block the AI from accidentally deleting files or to automatically format any code the AI writes, significantly improving safety and code quality.

AI SETUP PROMPT

Paste into Claude Code — it will scan your project and set everything up

# Install & Configure: Automate Development with Claude Code Hooks

## What This Is
This technique allows developers to automatically run custom actions at specific moments in the AI's workflow, such as before it runs a command or after it finishes a task. For example, it can be used to block the AI from accidentally deleting files or to automatically format any code the AI writes, significantly improving safety and code quality.

Source: https://code.claude.com/docs/en/hooks-guide

## Before You Start

Scan my workspace and analyze:
- The project language, framework, and directory structure
- Existing agent configuration (check for .claude/, .codex/, CLAUDE.md, settings.json, commands/, skills/ directories)

Then ask me before proceeding:
1. Which lifecycle event should this hook fire on? (PreToolUse, PostToolUse, Notification, etc.)
2. Are there any files, patterns, or tools this should be scoped to?

## Source Access Note

The source URL (https://code.claude.com/docs/en/hooks-guide) may not be directly accessible from the terminal. Use the Reference Implementation and Additional Context sections below instead. If you need more details, ask me to paste relevant content from the source.

## What to Implement

This is an **Agent Hook** — a shell/HTTP command that fires at lifecycle events.

- Add the hook configuration to `.claude/settings.json` under the lifecycle event I specified
- If the hook needs a shell script, create it and make it executable (`chmod +x`)
- If the hook calls an external API, configure it using credentials from my .env files
- Validate the JSON config is syntactically correct before saving

## Additional Context

- Create a `PreToolUse` hook to increase project safety. First, create a script at `.claude/hooks/block-rm.sh` that checks for destructive bash commands like `rm *` in the JSON passed via stdin and returns `{"deny": true}` if a match is found.
- Modify the `.claude/settings.json` file to add the hook configuration. Use the provided code snippet as a template, setting the `matcher` to `Bash`, the `if` condition to `Bash(rm *)`, and the `command` to execute your new `block-rm.sh` script.
- Implement a `PostToolUse` hook for automatic code formatting. When the AI uses a file-writing tool, trigger a script that runs a linter or formatter (like Prettier or Black) on the modified file to ensure code style consistency.

## Reference Implementation

```
{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Bash",
      "hooks": [{
        "type": "command",
        "if": "Bash(rm *)",
        "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-rm.sh"
      }]
    }]
  }
}
```

## Guidelines

- Adapt everything to my existing project — do not assume a specific stack or directory layout
- Review any fetched code for safety before installing or executing it
- After setup, run a quick verification and show me a summary of exactly what was installed, where, and how to use it
2,951 charactersCompatible with Claude Code & Codex CLI
MANUAL SETUP STEPS
  1. 01Create a `PreToolUse` hook to increase project safety. First, create a script at `.claude/hooks/block-rm.sh` that checks for destructive bash commands like `rm *` in the JSON passed via stdin and returns `{"deny": true}` if a match is found.
  2. 02Modify the `.claude/settings.json` file to add the hook configuration. Use the provided code snippet as a template, setting the `matcher` to `Bash`, the `if` condition to `Bash(rm *)`, and the `command` to execute your new `block-rm.sh` script.
  3. 03Implement a `PostToolUse` hook for automatic code formatting. When the AI uses a file-writing tool, trigger a script that runs a linter or formatter (like Prettier or Black) on the modified file to ensure code style consistency.

CODE INTELLIGENCE

bash
{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Bash",
      "hooks": [{
        "type": "command",
        "if": "Bash(rm *)",
        "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-rm.sh"
      }]
    }]
  }
}

FIELD OPERATIONS

Self-Formatting Code Agent

Configure a `PostToolUse` hook that triggers whenever the 'edit' or 'write' tool is used on files with extensions like `.js`, `.py`, or `.go`. The hook will run `prettier`, `black`, or `gofmt` on the modified file, ensuring all AI-generated code automatically adheres to the project's coding style.

Dynamic Session Starter

Implement a `SessionStart` hook that runs a script to analyze the current working directory. The script finds the nearest `README.md`, `package.json`, or a custom `.project-context` file and injects its contents as an initial system message, giving Claude immediate, relevant context.

STRATEGIC APPLICATIONS

  • →In a regulated industry, use a `PreToolUse` hook as a security gateway to block the AI agent from accessing sensitive files, using unapproved external APIs, or executing risky shell commands, providing a programmable layer to enforce compliance.
  • →For a software team, configure `PostToolUseFailure` and `Stop` hooks to create an automated quality assurance feed. The hooks can log detailed tool execution errors to a service like Datadog and send session transcripts to a database for performance analysis.

TAGS

#hooks#automation#lifecycle#security#workflow#configuration
Source: WEB · Quality score: 8/10
VIEW SOURCE