Hooks & Automation
Relevant source files
The following files were used as context for generating this wiki page:
- cli-tool/components/skills/development/hook-development/SKILL.md
- cli-tool/components/skills/development/plugin-structure/references/component-patterns.md
- cli-tool/components/agents/expert-advisors/modernization.md
- cli-tool/components/commands/documentation/update-docs.md
- cli-tool/components/skills/development/command-creator/references/best-practices.md
Hooks & Automation within the Claude Code ecosystem serve as event-driven mechanisms designed to validate operations, enforce project policies, and integrate external tools into the development lifecycle. These scripts execute in response to specific events such as tool usage, session transitions, or user prompts, allowing for autonomous yet controlled agent behavior.
The system facilitates a broad range of capabilities, from context loading during session startup to complex LLM-driven validation of code changes before they are committed. By utilizing both deterministic command-based hooks and reasoning-based prompt hooks, the project ensures that automation remains both efficient and context-aware.
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:10-21
Architecture and Hook Types
The automation architecture is categorized into two primary execution models: Prompt-Based Hooks and Command Hooks. This dual approach allows developers to choose between flexible reasoning and fast, deterministic validation.
Hook Execution Models
| Hook Type | Execution Method | Primary Use Case |
|---|---|---|
| Prompt-Based | LLM-driven natural language evaluation | Context-aware validation and reasoning-based decisions. |
| Command | Bash/Shell script execution | Deterministic checks, file operations, and tool integration. |
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:23-45
Hook Lifecycle
Hooks are registered during the initialization phase of the CLI tool. The system scans enabled plugins, parses definitions in hooks.json, and registers them for specific event triggers. Notably, hooks are loaded at the start of a session; any configuration changes require a session restart to take effect.
Sources: cli-tool/components/skills/development/plugin-structure/references/component-patterns.md:7-14, cli-tool/components/skills/development/hook-development/SKILL.md:316-325
The diagram shows the lifecycle from initial plugin discovery to event-driven execution.
Sources: cli-tool/components/skills/development/plugin-structure/references/component-patterns.md:7-19, cli-tool/components/skills/development/hook-development/SKILL.md:235-255
Event Triggers and Matchers
The automation system reacts to a specific set of lifecycle and operational events. Each hook configuration defines a matcher to determine which specific tools or actions trigger the automation.
Core Hook Events
| Event | Trigger Point | Use Case |
|---|---|---|
PreToolUse | Before a tool (e.g., Write, Bash) runs | Approval, denial, or modification of tool inputs. |
PostToolUse | After tool completion | Analysis of results, feedback, or logging. |
Stop | When the main agent finishes | Validating task completeness and requirements. |
SessionStart | When the CLI session begins | Loading environment variables and project context. |
UserPromptSubmit | When a user submits a prompt | Adding security warnings or context. |
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:95-155, cli-tool/components/skills/development/hook-development/SKILL.md:364-374
Matcher Syntax
Matchers support exact names, multiple tools via pipe delimiters, and wildcards.
- Exact:
"matcher": "Write" - Multiple:
"matcher": "Read|Write|Edit" - Wildcard:
"matcher": "*"(runs for all tools) - Regex:
"matcher": "mcp__.*"(runs for all Model Context Protocol tools)
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:235-257
Data Contracts and Input/Output
Hooks operate by receiving JSON via stdin and returning structured JSON to the system. This standardized contract allows hooks to communicate permission decisions and system messages back to the agent.
Input Schema
All hooks receive common fields providing session and environmental context:
session_id: Unique identifier for the current session.cwd: Current working directory of the process.tool_name/tool_input: Available duringPreToolUseandPostToolUse.user_prompt: Available duringUserPromptSubmit.
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:195-210
Output Schema
Hooks must return a JSON object to influence the agent's behavior:
{
"continue": true,
"permissionDecision": "allow|deny|ask",
"systemMessage": "Message shown to the agent",
"updatedInput": { "field": "modified_value" }
}
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:175-190, cli-tool/components/skills/development/hook-development/SKILL.md:112-117
This sequence diagram illustrates the synchronous validation flow during a PreToolUse event.
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:103-118, cli-tool/components/skills/development/hook-development/SKILL.md:175-185
Implementation Patterns
Contextual Modernization Automation
Automation is extensively used in complex workflows like project modernization. The Modernization Agent utilizes a structured 9-step workflow where automation handles file-by-file analysis and documentation generation. It tracks progress using a manage_todo_list tool, ensuring that "Deep Understanding" is achieved before moving to architectural recommendations.
Sources: cli-tool/components/agents/expert-advisors/modernization.md:19-45, cli-tool/components/agents/expert-advisors/modernization.md:105-115
Documentation Synchronization
The update-docs command provides an automation framework for keeping technical documentation in sync with implementation. It utilizes shell commands to find files, grep for status indicators (✅, ⚠️, ❌), and automatically update phase implementation documents based on recent git logs.
Sources: cli-tool/components/commands/documentation/update-docs.md:10-35, cli-tool/components/commands/documentation/update-docs.md:40-65
Development Best Practices for Hooks
- Portability: Always use
${CLAUDE_PLUGIN_ROOT}for paths to ensure hooks work across different environments. - Security: Hooks must validate inputs to prevent path traversal (e.g., checking for
..in file paths) and protect sensitive files like.env. - Parallelism: All matching hooks for a single event run in parallel and do not see each other's output.
- Environment Persistence: Use the
$CLAUDE_ENV_FILEduringSessionStartto persist variables across the entire session.
Sources: cli-tool/components/skills/development/hook-development/SKILL.md:215-225, cli-tool/components/skills/development/hook-development/SKILL.md:259-280, cli-tool/components/skills/development/hook-development/SKILL.md:300-305
Summary
The Hooks & Automation system provides a robust framework for extending the capabilities of the Claude Code environment. By integrating event-driven scripts at key lifecycle points—such as tool use, session management, and task completion—developers can enforce high standards for security, code quality, and documentation consistency. Whether through deterministic shell scripts or flexible LLM prompts, automation ensures that complex workflows like project modernization and documentation updates remain accurate and well-governed.