hook-authoring
Included with Lifetime
$97 forever
Guide creating Claude Code hooks with security-first design. Use for validation and enforcement.
hook-developmenthookssdksecurityperformanceautomationvalidationscripts
What this skill does
## Table of Contents
- [Overview](#overview)
- [Key Capabilities](#key-capabilities)
- [Quick Start](#quick-start)
- [Your First Hook (JSON - Claude Code)](#your-first-hook-json-claude-code)
- [Your First Hook (Python - Claude Agent SDK)](#your-first-hook-python-claude-agent-sdk)
- [Hook Event Types](#hook-event-types)
- [Claude Code vs SDK](#claude-code-vs-sdk)
- [JSON Hooks (Claude Code)](#json-hooks-claude-code)
- [Python SDK Hooks](#python-sdk-hooks)
- [Security Essentials](#security-essentials)
- [Critical Security Rules](#critical-security-rules)
- [Example: Secure Logging Hook](#example-secure-logging-hook)
- [Performance Guidelines](#performance-guidelines)
- [Performance Best Practices](#performance-best-practices)
- [Example: Efficient Hook](#example-efficient-hook)
- [Scope Selection](#scope-selection)
- [Decision Framework](#decision-framework)
- [Scope Comparison](#scope-comparison)
- [Common Patterns](#common-patterns)
- [Validation Hook](#validation-hook)
- [Logging Hook](#logging-hook)
- [Context Injection Hook](#context-injection-hook)
- [Testing Hooks](#testing-hooks)
- [Unit Testing](#unit-testing)
- [Module References](#module-references)
- [Tools](#tools)
- [Related Skills](#related-skills)
- [Next Steps](#next-steps)
- [References](#references)
# Hook Authoring Guide
## Overview
Hooks are event interceptors that allow you to extend Claude Code and Claude Agent SDK behavior by executing custom logic at specific points in the agent lifecycle. They enable validation before tool use, logging after actions, context injection, workflow automation, and security enforcement.
This skill teaches you how to write effective, secure, and performant hooks for both declarative JSON (Claude Code) and programmatic Python (Claude Agent SDK) use cases.
### Key Capabilities
- **PreToolUse**: Validate, filter, or transform tool inputs before execution; inject context (2.1.9+)
- **PostToolUse**: Log, analyze, or modify tool outputs after execution
- **UserPromptSubmit**: Inject context or filter user messages before processing
- **Stop/SubagentStop**: Cleanup, final reporting, or result aggregation
- **TeammateIdle/TaskCompleted**: Multi-agent coordination and orchestration (2.1.33+)
- **PreCompact**: State preservation before context window compaction
> **New in 2.1.9**: PreToolUse hooks can now return `additionalContext` to inject information before a tool executes. This enables patterns like cache hints, security warnings, or relevant context injection.
## Quick Start
### Your First Hook (JSON - Claude Code)
Create a simple logging hook in `.claude/settings.json`:
```json
{
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "echo \"$(date): Executed $CLAUDE_TOOL_NAME\" >> ~/.claude/audit.log"
}]
}
]
}
```
**Note**: Use string matchers (`"Bash"`) not object matchers (`{"toolName": "Bash"}`).
**Verification:** Run the command with `--help` flag to verify availability.
This logs every Bash command execution with a timestamp.
### Your First Hook (Python - Claude Agent SDK)
Create a validation hook using the SDK:
```python
from claude_agent_sdk import AgentHooks
class ValidationHooks(AgentHooks):
async def on_pre_tool_use(self, tool_name: str, tool_input: dict) -> dict | None:
"""Validate tool inputs before execution."""
if tool_name == "Bash":
command = tool_input.get("command", "")
if "rm -rf /" in command:
raise ValueError("Dangerous command blocked by hook")
# Return None to proceed unchanged, or modified dict to transform
return None
```
**Verification:** Run the command with `--help` flag to verify availability.
## Hook Event Types
Quick reference for all supported hook events:
| Event | Trigger Point | Parameters | Common Use Cases |
|-------|--------------|------------|------------------|
| **PreToolUse** | Before tool execution | `tool_name`, `tool_input` | Validation, filtering, input transformation |
| **PostToolUse** | After tool execution | `tool_name`, `tool_input`, `tool_output` | Logging, metrics, output transformation |
| **UserPromptSubmit** | User sends message | `message` | Context injection, content filtering |
| **PermissionRequest** | Permission dialog shown | `tool_name`, `tool_input` | Auto-approve/deny with custom logic |
| **Notification** | Claude Code sends notification | `message` | Custom notification handling |
| **Stop** | Agent completes | `reason`, `result` | Final cleanup, summary reports |
| **SubagentStop** | Subagent completes | `subagent_id`, `result` | Result processing, aggregation |
| **TeammateIdle** | Teammate agent becomes idle | `agent_id`, `session_id` | Work assignment, load balancing (2.1.33+) |
| **TaskCompleted** | Task finishes execution | `task_id`, `result` | Coordination, chaining, reporting (2.1.33+) |
| **PreCompact** | Before context compact | `context_size` | State preservation, checkpointing |
| **SessionStart** | Session starts/resumes | `session_id`, `source`, `agent_type` | Initialization, context loading |
| **SessionEnd** | Session terminates | `session_id` | Cleanup, final logging |
| **WorktreeCreate** | Agent worktree created | `worktree_path`, `session_id` | Custom VCS setup, symlink .venv, pre-populate caches (2.1.50+) |
| **WorktreeRemove** | Agent worktree removed | `worktree_path`, `session_id` | Cleanup temp files, teardown worktree-scoped resources (2.1.50+) |
### SessionStart Input Schema (Claude Code 2.1.2+)
The SessionStart hook receives JSON input via stdin with these fields:
```json
{
"session_id": "abc123",
"source": "startup",
"agent_type": "my-agent"
}
```
Fields: `source` is one of `"startup"`, `"resume"`, `"clear"`, or `"compact"`. `agent_type` is populated when the `--agent` flag is used.
**`agent_type` field**: When Claude Code is launched with `--agent my-agent`, this field contains the agent name, enabling agent-specific initialization:
```python
# Python example: Agent-aware SessionStart hook
input_data = json.loads(sys.stdin.read())
agent_type = input_data.get("agent_type", "")
if agent_type in ["code-reviewer", "quick-query"]:
# Skip heavy context injection for lightweight agents
print(json.dumps({"hookSpecificOutput": {"additionalContext": "Minimal context"}}))
else:
# Full initialization for implementation agents
print(json.dumps({"hookSpecificOutput": {"additionalContext": full_context}}))
```
```bash
# Bash example: Agent-aware SessionStart hook
HOOK_INPUT=$(cat)
AGENT_TYPE=$(echo "$HOOK_INPUT" | jq -r '.agent_type // empty')
case "$AGENT_TYPE" in
code-reviewer|quick-query)
echo '{"hookSpecificOutput": {"additionalContext": "Minimal context"}}'
;;
*)
echo '{"hookSpecificOutput": {"additionalContext": "Full context"}}'
;;
esac
```
## Hooks in Frontmatter (Claude Code 2.1.0+)
**New in 2.1.0:** Define hooks directly in skill, command, or agent frontmatter. These hooks are scoped to the component's lifecycle.
### Skill/Command/Agent Frontmatter Hooks
```yaml
---
name: validated-skill
description: Skill with lifecycle hooks
hooks:
PreToolUse:
- matcher: "Bash"
command: "./validate-command.sh"
once: true # NEW: Run only once per session
- matcher: "Write|Edit"
command: "./pre-edit-check.sh"
PostToolUse:
- matcher: "Write|Edit"
command: "./format-on-save.sh"
Stop:
- command: "./cleanup-and-report.sh"
---
```
### The `once: true` Configuration
**New in 2.1.0:** Use `once: true` to execute a hook only once per session, ideal for:
- One-time setup/initialization
- Resource allocation that shouldn't repeat
- Session-level configuration
```yaml
hooks:
PreToolUse:
- matcher: "Bash"
command: "./setup-environment.sh"
once: true # Runs only on first Bash call
SessionStart:
- command: "./initialize-session.sh"
once: true # Runs only once at sessiRelated in hook-development
rule-catalog
IncludedBrowse hookify rule catalog. Use when installing pre-built rules or browsing categories. Do not use when writing custom rules; use hookify:writing-rules.
hook-development
writing-rules
IncludedCreates behavioral rules in markdown to block dangerous commands or restrict AI behavior. Use when adding safety guardrails or preventing specific commands.
hook-development