Claude Code Session Hooks: Auto-Load Context Every Time
SessionStart, SessionEnd, Setup, and PreCompact hooks for Claude Code. Auto-load context at startup and clean up on session end automatically.
Problem: Every time you start a Claude Code session, you manually remind it about your project state, environment setup, or current tasks. When sessions end, cleanup tasks are forgotten.
Quick Win: Add this SessionStart hook and Claude always knows your git state:
Now every session starts with context. Zero manual setup.
The Session Lifecycle Hooks
Four hooks control session lifecycle:
| Hook | When It Fires | Can Block? | Use Case |
|---|---|---|---|
| Setup | With --init or --maintenance | NO | One-time setup, migrations |
| SessionStart | Every session start/resume | NO | Load context, set env vars |
| PreCompact | Before context compaction | NO | Backup transcripts |
| SessionEnd | Session terminates | NO | Cleanup, logging |
SessionStart: Load Context Every Time
SessionStart fires when sessions begin or resume. Use it for context that should always be present.
Basic Context Injection
With JSON Output
For structured context injection:
SessionStart Matchers
Target specific session events:
startup- New sessionresume- From--resume,--continue, or/resumeclear- After/clearcompact- After compaction
Persist Environment Variables
SessionStart has access to CLAUDE_ENV_FILE for setting session-wide environment variables:
Variables written to CLAUDE_ENV_FILE are available in all subsequent bash commands Claude runs.
Setup: One-Time Operations
Setup hooks run only when explicitly invoked with --init, --init-only, or --maintenance. Use them for operations you don't want on every session.
When to Use Setup vs SessionStart
| Operation | Use Setup | Use SessionStart |
|---|---|---|
| Install dependencies | Yes | No |
| Run database migrations | Yes | No |
| Load git status | No | Yes |
| Set environment variables | Yes | Yes |
| Inject project context | No | Yes |
| Cleanup temp files | Yes (maintenance) | No |
Setup Configuration
Invoke with:
Setup hooks also have access to CLAUDE_ENV_FILE for persisting environment variables.
PreCompact: Before Context Loss
PreCompact fires before compaction (manual /compact or automatic when context fills).
Backup Transcripts
PreCompact Matchers
auto- Context window filled, automatic compactionmanual- User ran/compact
Create Recovery Markers
Use PreCompact with SessionStart for context recovery. See the Context Recovery Hook for the complete pattern.
SessionEnd: Cleanup
SessionEnd fires when sessions terminate. It cannot block termination but can perform cleanup.
Log Session Stats
SessionEnd Reasons
The reason field indicates why the session ended:
clear- User ran/clearlogout- User logged outprompt_input_exit- User exited while prompt was visibleother- Other exit reasons
Complete Lifecycle Example
Here's a complete lifecycle configuration:
Input Payloads
SessionStart Input
Setup Input
PreCompact Input
SessionEnd Input
Best Practices
-
Keep SessionStart fast - It runs every session. Heavy operations go in Setup.
-
Use Setup for one-time work - Dependency installation, migrations, initial setup.
-
Backup before compaction - PreCompact is your last chance to save context.
-
Log session ends - SessionEnd is useful for analytics and debugging.
-
Use matchers wisely - Different behavior for
startupvsresumevscompact.
Next Steps
- Set up the main Hooks Guide for all 12 hooks
- Configure Context Recovery for compaction survival
- Use Stop Hooks for task enforcement
- Explore Skill Activation for automatic skill loading
Last updated on