Feature Request: PreSessionEnd Hook
Feature Request: PreSessionEnd Hook
Repository: https://github.com/anthropics/claude-code/issues
---
Summary
Request for a PreSessionEnd hook that fires when the user signals exit, but before Claude terminates — enabling Claude-dependent operations as part of the exit flow.
The Problem
The current SessionEnd hook runs after Claude has already exited. Any operation requiring Claude's participation is impossible at that point.
Current flow:
User: /exit or Ctrl-D
→ Claude exits immediately
→ SessionEnd hook fires (Claude is gone)
→ Hook can only run shell commands — Claude cannot participate
Desired flow:
User: /exit or Ctrl-D
→ PreSessionEnd hook fires (Claude still active)
→ Claude executes any configured prompt/operation
→ Claude exits
→ SessionEnd hook fires for post-exit tasks
The Architectural Gap
The hooks system currently covers:
- SessionStart — before Claude is ready (injection)
- PostToolUse — during session (validation, logging)
- SessionEnd — after Claude exits (cleanup)
What's missing: a hook at the moment the user signals "I'm done" but Claude is still active. This is the only point where Claude can participate in exit-related operations.
Use Case
I've built a custom context persistence system:
- SessionStart hook: Decrypts and injects saved context
- SessionEnd hook: Encrypts context file for storage
- Custom
/save-contextskill: Claude summarises session → writes to.claude/context.md
The gap: The encryption hook runs after Claude exits, but the context generation requires Claude to be active. Currently I must manually run /save-context before exiting. If I forget, there's nothing to encrypt.
This isn't specific to my workflow — it's the general pattern: any exit operation requiring Claude's participation has no trigger point.
Proposed Solution
A PreSessionEnd hook that fires when the user signals exit (/exit, Ctrl-D twice, etc.) but before Claude terminates.
{
"hooks": {
"PreSessionEnd": [{
"hooks": [{
"type": "prompt",
"prompt": "Summarise this session and save context to .claude/context.md"
}]
}]
}
}
Hook Types
type: "prompt"— Inject a prompt for Claude to execute before exit. This is the key capability: Claude can summarise, clean up, confirm, or run any skill.type: "command"— Run a shell command (consistent with other hooks, though less useful here since the point is Claude participation).
Behaviour
- Hook fires once per exit signal
- Claude processes the prompt normally
- After completion, exit proceeds
- If hook fails or times out, exit proceeds anyway (graceful degradation)
Workaround
I've created an /exit-save command that chains my save operation with /exit. It works, but requires discipline to use the right exit command — easy to forget and just Ctrl-D.
Impact
This fills the one timing gap in the hooks system. Users could:
- Auto-generate session summaries on exit
- Run cleanup prompts before termination
- Trigger any Claude-dependent operation as part of the exit flow
- Build more robust context persistence without manual steps
The hooks architecture is already excellent. This adds the missing lifecycle event.
---
Environment: Claude Code CLI
Related: SessionStart, SessionEnd, PostToolUse hooks
This issue has 11 comments on GitHub. Read the full discussion on GitHub ↗