[Feature Request] Add automatic cost reporting on session exit
Bug Description
Claude should have a way to report /cost at the end of a session or session exit regardless of exit strategy.
⏺ Unfortunately, it doesn't look like there's a built-in way to have Claude automatically run /cost before exiting. The SessionEnd hook runs after the
session ends, so the session state (where /cost gets its data) is already gone.
Your options are:
- Manual /cost before exit - Just run /cost manually before you exit each session (what you're already doing)
- Shell alias - Create a shell alias that runs /cost then exits:
alias cexit='claude cost && exit'
- SessionEnd hook with transcript parsing - The script I tried to create would parse the transcript file to calculate costs, but this would be an
approximation based on parsing the JSONL usage data
- Feature request - This seems like a legitimate feature gap - Claude Code should probably print cost summary automatically on exit, similar to how
many CLI tools print summary statistics
The fundamental issue is that /cost relies on in-memory session state that's cleaned up before SessionEnd hooks run. There's no "BeforeSessionEnd"
hook that would allow capturing this data while it's still available.
This is what claude tried^^
Environment Info
- Platform: darwin
- Terminal: iTerm.app
- Version: 2.0.19
- Feedback ID: a4017781-d2dc-491d-b86a-4f0a94c65b1c
Errors
[{"error":"SyntaxError: Unexpected token '`', \"```json\n{\n\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:62:719\n at Q (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:8:15093)\n at Pi2 (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:1056:20444)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","timestamp":"2025-10-16T16:37:08.166Z"},{"error":"SyntaxError: Unexpected token '`', \"```json\n{\n\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:62:719\n at Q (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:8:15093)\n at Pi2 (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:1056:20444)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","timestamp":"2025-10-16T17:12:40.486Z"},{"error":"AxiosError: Request failed with status code 404\n at MO (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:24:1136)\n at IncomingMessage.<anonymous> (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:29:9845)\n at IncomingMessage.emit (node:events:536:35)\n at IncomingMessage.emit (node:domain:489:12)\n at endReadableNT (node:internal/streams/readable:1698:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21)\n at K31.request (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:31:2129)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async b0Q (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:2416:1062)\n at async f0Q (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:2416:2055)\n at async Object.call (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:2416:8350)\n at async hl5 (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:2574:64999)\n at async aZQ (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:2574:62053)\n at async file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:2574:61222\n at async nl2 (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:724:1644)\n at async fl5 (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:2574:61134)","timestamp":"2025-10-16T17:14:42.007Z"},{"error":"SyntaxError: Unexpected token '`', \"```json\n{\n\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:62:719\n at Q (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:8:15093)\n at Pi2 (file:///Users/harrison.katz/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:1056:20444)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","timestamp":"2025-10-16T17:16:27.443Z"}]This issue has 11 comments on GitHub. Read the full discussion on GitHub ↗