Gratitude for `SessionStart` Hook & Feature Request for its Counterpart: `SessionEnd`
Title: Gratitude for SessionStart Hook & Feature Request for its Counterpart: SessionEnd
Labels: feature-request, enhancement, hooks
Hi Anthropic Team,
First and foremost, a huge thank you for implementing the SessionStart hook in v1.0.62! As someone who was eagerly anticipating this feature, I was thrilled to see it in the changelog. The implementation is even better than I had hoped for—the inclusion of matchers for startup, resume, and clear is a fantastic addition that provides the granular control needed for sophisticated workflows. It perfectly solves the "session setup" half of the equation.
This brings me to the second half: session teardown. While SessionStart allows us to automate initialization, we currently lack a corresponding hook for cleanup, archiving, and post-session analysis.
The Feature Gap: No Clean Way to Hook into Session Termination
The current hooks system provides excellent coverage for events during a session. However, once a session ends cleanly (e.g., via Ctrl+D or /exit), there is no official mechanism to trigger automated actions.
This means that crucial teardown tasks—like archiving transcripts, logging final session metrics, or cleaning up temporary resources—must be handled by brittle external wrapper scripts. These workarounds are suboptimal because they can't access final session state (like total cost or transcript path) and can't differentiate a clean exit from a crash.
The Proposal: A SessionEnd Hook
I'd like to formally request the introduction of a SessionEnd hook. This would serve as the natural counterpart to SessionStart and complete the session lifecycle management capabilities of Claude Code.
Proposed Behavior:
- Trigger: The hook would fire upon clean session termination.
- Configuration: It would be configured in
settings.jsonalongside other top-level events likeStopandSessionStart, without amatcher. - Input (stdin): The hook script should receive a JSON payload with the final session state. This would be incredibly powerful if it included:
session_idtranscript_pathcwd- Final cost/token metrics (e.g.,
total_cost_usd,total_input_tokens,total_output_tokens) - Total session duration (
duration_ms)
Benefits and Use Cases of a SessionEnd Hook
Implementing SessionEnd would unlock a massive range of professional automation and integration workflows.
1. Comprehensive Observability and Monitoring
The existing OpenTelemetry integration is great for real-time metrics, but a SessionEnd hook provides a definitive, transactional summary of an entire session.
- Final Cost Logging: On
SessionEnd, a script could push the finaltotal_cost_usdand token counts for the session to a central cost management dashboard or an OTel collector. This is perfect for tracking per-project or per-developer spend. - Session Summary Metrics: Log a single event summarizing the entire session: total duration, number of tool calls, files edited, and final cost. This is much cleaner than trying to aggregate this from a stream of real-time events.
2. Automated Archiving and Knowledge Management
Developers generate a huge amount of valuable context during a session. SessionEnd could help capture it automatically.
- Transcript Archiving: Use the
transcript_pathto automatically copy the final conversation to a designated location like S3, a team's shared drive, or attach it to a Confluence page. - Jira/Linear Integration: A script could parse the transcript for key decisions or commit SHAs and post a summary comment on a related ticket, automatically documenting the work done.
3. State Management and Cleanup
For every setup action in SessionStart, there's often a corresponding teardown action.
- Resource Cleanup: Automatically delete temporary files, branches, or Docker containers that were spun up by a
SessionStarthook for the session. - Stop Background Processes: If a dev server or file watcher was started at the beginning of the session, the
SessionEndhook can ensure it's properly terminated. - Release Locks: If a session acquires a lock on a shared resource (e.g., a staging environment), this hook can ensure the lock is released.
4. Custom Notifications and Reporting
- End-of-Session Summary: Generate a summary of the session (e.g., features completed, bugs fixed, cost incurred) and send it via email or a Slack webhook.
- Usage Alerts: Trigger alerts if a session's cost or duration exceeds a predefined budget, helping teams manage usage proactively.
5. Seamless Git Worktree Integration
As highlighted in your common-workflows documentation, Git worktrees are a fantastic way to work in parallel. Session hooks make this workflow seamless.
SessionStart: Astartuphook runsnpm installin a new worktree.SessionEnd: A hook automatically runsgit worktree removeif the branch has been merged, keeping the repository clean.
---
Adding a SessionEnd hook would truly elevate Claude Code's capabilities for enterprise and power-user workflows. It would make the tool more autonomous, reliable, and deeply integrable into the full development lifecycle.
Thank you again for the amazing work on SessionStart. I look forward to hearing your thoughts on completing the picture with SessionEnd.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗