Loading...
Loading...
Set up Claude Code lifecycle hooks and event handlers in settings.json. Use when you want to trigger a script on session start, run a hook before or after tool calls (PreToolUse/PostToolUse), configure hook timeouts to prevent cancellation errors, or debug hooks that aren't firing correctly.
npx skill4agent add laurigates/claude-plugins claude-hooks-configuration| Hook | Trigger | Default Timeout |
|---|---|---|
| When Claude Code session begins | 600s (command) |
| When session ends or | 600s (command) |
| When main agent stops responding | 600s (command) |
| When a subagent (Task tool) finishes | 600s (command) |
| Before a tool executes | 600s (command) |
| After a tool completes | 600s (command) |
| After a tool execution fails | 600s (command) |
| When Claude requests permission for a tool | 600s (command) |
| New git worktree created via EnterWorktree | 600s (command) |
| Worktree removed after session exits | 600s (command) |
| Teammate in agent team goes idle | 600s (command) |
| Task in shared task list marked complete | 600s (command) |
| Claude Code settings change at runtime | 600s (command) |
commandpromptagent| Type | How It Works | Default Timeout | Use When |
|---|---|---|---|
| Runs a shell command, reads stdin, returns exit code/JSON | 600s | Deterministic rules |
| Sends hook data to an HTTPS endpoint | 30s | Remote/centralized policy |
| Single-turn LLM call, returns | 30s | Judgment on hook input data |
| Multi-turn subagent with tool access, returns | 60s | Verification needing file/tool access |
async: trueonce: trueSessionEnd hook [bash ~/.claude/session-logger.sh] failed: Hook cancelled"timeout"timeout.claude/settings.json~/.claude/settings.json<project>/.claude/settings.json{
"hooks": {
"SessionEnd": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "~/.claude/session-logger.sh",
"timeout": 120
}
]
}
],
"SessionStart": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "~/.claude/session-setup.sh",
"timeout": 180
}
]
}
],
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "~/.claude/stop-hook-git-check.sh",
"timeout": 30
}
]
}
]
}
}| Hook Type | Recommended Timeout | Use Case |
|---|---|---|
| SessionStart | 120–300s | Tests, linters, dependency checks |
| SessionEnd | 60–120s | Logging, cleanup, state saving |
| Stop / SubagentStop | 30–60s | Git status checks, quick validations |
| PreToolUse | 10–30s | Quick validations |
| PostToolUse | 30–120s | Logging, notifications |
| PermissionRequest | 5–15s | Keep fast for good UX |
#!/bin/bash
# ~/.claude/session-logger.sh
# Exits instantly, work continues in background
(
# All slow operations go here
echo "$(date): Session ended" >> ~/.claude/session.log
curl -s -X POST "https://api.example.com/log" -d "session_end=$(date)"
# Any other slow work...
) &>/dev/null &
exit 0( )&&>/dev/nullexit 0| Approach | Portability | Speed | Notes |
|---|---|---|---|
| bash, zsh, sh | Instant | Recommended |
| Bash-only | Instant | Not POSIX |
| POSIX | Slight overhead | Overkill for hooks |
cat ~/.claude/settings.json | jq '.hooks'
# Edit to add "timeout": <seconds> to each hook| Optimization | Pattern |
|---|---|
| Background subshell | |
| Fast test modes | |
| Skip heavy operations | Conditional execution |
| Parallel execution | Use |
[WARN] - (starship::utils): Executing command "...node" timed out.~/.config/starship.tomlcommand_timeout = 1000 # 1 second (default is 500ms)[nodejs]
disabled = false
detect_files = ["package.json"] # Skip .nvmrc to speed up detection
[command]
command_timeout = 2000 # Increase if still timing out| Context | Command |
|---|---|
| View hooks config | |
| Test hook script | |
| Find slow operations | |
| Check starship config | |
| Setting | Location | Default |
|---|---|---|
| Hook timeout | | 10 minutes (600s) since 2.1.50 |
| Starship timeout | | 500ms |
| Node detection | | Auto |
| Error | Cause | Fix |
|---|---|---|
| Hook cancelled | Timeout exceeded | Add explicit |
| Hook failed | Script error | Check exit code, add error handling |
| Command not found | Missing script | Verify script path and permissions |
| Permission denied | Script not executable | |
( ) &>/dev/null &exit 0timeouttime bash ~/.claude/script.sh&>/dev/null/hooks