# 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