[Feature Request] Add automatic cost reporting on session exit

Status Closed — not planned
Maintainer reply None cached
Activity 11 comments · opened Oct 16, 2025 · closed Jan 14, 2026

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:

  1. Manual /cost before exit - Just run /cost manually before you exit each session (what you're already doing)
  2. Shell alias - Create a shell alias that runs /cost then exits:

alias cexit='claude cost && exit'

  1. 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

  1. 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"}]

View original on GitHub ↗

11 Comments

carschandler · 10 months ago
hydrosquall · 10 months ago

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.

github-actions[bot] · 8 months ago

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.

Gattocrucco · 8 months ago

Keep open

igorwojda · 8 months ago
github-actions[bot] · 7 months ago

This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.

marcindulak · 7 months ago

This issue was closed incorrectly despite recent human comments. This behavior of the bot is reported at https://github.com/anthropics/claude-code/issues/16497. Upvote or comment on that issue, so maybe it gets noticed.

github-actions[bot] · 7 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

marcindulak · 7 months ago

Ha ha! Not locked!

marcindulak · 7 months ago

The bot fails to lock some issues due to "Requires authentication" error https://github.com/anthropics/claude-code/issues/16497#issuecomment-3792158713

github-actions[bot] · 7 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.