[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 used to be a feature and was removed, just for the record.
See https://github.com/anthropics/claude-code/issues/9293, https://github.com/anthropics/claude-code/issues/9463
As a temporary workaround, I've added a cost / token usage indicator to my statusline as a replacement for the old functionality.
It's not ideal since I don't actually need this info on every command, but I definitely want to know about my usage when each session ends as it gives me a sense of both my spend + prompting efficiency.
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
Keep open
A nice workaround - use this tool https://github.com/Maciek-roboblog/Claude-Code-Usage-Monitor