Agent SDK Hook Design
When to invoke
- Adding a new PreToolUse or PostToolUse hook to a Claude Agent SDK session.
- A hook is firing on the wrong events or causing unintended blocks.
- Designing the hook's advisory-vs-blocking posture.
- Reviewing a hook handler for safety and idempotency.
Hook event map
| Event |
Fires |
Typical use |
PreToolUse |
Before a tool call is executed |
Validate args, block dangerous calls, log intent |
PostToolUse |
After a tool returns |
Redact PII from results, log outcome, rate-limit |
SessionStart |
When a new agent session opens |
Inject context, warm up state, validate auth |
Stop |
When the agent concludes (any reason) |
Audit log, cleanup ephemeral resources |
Step 1 — Choose advisory vs blocking
Decision: should the hook STOP execution on a violation?
├── YES (blocking) — exit with a non-zero code / raise + return deny verdict
│ Use for: security controls, destructive-action gates, required-field validation
│ Rule: blocking hooks must fail FAST (< 200 ms); never block on network I/O
└── NO (advisory) — log to stderr and exit 0
Use for: cost warnings, style guidance, telemetry
Rule: advisory hooks must NEVER cause data loss if ignored
Step 2 — Write the handler contract
Every hook handler must state:
- Scope — which tool names (or
*) it fires on.
- Input — what fields from the tool-call payload it reads.
- Output — allow (exit 0) / deny (exit non-zero + message) / annotate (exit 0 + JSON to stderr).
- Latency budget — for blocking hooks: target < 100 ms; hard ceiling 500 ms.
- Failure mode — if the handler itself errors, does it allow or deny? Default: allow (fail-open) unless the hook is a security gate (then fail-closed).
# Example: PreToolUse blocking hook — deny file writes outside /tmp
import json, sys
payload = json.load(sys.stdin)
tool_name = payload.get("tool", {}).get("name", "")
tool_input = payload.get("tool", {}).get("input", {})
if tool_name == "write_file":
path = tool_input.get("path", "")
if not path.startswith("/tmp/"):
print(json.dumps({"decision": "deny",
"reason": f"write_file outside /tmp is not allowed: {path}"}))
sys.exit(1)
sys.exit(0)
Step 3 — Scope narrowly
| Anti-pattern |
Fix |
Hook fires on * (all tools) for a write-specific check |
Scope to write_file, edit_file, etc. |
| Hook reads the full message history on every call |
Read only tool.input; access history only in SessionStart |
| PostToolUse hook mutates the tool result |
Return annotated metadata to stderr; never mutate — the model already processed the result |
| Hook makes a synchronous HTTP call in the hot path |
Cache, pre-fetch in SessionStart, or make the hook advisory |
Step 4 — Idempotency and re-entry
Hooks may fire multiple times on retried tool calls. Design for idempotency:
- Log with a deduplication key (session ID + tool call ID).
- If the hook writes to an external system, use upsert semantics.
- For rate-limit hooks: track counts in a shared store keyed by session, not per-invocation.
Hook testing checklist
Pitfalls
- A blocking hook with a database lookup in the hot path — one slow query serialises every tool call.
- Using
PostToolUse to decide whether to allow a tool call — the tool already executed; PostToolUse cannot undo writes.
- Registering the same hook twice (in both the plugin's
hooks.json and the session-level config) — both fire; effects double.
- Forgetting that
PreToolUse for computer-use tools fires before the screen action — keep blocking logic tight or you'll desynchronise the computer-use state machine.
1---2name: agent-sdk-hook-design3description: "Playbook for designing Claude Agent SDK hooks (PreToolUse, PostToolUse, SessionStart, Stop) — selecting the right event, writing the handler contract, deciding advisory vs blocking behaviour, and avoiding the four common hook anti-patterns. Owned by agent-sdk-engineer."4metadata:5 source: https://skillmd.com/skills/mcorbett51090/agent-sdk-hook-design6---78# Agent SDK Hook Design910## When to invoke1112- Adding a new PreToolUse or PostToolUse hook to a Claude Agent SDK session.13- A hook is firing on the wrong events or causing unintended blocks.14- Designing the hook's advisory-vs-blocking posture.15- Reviewing a hook handler for safety and idempotency.1617## Hook event map1819| Event | Fires | Typical use |20|---|---|---|21| `PreToolUse` | Before a tool call is executed | Validate args, block dangerous calls, log intent |22| `PostToolUse` | After a tool returns | Redact PII from results, log outcome, rate-limit |23| `SessionStart` | When a new agent session opens | Inject context, warm up state, validate auth |24| `Stop` | When the agent concludes (any reason) | Audit log, cleanup ephemeral resources |2526## Step 1 — Choose advisory vs blocking2728```29Decision: should the hook STOP execution on a violation?30├── YES (blocking) — exit with a non-zero code / raise + return deny verdict31│ Use for: security controls, destructive-action gates, required-field validation32│ Rule: blocking hooks must fail FAST (< 200 ms); never block on network I/O33└── NO (advisory) — log to stderr and exit 034 Use for: cost warnings, style guidance, telemetry35 Rule: advisory hooks must NEVER cause data loss if ignored36```3738## Step 2 — Write the handler contract3940Every hook handler must state:41421. **Scope** — which tool names (or `*`) it fires on.432. **Input** — what fields from the tool-call payload it reads.443. **Output** — allow (exit 0) / deny (exit non-zero + message) / annotate (exit 0 + JSON to stderr).454. **Latency budget** — for blocking hooks: target < 100 ms; hard ceiling 500 ms.465. **Failure mode** — if the handler itself errors, does it allow or deny? Default: allow (fail-open) unless the hook is a security gate (then fail-closed).4748```python49# Example: PreToolUse blocking hook — deny file writes outside /tmp50import json, sys5152payload = json.load(sys.stdin)53tool_name = payload.get("tool", {}).get("name", "")54tool_input = payload.get("tool", {}).get("input", {})5556if tool_name == "write_file":57 path = tool_input.get("path", "")58 if not path.startswith("/tmp/"):59 print(json.dumps({"decision": "deny",60 "reason": f"write_file outside /tmp is not allowed: {path}"}))61 sys.exit(1)6263sys.exit(0)64```6566## Step 3 — Scope narrowly6768| Anti-pattern | Fix |69|---|---|70| Hook fires on `*` (all tools) for a write-specific check | Scope to `write_file`, `edit_file`, etc. |71| Hook reads the full message history on every call | Read only `tool.input`; access history only in `SessionStart` |72| PostToolUse hook mutates the tool result | Return annotated metadata to stderr; never mutate — the model already processed the result |73| Hook makes a synchronous HTTP call in the hot path | Cache, pre-fetch in `SessionStart`, or make the hook advisory |7475## Step 4 — Idempotency and re-entry7677Hooks may fire multiple times on retried tool calls. Design for idempotency:7879- Log with a deduplication key (session ID + tool call ID).80- If the hook writes to an external system, use upsert semantics.81- For rate-limit hooks: track counts in a shared store keyed by session, not per-invocation.8283## Hook testing checklist8485- [ ] Unit-test the handler with a synthetic `payload.json` — both allow and deny branches.86- [ ] Confirm the hook exits within its latency budget under synthetic load.87- [ ] Test the error path: handler crashes → what does the session do?88- [ ] Verify `PreToolUse` deny message surfaces to the agent (not swallowed by the SDK runtime).89- [ ] For security-gate hooks: confirm fail-closed behaviour (an unexpected exception should deny, not allow).9091## Pitfalls9293- A blocking hook with a database lookup in the hot path — one slow query serialises every tool call.94- Using `PostToolUse` to decide whether to allow a tool call — the tool already executed; `PostToolUse` cannot undo writes.95- Registering the same hook twice (in both the plugin's `hooks.json` and the session-level config) — both fire; effects double.96- Forgetting that `PreToolUse` for computer-use tools fires *before* the screen action — keep blocking logic tight or you'll desynchronise the computer-use state machine.