Skip to content

[Feature Request] Support historical pricing for accurate cost calculation #764

Description

@sunmingyang666

Problem

Currently, ccusage fetches current prices from LiteLLM API and applies them to all historical data. However, Claude API pricing changes over time, which leads to inaccurate cost calculations for past usage.

For example:

  • Claude 3.5 Sonnet was priced at $3/$15 per 1M tokens
  • Claude 4 Sonnet is now priced at $3/$15 per 1M tokens (same, but models change)
  • Older models like Claude 3 Opus had different pricing
  • If pricing changes in the future, running ccusage monthly will show incorrect costs for historical months

The reported costs won't reflect what was actually charged at the time of usage.

Background

From #4, I understand that:

  • Claude Code 1.0.9+ no longer provides the costUSD field in logs
  • ccusage now calculates costs using: tokens × current_price
  • Prices are fetched from LiteLLM API at runtime

This works well for current usage, but becomes inaccurate for historical data when prices change.

Suggested Solution

Maintain a historical pricing table that maps date ranges to prices:

const historicalPricing = [
  { 
    model: "claude-sonnet-4-20250514", 
    validFrom: "2025-05-14", 
    validTo: null, 
    input: 3, 
    output: 15,
    cacheCreation: 3.75,
    cacheRead: 0.30
  },
  { 
    model: "claude-3-5-sonnet-20241022", 
    validFrom: "2024-10-22", 
    validTo: "2025-05-13", 
    input: 3, 
    output: 15,
    cacheCreation: 3.75,
    cacheRead: 0.30
  },
  // ... older prices
];

When calculating costs, look up the price that was in effect on the date of each log entry.

Alternatives

  1. Use Anthropic's Usage and Cost API (related to New Usage and Cost API #610) - fetch actual billed costs instead of calculating
  2. Record total_cost_usd from statusline (related to [suggestion]: extracting total_cost_usd, record it, and use it for other commands (daily, monthly...) #576) - only solves future data
  3. Add --use-current-prices flag - explicitly opt into current pricing, default to historical
  4. Show warning - when displaying costs for periods where pricing may have changed

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions