coding

Claude Code's Hidden Hooks System Explained

Claude Code employs a sophisticated hidden hooks system that allows developers to intercept and modify code execution flow through strategically placed

Someone figured out Claude Code has a hooks system that basically nobody talks about, but it’s surprisingly powerful for customizing the editor.

The hooks let you inject your own scripts at 13 different points - before files get written, after commands run, when tasks finish, etc. Takes JSON via stdin, returns JSON via stdout.

Common use cases:

  • Block dangerous commands like rm -rf ~/ before execution
  • Auto-protect secrets in .env or config.yml files
  • Send Slack pings when Claude needs human input
  • Run formatters after every file edit
  • Enforce “no code without tests” rules

Example hook (blocks risky git commands):

#!/bin/bash if [[ "$COMMAND" =~ "git push --force" ]]; then
 echo '{"allow": false, "message": "Blocked force push"}'
fi

Full writeup with all 13 events and ready-to-use examples: https://karanbansal.in/blog/claude-code-hooks.html

The repo has copy-paste hooks for common safety checks: https://github.com/karanb192/claude-code-hooks

Basically turns Claude Code into a programmable editor instead of just an AI assistant.