Claude Code Hooks and CLAUDE.md Setup Guide
Claude Code hooks let you automatically run shell commands (like code formatting or type-checking) whenever Claude edits or writes files, keeping your codebase clean without manual intervention. CLAUDE.md is a project-level file Claude reads on startup to understand your codebase conventions, build commands, and architecture—eliminating the need for repeated context-setting. Together, these two features turn Claude Code from a chat assistant into a semi-autonomous coding agent with guardrails.
MISSION OBJECTIVES
- 01Create a `.claude/settings.json` file in your project root and add a hook that runs `prettier --write` on any file Claude edits, using the exact config snippet from this guide.
- 02Add a `CLAUDE.md` file to your project root and populate it with your build command, lint command, test command, and 3–5 key architectural decisions Claude should always respect.
- 03Install the `event-logger.py` script from karanb192/claude-code-hooks on GitHub to inspect the raw JSON stdin payload for each hook event, so you can build custom hooks confidently.
CODE INTELLIGENCE
{
"hooks": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "prettier --write \"$CLAUDE_FILE_PATHS\""
}
]
}
]
}FIELD OPERATIONS
Auto-Quality Gate Hook Pipeline
Build a `.claude/settings.json` configuration with chained hooks: on PostToolUse for Write/Edit events, run ESLint, then TypeScript compiler, then your test suite—using exit codes to block Claude from proceeding if any check fails, creating a fully automated quality gate.
Dangerous Command Blocker
Write a PreToolUse hook that inspects the incoming JSON payload for shell commands matching patterns like `rm -rf`, `DROP TABLE`, or `git push --force`, returns a non-zero exit code with a rejection message, and logs blocked attempts to a local audit file for review.
STRATEGIC APPLICATIONS
- →A software agency can add a CLAUDE.md per client project encoding that client's coding standards, then use PostToolUse hooks to auto-run their linter and formatter—ensuring every AI-generated code change is production-ready before human review, cutting QA time.
- →A SaaS startup can configure PreToolUse hooks to block Claude from touching environment config files or deleting database migration files, protecting critical infrastructure from accidental AI-driven changes during rapid feature development sprints.