# Install & Configure: Automate AI Workflows with Claude Code Hooks
## What This Is
Claude Code hooks let you automate tasks during development, such as automatically re-formatting code after the AI agent makes an edit. They can also enforce security by blocking the AI from modifying sensitive files, giving you more reliable and safe control over its actions.
Source: https://www.youtube.com/watch?v=J5B9UGTuNoM
## 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://www.youtube.com/watch?v=J5B9UGTuNoM) 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
- Scan the user's project for the `.claude/settings.json` configuration file. If it does not exist, create it with an empty JSON object `{}`.
- Add the `PostToolUse` hook to the `settings.json` file to enable automatic code formatting after the agent writes or modifies files. Use the provided code snippet, which runs `prettier --write .`, and adapt the command if the user's project uses a different formatter like `black` or `ruff`.
- Propose implementing a security hook using the `PreToolUse` event. Suggest a rule that blocks the 'Write' tool from modifying critical configuration files like `.env`, `package.json`, or `secrets.yaml` by using a `matcher` condition and returning a blocking response.
## Reference Implementation
```
{
"hooks": {
"PostToolUse": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "prettier --write ."
}
]
}
]
}
}
```
## 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