Skip to content

[Feature Request] Add automatic cost reporting on session exit #9675

Description

@hjkatz

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'
  3. 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
  4. 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"}]

Activity

  1. carschandler commented on Oct 20, 2025

    @carschandler

    This used to be a feature and was removed, just for the record.

    See #9293, #9463

  2. hydrosquall commented on Nov 3, 2025

    @hydrosquall

    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.

  3. github-actions commented on Dec 12, 2025

    @github-actions

    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.

  4. Gattocrucco commented on Dec 12, 2025

    @Gattocrucco

    Keep open

  5. igorwojda commented on Dec 12, 2025

    @igorwojda
  6. github-actions commented on Jan 14, 2026

    @github-actions

    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.

  7. marcindulak commented on Jan 14, 2026

    @marcindulak

    This issue was closed incorrectly despite recent human comments. This behavior of the bot is reported at #16497. Upvote or comment on that issue, so maybe it gets noticed.

  8. github-actions commented on Jan 22, 2026

    @github-actions

    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.

  9. marcindulak commented on Jan 23, 2026

    @marcindulak

    Ha ha! Not locked!

  10. marcindulak commented on Jan 23, 2026

    @marcindulak

    The bot fails to lock some issues due to "Requires authentication" error #16497 (comment)

  11. github-actions commented on Jan 31, 2026

    @github-actions

    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.

  12. locked as resolved and limited conversation to collaborators on Jan 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions