Skip to content

[FEATURE] Pre-Exit Hook ( or bring us back Automatic Cost Display on Exit) #9293

@pimpin

Description

@pimpin

Preflight Checklist

  • I have searched existing requests and this feature hasn't been requested yet
  • This is a single feature request (not multiple features)

Problem Statement

Since some time, I miss the cost report on exiting. I wanted to bring it back ... but "according to Claude", currently, when exiting Claude Code, there is no way to automatically view the session cost report. Users must remember to manually run /cost before exiting, otherwise the cost information is lost when the session ends.

Proposed Solution

Add one of the following options:

Option 1: Pre-Exit Hook (Preferred)

Add a new hook event PreExit or Exit that runs before the Claude Code session terminates. This would allow users to execute commands like /cost automatically.

Example configuration:

{
  "hooks": {
    "PreExit": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "echo 'Session Cost Summary:' && claude-code /cost"
          }
        ]
      }
    ]
  }
}

Alternative Solutions

Auto-Display Cost Setting

Add a setting to automatically display the cost report when exiting to bring it back as it was few times ago :

{
  "displayCostOnExit": true
}

Priority

Critical - Blocking my work

Feature Category

CLI commands and flags

Use Case Example

As a user who tracks AI usage costs, I want to:

  • Automatically see my session costs when I exit Claude Code
  • Keep a record of costs without having to remember to run /cost manually
  • Optionally save cost reports to a log file for tracking purposes

Additional Context

  • The existing Stop hook runs after each agent response, not on application exit
  • Currently, there is no hook that runs when the user exits the CLI
  • Cost data is only available during an active session, making it impossible to retrieve after exit

Alternatives Considered

  1. Shell alias wrapper - Not viable because cost data is unavailable after session ends
  2. Stop hook - Not suitable as it runs after every response, not on exit
  3. Manual /cost before exit - Current workaround, but easy to forget

Benefits

  • Improved user experience for cost-conscious users
  • Better cost tracking and budgeting capabilities
  • Consistent with user expectations for session summary information
  • Could be extended to show other session statistics (duration, token usage, etc.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions