AI Learn
Anthropic · Practical Tutorial

Use Claude Code Hooks to automate checks without hiding risk

Run deterministic logic at lifecycle events: format after edits, block dangerous commands, inject context or verify completion without relying on Claude to remember.

Reviewed September 20, 2026Independent tutorialFeatures may vary
Not official support. AI Learn is an independent education site. Check the provider’s current documentation for plan-specific limits, pricing and feature availability.
Before you start

Hooks execute real commands with your permissions. Start with harmless, observable checks and treat every hook script like production automation: validate input, quote variables, use explicit paths and never expose secrets in output.

1. Choose one deterministic event that should always happen

Choose an event that truly benefits from deterministic enforcement—such as blocking commits when generated files are stale—instead of turning every preference into a hook. Hooks are strongest for rules that should run every time.

2. Write the smallest script and test it manually before attaching it

Implement the smallest script that can express the check and run it directly in the shell with both passing and failing examples. Record the expected exit codes and output before Claude Code is involved.

3. Add the hook in the appropriate project or user settings file with a narrow matcher

Attach the hook to the narrowest event and matcher that covers the rule. Broad matchers increase latency and can unexpectedly block unrelated commands, so start restrictive and expand only with evidence.

4. Trigger one controlled Claude Code action and inspect input, output and exit behavior

Trigger one controlled action and inspect the actual input payload, stdout/stderr and exit status. Confirm a passing case remains unobtrusive and a failing case explains exactly what the developer should fix.

5. Keep blocking hooks explicit and document how teammates can disable or debug them

Document how to disable or debug the hook before sharing it with a team. A blocking automation without an escape hatch or clear owner can halt work even when the underlying policy is correct.

Try this prompt

Create a project-level Claude Code hook that runs [formatter/linter/check] only after Write or Edit. Keep the matcher narrow. Show me the settings JSON and script separately, explain the exit codes, and do not add auto-approval for unrelated commands.

Common mistakes

Attaching a wildcard matcher to a destructive script; parsing untrusted stdin unsafely; using hooks to silently approve broad shell access; hiding failures with `|| true`; committing machine-specific paths into shared settings.

Finish check

The hook fires only on the intended event, its command can be explained and reproduced outside Claude Code, failures are visible, and it does not widen unrelated permissions.

开始之前

Hook 会以你的本机权限真正执行命令。先从无害、可观察的检查开始,并把 Hook 脚本当成生产自动化来审查:校验输入、正确引用变量、使用明确路径,不要在输出里暴露秘密。

1. 先选一个“每次都应该发生”的确定性事件

先选择真正适合“每次都强制执行”的确定性事件,例如生成文件过期时阻止提交,而不是把所有个人偏好都做成 Hook。Hook 最适合必须稳定重复执行的规则。

2. 先手工测试最小脚本,再把它接入 Hook

先写出能够完成检查的最小脚本,并直接在终端里分别跑“通过”和“失败”案例。先确认预期退出码和输出,再把脚本接到 Claude Code。

3. 在合适的项目级或用户级 settings 中配置,并使用尽量窄的 matcher

把 Hook 绑定到最窄的事件和 matcher。匹配范围太大会增加延迟,还可能意外拦截无关命令,因此应先收紧,再根据真实需要扩展。

4. 触发一次受控的 Claude Code 操作,检查 Hook 的输入、输出和退出行为

触发一次可控操作,检查实际输入、stdout/stderr 和退出状态。通过案例应尽量安静,失败案例必须明确告诉开发者哪里不符合要求以及下一步怎么修。

5. 阻断型 Hook 必须写清规则,并记录团队如何禁用和调试

团队使用前写清如何禁用和调试这个 Hook。没有退出通道、错误信息或负责人,即使规则本身正确,也可能因为自动化故障让整个开发流程停住。

可以直接套用的提示词

创建一个项目级 Claude Code Hook:只在 Write 或 Edit 后运行 [formatter/linter/check]。matcher 要尽量窄。分别给出 settings JSON 和脚本,解释退出码,不要顺便给其他无关命令自动授权。

常见错误

用通配 matcher 触发破坏性脚本;不安全地解析 stdin;利用 Hook 偷偷批准宽泛 shell 权限;用 `|| true` 把失败全部吞掉;把本机专用路径提交进共享配置。

完成检查

Hook 只在目标事件触发,命令可以在 Claude Code 之外独立解释和复现,失败不会被隐藏,也不会扩大无关权限。

開始之前

Hook 會以你的本機權限真正執行命令。先從無害、可觀察的檢查開始,並把 Hook 腳本當成生產自動化來審查:校驗輸入、正確引用變量、使用明確路徑,不要在輸出裡暴露秘密。

1. 先選一個“每次都應該發生”的確定性事件

先選擇真正適合“每次都強制執行”的確定性事件,例如生成檔案過期時阻止提交,而不是把所有個人偏好都做成 Hook。Hook 最適合必須穩定重復執行的規則。

2. 先手工測試最小腳本,再把它接入 Hook

先寫出能夠完成檢查的最小腳本,並直接在終端裡分別跑“通過”和“失敗”案例。先確認預期退出碼和輸出,再把腳本接到 Claude Code。

3. 在合適的專案級或使用者級 settings 中設定,並使用盡量窄的 matcher

把 Hook 綁定到最窄的事件和 matcher。匹配範圍太大會增加延遲,還可能意外攔截無關命令,因此應先收緊,再根據真實需要擴展。

4. 觸發一次受控的 Claude Code 操作,檢查 Hook 的輸入、輸出和退出行為

觸發一次可控操作,檢查實際輸入、stdout/stderr 和退出狀態。通過案例應盡量安靜,失敗案例必須明確告訴開發者哪裡不符合要求以及下一步怎麼修。

5. 阻斷型 Hook 必須寫清規則,並記錄團隊如何禁用和調試

團隊使用前寫清如何禁用和調試這個 Hook。沒有退出通道、錯誤資訊或負責人,即使規則本身正確,也可能因為自動化故障讓整個開發流程停住。

可以直接套用的提示詞

創建一個專案級 Claude Code Hook:只在 Write 或 Edit 後執行 [formatter/linter/check]。matcher 要盡量窄。分別給出 settings JSON 和腳本,解釋退出碼,不要順便給其他無關命令自動授權。

常見錯誤

用通配 matcher 觸發破壞性腳本;不安全地解析 stdin;利用 Hook 偷偷批准寬泛 shell 權限;用 `|| true` 把失敗全部吞掉;把本機專用路徑提交進共享設定。

完成檢查

Hook 只在目標事件觸發,命令可以在 Claude Code 之外獨立解釋和復現,失敗不會被隱藏,也不會擴大無關權限。

Official references

Check current product details at the source

Related practical tutorials

Continue with another single-task workflow.

All tutorials →