ポッドキャストに戻るClaude
Claude Code 101
Claude Code の Hooks
Hooks let you run commands at different points in Claude code's life cycle.
hooks を使うと、Claude Code のライフサイクルのさまざまな時点でコマンドを実行できます。
The key difference between hooks and everything else we covered is that hooks are deterministic.
hooks とこれまで説明してきた他の機能との重要な違いは、hooks が決定論的であるという点です。
They always run.
必ず実行されます。
So, put it this way.
こう言い換えましょう。
You can tell Claude in your claude.md file to run prettier after every file edit and most of the time it will do that, but sometimes it won't.
claude.md ファイルで Claude にファイル編集のたびに prettier を実行するよう指示できますが、ほとんどの場合はそうしてくれるものの、そうならないこともあります。
It's not perfect.
完璧ではありません。
But a hook makes it happen every single time with no exceptions.
しかし hook を使えば、例外なく毎回必ず実行されます。
Common use cases could include auto formatting after file edits, logging all executed commands for compliance, blocking dangerous operations like modifying production files, and sending yourself notifications when Claude finishes a task.
よくあるユースケースには、ファイル編集後の自動フォーマット、コンプライアンスのための実行コマンドのログ記録、本番ファイルの変更などの危険な操作のブロック、Claude がタスクを完了したときの通知送信などが含まれます。
Hooks are configured in your settings.json file.
hooks は settings.json ファイルで設定します。
You pick an event, optionally set a matcher for which tool it applies to, and provide a command to run.
イベントを選び、任意でそれが適用されるツールのマッチャーを設定し、実行するコマンドを指定します。
User prompts submit runs when you submit a prompt before Claude processes it.
UserPromptSubmit は、プロンプトを送信した後、Claude が処理する前に実行されます。
Pre-tool use which runs before a tool call, post-tool use runs after a tool call completes.
PreToolUse はツール呼び出しの前に実行され、PostToolUse はツール呼び出しが完了した後に実行されます。
Notification runs when Claude sends a notification, and stop runs when Claude finishes responding.
Notification は Claude が通知を送信するときに実行され、Stop は Claude が応答を終えたときに実行されます。
The most common hook.
最も一般的な hook です。
Auto formatting after edits.
編集後の自動フォーマット。
You set a post-tool use hook with a matcher of edit or multi-edit, right?
PostToolUse hook を edit または multi-edit のマッチャーで設定しますよね?
So, it fires whenever Claude modifies a file.
これにより、Claude がファイルを変更するたびに発火します。
The command checks the file extension and runs the appropriate formatter.
コマンドはファイルの拡張子を確認し、適切なフォーマッターを実行します。
This could be prettier for TypeScript, go format for go, rough for Python, whatever your project uses.
TypeScript なら prettier、Go なら go format、Python なら ruff など、プロジェクトで使用しているものが使えます。
Pre-tool use hooks can block tool calls before they execute.
PreToolUse hooks は、ツール呼び出しが実行される前にそれをブロックできます。
So, your hook receives the tool name and input as JSON on stdin.
hook は stdin を通じて JSON 形式でツール名と入力を受け取ります。
If it exits with code two, the action is blocked and the STD error message gets fed back to Claude's feedback so Claude knows why it was blocked and can adjust.
終了コード 2 で終了した場合、アクションはブロックされ、標準エラーのメッセージが Claude にフィードバックされ、Claude はブロックされた理由を把握して調整できます。
Exit code zero means proceed.
終了コード 0 は続行を意味します。
Exit code two means block.
終了コード 2 はブロックを意味します。
This is how you enforce hard rules.
これがハードルールを強制する方法です。
Block writes to a production config directory, block bash commands that contain rm-rf, block commits to main, whatever your team needs to be guaranteed, not suggested.
本番設定ディレクトリへの書き込みをブロック、rm-rf を含む bash コマンドをブロック、main へのコミットをブロックなど、チームが保証する必要があることを、単なる提案ではなく確実に実現します。
Hooks configured in .Claude/settings.json are project level and can be checked into your repo. This means that your entire team gets the same hooks automatically.
.Claude/settings.json に設定された hooks はプロジェクトレベルのもので、リポジトリにチェックインできます。これにより、チーム全体が自動的に同じ hooks を取得します。
Use the Claude project dir environment variable in your commands to reference scripts stored in your project so they work regardless of Claude's current working directory.
コマンドで Claude プロジェクトディレクトリ環境変数を使い、プロジェクトに保存されたスクリプトを参照することで、Claude の現在の作業ディレクトリに関係なく動作します。
[music]
[music]
Hooks gives you deterministic control over Claude code's behavior.
hooks を使うことで、Claude Code の動作を決定論的に制御できます。
[music]
[music]
Use post-tool-use for auto-formatting and logging. Use pre-tool-use to block dangerous operations. Configure them in the /hooks or in settings.
PostToolUse を自動フォーマットやログ記録に使用し、PreToolUse を危険な操作のブロックに使用します。/hooks または settings で設定してください。
json
json
[music]
[music]
and check them into your repository so your team gets them, too.
そしてリポジトリにチェックインして、チームも使えるようにしましょう。
If something needs to happen every time without fail, don't put it in a prompt.
何かが毎回必ず実行される必要があるなら、プロンプトには書かないでください。
Put it in a hook.
hook に書いてください。
[music]
[music]