Lifecycle event hooks configured in ~/.claude/settings.json
that persist context and enforce quality across sessions
Active Script: ~/.claude/hooks/context-save-check.sh
linuxserver.lan — ai-servicers.com | Official Hooks Docs
A hook is a script that Claude Code runs automatically at a specific point in its lifecycle. Claude Code supports several hook types; this deck focuses on command hooks — shell scripts that receive a JSON payload on stdin and return text on stdout.
Command hooks are deterministic — unlike skills (LLM prompts) or agents (autonomous), they always execute the same logic. They can remind, block, or inject context without any AI interpretation.
Official docs: docs.anthropic.com/en/docs/claude-code/hooks
session_id — current sessioncwd — working directoryhook_event_name — which event fired/name or auto-detectedsettings.json~/.claude/skills/~/.claude/agents/Key insight: Only command hooks can stop an action before it executes. Skills advise, agents act, hooks enforce.
Claude Code exposes 19 lifecycle events across 4 categories
Stopagent finishes turnSessionStartsession beginsSessionEndsession terminatesPreCompactbefore compactionSetupinitial setupPreToolUsebefore executionPostToolUseafter successPostToolUseFailureafter failurePermissionRequestasks permissionSubagentStartspawnedSubagentStopfinishedTeammateIdleidleTaskCompletedtask doneUserPromptSubmitprompt sentNotificationnotificationInstructionsLoadedcontext loadedConfigChangeconfig updatedWorktreeCreateworktree addedWorktreeRemoveworktree removedView full payload reference → | Official docs: docs.anthropic.com/en/docs/claude-code/hooks
These 4 hooks fire at every point where knowledge could be lost
Agent finished its work. The natural moment to reflect on what changed and update docs/context/.
Subagents (Explore, custom agents) often make significant changes. Their work must be captured before the parent continues.
Compaction erases conversation history to reclaim context window. Last chance to save context before it's gone.
Session is terminating (exit, clear, logout). Absolute last opportunity to persist anything. No second chances.
The hook script uses git diff to determine reminder intensity
No output. Context docs already updated, or stop_hook_active is true. Also silent outside git repos.
Trigger: docs/context/ changed in diff
Gentle nudge. No code changes or <15 lines of diff.
"If your changes affect architecture, security, or operations, update docs/context/."
Explicit file list. 15+ lines of diff but docs/context/ untouched.
"You made significant changes but did not update docs/context/."
Projects without docs/context/ get: "Run /context-save init to scaffold project context files"
You don't need to edit config files manually. Just tell Claude what you want.
Create a shell script in ~/.claude/hooks/. It reads JSON from stdin and prints text to stdout.
"Add a Stop hook that runs ~/.claude/hooks/my-script.sh"
Claude updates ~/.claude/settings.json for you — or use /hooks to manage interactively.
Hooks activate immediately. No restart needed. Claude runs your script at every matching event and injects the output as a system message.
Scope: User hooks (~/.claude/settings.json) apply across all projects |
Project hooks (.claude/settings.json) apply per-repo
Official docs: docs.anthropic.com/en/docs/claude-code/hooks
AI agents are stateless by default. Every new session starts from zero. The context-save skill and its 4 hooks solve this by ensuring that project knowledge is captured into files before it can be lost — whether the agent stops, a subagent finishes, the context compacts, or the session ends.
docs/context/Context-Driven Development: treat project context as a first-class artifact — committed, reviewed, and evolved alongside the code.
Files in docs/context/ — a reconstruction blueprint
/context-save init scaffolds all files from templates
Full Reference Page | ai-servicers.com