← ClaudeAtlas

writing-hookslisted

How to write Claude Code hooks -- event selection, hook types, matcher patterns, blocking vs advisory, portable paths. Use when creating hooks for quality gates, automation, or policy enforcement.
xiaolai/nlpm-for-claude · ★ 62 · AI & Automation · score 80
Install: claude install-skill xiaolai/nlpm-for-claude
# Writing Hooks > Scope: covers hooks.json authoring and hook script design. For plugin architecture, see [[writing-plugins]]. For rules (which are simpler but static), see [[writing-rules]]. ## 1. Three Hook Types | Type | What it does | When to use | Complexity | |------|-------------|-------------|------------| | `command` | Runs a shell script, reads JSON from stdin | Deterministic checks: file existence, JSON validation, regex matching | Medium | | `prompt` | Injects text into Claude's context | Advisory: reminders, context injection, style guidance | Low | | `agent` | Spawns a verification agent | Complex verification: code quality, semantic analysis, multi-file checks | High | ### Type Selection Flowchart ``` Is the check deterministic (regex, file exists, JSON schema)? YES --> command hook (shell script) NO --> Does it need AI judgment? YES --> agent hook NO --> prompt hook (context injection) ``` ### Command Hook Example ```json { "hooks": { "PreToolUse": [ { "matcher": "Write|Edit", "hooks": [ { "type": "command", "command": "${CLAUDE_PLUGIN_ROOT}/scripts/check-loc.sh", "timeout": 10000 } ] } ] } } ``` Hook script receives JSON on stdin with tool name and parameters. It outputs JSON to stdout. ### Prompt Hook Example ```json { "hooks": { "UserPromptSubmit": [ { "hooks": [ { "type": "prompt",