Browse
Core Concepts
Reasoning
Memory & Retrieval
Agent Types
Design Patterns
Training & Alignment
Frameworks
Tools
Safety
Meta
Browse
Core Concepts
Reasoning
Memory & Retrieval
Agent Types
Design Patterns
Training & Alignment
Frameworks
Tools
Safety
Meta
Claude Code Lifecycle Hooks refer to a system of automatic event capture triggers that fire at specific points during the execution of Claude-based agent systems. These hooks enable developers to monitor, log, and respond to state changes throughout the lifecycle of an agent session, providing granular visibility and control over agent behavior and resource management.
Claude Code Lifecycle Hooks represent a structured approach to instrumenting agent execution pipelines. The system provides twelve distinct hook points that capture different phases of agent operation, from session initialization through termination 1). Each hook functions as an event trigger that activates when specific conditions occur during agent execution, enabling developers to implement custom logic at predefined moments in the agent lifecycle.
The architecture employs Node.js scripts as the primary implementation mechanism. When a lifecycle event occurs, the corresponding hook fires and passes event data as JSON to stdin, which the Node script processes 2). The script then communicates with a local REST API via POST requests, enabling integration with external systems, logging infrastructure, or agent memory systems.
The lifecycle hook system comprises twelve distinct triggering points across the agent execution pipeline:
Session Management Hooks: The SessionStart hook activates when an agent session begins, allowing initialization of session-specific resources, context, or memory structures. The SessionEnd hook fires upon session termination, enabling cleanup operations, final logging, or persistent storage of session artifacts.
User Interaction Hooks: The UserPromptSubmit hook captures when a user submits a prompt to the agent, providing an opportunity to preprocess input, validate queries, or update user interaction logs before the agent begins processing.
Tool Integration Hooks: The PreToolUse hook fires immediately before the agent invokes an external tool or function, enabling validation of tool calls, parameter verification, or execution planning. The PostToolUse hook activates after successful tool execution, allowing result logging, caching, or downstream processing. The PostToolUseFailure hook captures tool execution failures, enabling error recovery logic, retry mechanisms, or failure analysis 3).
Resource Management Hooks: The PreCompact hook activates before context compression or memory optimization operations, allowing preparation for state reduction. The Notification hook provides a general-purpose event trigger for custom notifications or status updates during execution.
Subagent Coordination Hooks: The SubagentStart hook fires when a subagent or delegated process begins execution, enabling tracking of hierarchical agent behavior. The SubagentStop hook captures subagent termination, allowing consolidation of subagent results and state management 4).
Completion Hooks: The TaskCompleted hook fires when an agent successfully completes an assigned task, enabling result recording and downstream processing. The Stop hook captures explicit termination commands or completion signals, providing a final opportunity for cleanup before session closure.
Each lifecycle hook operates through a standardized implementation pattern. When a triggering event occurs, the hook execution environment serializes relevant context data as JSON. This JSON payload includes event metadata, agent state information, tool parameters (if applicable), execution timing, and any error details 5). The JSON-formatted stdin is then piped to a Node.js script specified for that particular hook.
The Node.js handler script reads the JSON input from stdin, parses the event data, and executes custom logic as needed. This might include database logging, metric collection, memory updates, or external API calls. Communication with persistent systems occurs through POST requests to a local REST API endpoint, which the agent system or external service provides. This architectural choice enables loose coupling between hook handlers and the agent core system while maintaining synchronous execution semantics where necessary.
Lifecycle hooks enable several important developer capabilities. Observability and Monitoring: Hook scripts can log all agent activities to centralized monitoring systems, providing complete audit trails and performance metrics. Memory and Context Management: Hooks can update persistent memory systems (such as agent memory databases) at appropriate lifecycle points, ensuring that learned information or session context persists across sessions. Error Recovery and Retry Logic: Failed tool invocations trigger PostToolUseFailure hooks, which can implement sophisticated retry strategies, fallback mechanisms, or error escalation.
Agent Coordination: Subagent lifecycle hooks enable tracking of hierarchical agent behavior, resource allocation between agents, and coordination of parallel agent execution. Session Optimization: PreCompact hooks enable optimization routines that compress or reorganize agent context before critical memory constraints are reached.