[H1][HOOKS-BUILDER]
Dictum: Deterministic behavior requires hooks; prompts fail execution guarantees.
Build Claude Code hooks—shell commands, prompt evaluations, or multi-turn agents execute at 15 agent lifecycle events.
Tasks:
- Read lifecycle.md — 14 events, input schemas, exit codes, blocking behavior
- Read schema.md — Configuration structure, matchers, JSON responses, hook types
- (integration) Read integration.md — Environment variables, context injection
- (scripting) Read scripting.md — Python standards, security patterns
- (recipes) Read recipes.md — Proven implementation patterns
- (troubleshooting) Read troubleshooting.md — Known issues, platform workarounds
- (prose) Load
style-standards skill — Voice, formatting, constraints
- Validate — Quality gate; see §VALIDATION
Scope:
- Event Selection: Choose hook type by automation goal (blocking vs observing).
- Configuration: Author settings.json entries with matchers and timeouts.
- Response Handling: Control agent via exit codes, JSON responses, or prompt evaluation.
References:
| Domain |
File |
| Schema |
schema.md |
| Lifecycle |
lifecycle.md |
| Integration |
integration.md |
| Scripting |
scripting.md |
| Recipes |
recipes.md |
| Troubleshooting |
troubleshooting.md |
| Validation |
validation.md |
[1][EVENT_SELECTION]
Dictum: Automation goal determines hook type; blocking capability varies by event.
Decision Gate:
- Intercept before execution? → PreToolUse (validate/block/modify parameters)
- Control permission dialogs? → PermissionRequest (auto-approve/deny)
- React after completion? → PostToolUse (format, lint, add context)
- React after failure? → PostToolUseFailure (error handling, retry logic)
- Inject at session boundaries? → SessionStart (context), UserPromptSubmit (per-message)
- One-time provisioning? → Setup (tool installation, dependency setup via
claude --init)
- Evaluate task completion? → Stop/SubagentStop (prompt/agent type for LLM judgment)
- Coordinate teams? → TeammateIdle (prevent idle), TaskCompleted (validate completion)
- Observe subagent lifecycle? → SubagentStart/SubagentStop (logging)
Blocking Events (exit 2 blocks action):
| [INDEX] |
[EVENT] |
[EXIT_2_EFFECT] |
| [1] |
PreToolUse |
Blocks tool call; stderr shown to Claude |
| [2] |
PermissionRequest |
Denies the permission |
| [3] |
UserPromptSubmit |
Blocks prompt processing; erases prompt from context |
| [4] |
Stop |
Prevents Claude from stopping; continues conversation |
| [5] |
SubagentStop |
Prevents subagent from stopping |
| [6] |
TeammateIdle |
Prevents teammate from going idle; stderr = feedback |
| [7] |
TaskCompleted |
Prevents task completion; stderr = feedback to model |
Non-blocking Events (exit 2 shows stderr only):
| [INDEX] |
[EVENT] |
[EXIT_2_EFFECT] |
| [1] |
PostToolUse |
Shows stderr to Claude (tool already ran) |
| [2] |
PostToolUseFailure |
Shows stderr to Claude (tool already failed) |
| [3] |
SessionStart |
Shows stderr to user only |
| [4] |
Setup |
Shows stderr to user only |
| [6] |
SessionEnd |
Shows stderr to user only |
| [7] |
Notification |
Shows stderr to user only |
| [8] |
SubagentStart |
Shows stderr to user only |
| [9] |
PreCompact |
Shows stderr to user only |
[2][CONFIGURATION]
Dictum: Centralized configuration enables scope-aware hook precedence.
| [INDEX] |
[SCOPE] |
[PATH] |
[USE] |
[GIT] |
| [1] |
User |
~/.claude/settings.json |
Global, all projects |
N/A |
| [2] |
Project |
.claude/settings.json |
Shared, committed |
Commit |
| [3] |
Local |
.claude/settings.local.json |
Personal, testing |
Ignore |
Precedence: Local > Project > User. Same-event hooks from all scopes run in parallel.
Snapshot: Hooks captured at startup; mid-session edits require /hooks review to reload.
[3][IMPLEMENTATION]
Dictum: Deterministic and evaluative patterns require distinct execution modes.
| [INDEX] |
[TYPE] |
[USE_CASE] |
[TIMEOUT] |
[CHARACTERISTICS] |
| [1] |
command |
Validation, formatting, rules |
600s |
Deterministic, shell scripts |
| [2] |
prompt |
Complex evaluation, LLM judgment |
30s |
Single-turn, context-aware |
| [3] |
agent |
Tool-using evaluation |
60s |
Multi-turn, up to 50 turns |
Prompt/Agent Eligible Events: PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest, UserPromptSubmit, Stop, SubagentStop, TaskCompleted.
[CRITICAL] TeammateIdle does NOT support prompt or agent hooks — exit codes only.
Prompt/Agent Response Schema:
{"ok": true}
{"ok": false, "reason": "Explanation shown to Claude"}
ok: true allows the action. ok: false blocks it with the provided reason.
Command Hook Fields:
| [INDEX] |
[FIELD] |
[TYPE] |
[DEFAULT] |
[EFFECT] |
| [1] |
type |
string |
— |
"command", "prompt", or "agent" |
| [2] |
command |
string |
— |
Shell command or script path |
| [3] |
timeout |
number |
type-specific |
Seconds: command=600, prompt=30, agent=60 |
| [4] |
async |
boolean |
false |
Background execution; non-blocking |
| [5] |
statusMessage |
string |
— |
Custom spinner text during execution |
| [6] |
once |
boolean |
false |
Run once per session (skills only) |
Prompt/Agent Hook Fields:
| [INDEX] |
[FIELD] |
[TYPE] |
[DEFAULT] |
[EFFECT] |
| [1] |
prompt |
string |
— |
Instructions for LLM; $ARGUMENTS = hook JSON |
| [2] |
model |
string |
fast model |
Model to use for evaluation |
[4][SCRIPTING]
Dictum: Hook reliability requires functional pipeline patterns.
Python 3.14+ with strict typing. Zero imperative patterns.
[5][VALIDATION]
Dictum: Gates prevent incomplete artifacts.
[VERIFY] Completion:
[REFERENCE] Operational checklist: ->validation.md
1---2name: hooks-builder3description: Creates Claude Code hooks executing shell commands, prompt evaluations, or multi-turn agents at 15 lifecycle events. Use when building PreToolUse validation, PostToolUse formatting, PermissionRequest auto-approval, Stop/SubagentStop evaluation, TeammateIdle prevention, TaskCompleted gating, Setup provisioning, SessionStart context injection, or deterministic agent control via blocking/non-blocking hooks.4---56# [H1][HOOKS-BUILDER]7>**Dictum:** *Deterministic behavior requires hooks; prompts fail execution guarantees.*89<br>1011Build Claude Code hooks—shell commands, prompt evaluations, or multi-turn agents execute at 15 agent lifecycle events.1213**Tasks:**141. Read [lifecycle.md](./references/lifecycle.md) — 14 events, input schemas, exit codes, blocking behavior152. Read [schema.md](./references/schema.md) — Configuration structure, matchers, JSON responses, hook types163. (integration) Read [integration.md](./references/integration.md) — Environment variables, context injection174. (scripting) Read [scripting.md](./references/scripting.md) — Python standards, security patterns185. (recipes) Read [recipes.md](./references/recipes.md) — Proven implementation patterns196. (troubleshooting) Read [troubleshooting.md](./references/troubleshooting.md) — Known issues, platform workarounds207. (prose) Load `style-standards` skill — Voice, formatting, constraints218. Validate — Quality gate; see §VALIDATION2223**Scope:**24- *Event Selection:* Choose hook type by automation goal (blocking vs observing).25- *Configuration:* Author settings.json entries with matchers and timeouts.26- *Response Handling:* Control agent via exit codes, JSON responses, or prompt evaluation.2728**References:**2930| Domain | File |31| --------------- | ------------------------------------------------------ |32| Schema | [schema.md](references/schema.md) |33| Lifecycle | [lifecycle.md](references/lifecycle.md) |34| Integration | [integration.md](references/integration.md) |35| Scripting | [scripting.md](references/scripting.md) |36| Recipes | [recipes.md](references/recipes.md) |37| Troubleshooting | [troubleshooting.md](references/troubleshooting.md) |38| Validation | [validation.md](references/validation.md) |3940---41## [1][EVENT_SELECTION]42>**Dictum:** *Automation goal determines hook type; blocking capability varies by event.*4344<br>4546**Decision Gate:**<br>47- *Intercept before execution?* → PreToolUse (validate/block/modify parameters)48- *Control permission dialogs?* → PermissionRequest (auto-approve/deny)49- *React after completion?* → PostToolUse (format, lint, add context)50- *React after failure?* → PostToolUseFailure (error handling, retry logic)51- *Inject at session boundaries?* → SessionStart (context), UserPromptSubmit (per-message)52- *One-time provisioning?* → Setup (tool installation, dependency setup via `claude --init`)53- *Evaluate task completion?* → Stop/SubagentStop (prompt/agent type for LLM judgment)54- *Coordinate teams?* → TeammateIdle (prevent idle), TaskCompleted (validate completion)55- *Observe subagent lifecycle?* → SubagentStart/SubagentStop (logging)5657**Blocking Events (exit 2 blocks action):**5859| [INDEX] | [EVENT] | [EXIT_2_EFFECT] |60| :-----: | ----------------- | ----------------------------------------------------- |61| [1] | PreToolUse | Blocks tool call; stderr shown to Claude |62| [2] | PermissionRequest | Denies the permission |63| [3] | UserPromptSubmit | Blocks prompt processing; erases prompt from context |64| [4] | Stop | Prevents Claude from stopping; continues conversation |65| [5] | SubagentStop | Prevents subagent from stopping |66| [6] | TeammateIdle | Prevents teammate from going idle; stderr = feedback |67| [7] | TaskCompleted | Prevents task completion; stderr = feedback to model |6869**Non-blocking Events (exit 2 shows stderr only):**7071| [INDEX] | [EVENT] | [EXIT_2_EFFECT] |72| :-----: | ------------------ | -------------------------------------------- |73| [1] | PostToolUse | Shows stderr to Claude (tool already ran) |74| [2] | PostToolUseFailure | Shows stderr to Claude (tool already failed) |75| [3] | SessionStart | Shows stderr to user only |76| [4] | Setup | Shows stderr to user only |77| [6] | SessionEnd | Shows stderr to user only |78| [7] | Notification | Shows stderr to user only |79| [8] | SubagentStart | Shows stderr to user only |80| [9] | PreCompact | Shows stderr to user only |8182---83## [2][CONFIGURATION]84>**Dictum:** *Centralized configuration enables scope-aware hook precedence.*8586<br>8788| [INDEX] | [SCOPE] | [PATH] | [USE] | [GIT] |89| :-----: | ------- | ----------------------------- | -------------------- | :----: |90| [1] | User | `~/.claude/settings.json` | Global, all projects | N/A |91| [2] | Project | `.claude/settings.json` | Shared, committed | Commit |92| [3] | Local | `.claude/settings.local.json` | Personal, testing | Ignore |9394**Precedence:** Local > Project > User. Same-event hooks from all scopes run in parallel.9596**Snapshot:** Hooks captured at startup; mid-session edits require `/hooks` review to reload.9798---99## [3][IMPLEMENTATION]100>**Dictum:** *Deterministic and evaluative patterns require distinct execution modes.*101102<br>103104| [INDEX] | [TYPE] | [USE_CASE] | [TIMEOUT] | [CHARACTERISTICS] |105| :-----: | ------- | -------------------------------- | :-------: | ---------------------------- |106| [1] | command | Validation, formatting, rules | 600s | Deterministic, shell scripts |107| [2] | prompt | Complex evaluation, LLM judgment | 30s | Single-turn, context-aware |108| [3] | agent | Tool-using evaluation | 60s | Multi-turn, up to 50 turns |109110**Prompt/Agent Eligible Events:** PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest, UserPromptSubmit, Stop, SubagentStop, TaskCompleted.111112[CRITICAL] TeammateIdle does NOT support prompt or agent hooks — exit codes only.113114**Prompt/Agent Response Schema:**115```json116{"ok": true}117{"ok": false, "reason": "Explanation shown to Claude"}118```119120`ok: true` allows the action. `ok: false` blocks it with the provided reason.121122**Command Hook Fields:**123124| [INDEX] | [FIELD] | [TYPE] | [DEFAULT] | [EFFECT] |125| :-----: | --------------- | ------- | :-----------: | ----------------------------------------- |126| [1] | `type` | string | — | `"command"`, `"prompt"`, or `"agent"` |127| [2] | `command` | string | — | Shell command or script path |128| [3] | `timeout` | number | type-specific | Seconds: command=600, prompt=30, agent=60 |129| [4] | `async` | boolean | `false` | Background execution; non-blocking |130| [5] | `statusMessage` | string | — | Custom spinner text during execution |131| [6] | `once` | boolean | `false` | Run once per session (skills only) |132133**Prompt/Agent Hook Fields:**134135| [INDEX] | [FIELD] | [TYPE] | [DEFAULT] | [EFFECT] |136| :-----: | -------- | ------ | :--------: | ---------------------------------------------- |137| [1] | `prompt` | string | — | Instructions for LLM; `$ARGUMENTS` = hook JSON |138| [2] | `model` | string | fast model | Model to use for evaluation |139140---141## [4][SCRIPTING]142>**Dictum:** *Hook reliability requires functional pipeline patterns.*143144<br>145146Python 3.14+ with strict typing. Zero imperative patterns.147148---149## [5][VALIDATION]150>**Dictum:** *Gates prevent incomplete artifacts.*151152<br>153154[VERIFY] Completion:155- [ ] Event: Selected correct hook type for automation goal.156- [ ] Blocking: Verified event supports blocking (7 events) or observing (8 events).157- [ ] Schema: Configuration structure validated per schema.md.158- [ ] Timeout: Correct units (seconds): command=600, prompt=30, agent=60.159- [ ] Integration: Environment variables and context injection applied.160- [ ] Scripting: Security patterns and tooling gates passed.161- [ ] Quality: JSON syntax valid, timeouts appropriate.162163[REFERENCE] Operational checklist: [->validation.md](./references/validation.md)