Skip to content

Cost calculation of aisdk from @posthog/ai is not cache aware #3022

@liweizhi2109

Description

@liweizhi2109

Is your feature request related to a problem?

Cost calculation in sendEventToPosthog
Costs are calculated as inputCostUSD = inputCost * inputTokens and outputCostUSD = outputCost * outputTokens without subtracting cache

Example:
Image

The displayed cost is $0.0875765
But it should be

prompt_total = 21561
output_total = 139
cache_read = 17399
cache_write = 4157

# pricing reference: https://models.dev/?search=claude-sonnet-4-5%4020250929
price_input_per_1m = 3.0
price_output_per_1m = 5.0
price_read_per_1m = 0.3
price_write_per_1m = 3.75

# Assumption: Prompt total includes cache read and write tokens.
# Remainder is charged at standard input price.
standard_input_tokens = prompt_total - (cache_read + cache_write)

cost_standard = (standard_input_tokens / 1_000_000) * price_input_per_1m
cost_read = (cache_read / 1_000_000) * price_read_per_1m
cost_write = (cache_write / 1_000_000) * price_write_per_1m
cost_output = (output_total / 1_000_000) * price_output_per_1m

total_cost = cost_standard + cost_read + cost_write + cost_output

print(f"{standard_input_tokens=}")
print(f"{cost_standard=}")
print(f"{cost_read=}")
print(f"{cost_write=}")
print(f"{cost_output=}")
print(f"{total_cost=}")
standard_input_tokens=5
cost_standard=1.5000000000000002e-05
cost_read=0.0052197
cost_write=0.01558875
cost_output=0.000695
total_cost=0.02151845

Describe the solution you'd like

Describe alternatives you've considered

Related sub-libraries

  • All of them
  • posthog-js (web)
  • posthog-js-lite (web lite)
  • posthog-node
  • posthog-react-native
  • @posthog/react
  • @posthog/ai
  • @posthog/nextjs-config
  • @posthog/nuxt
  • @posthog/rollup-plugin
  • @posthog/webpack-plugin

Additional context

Thank you for your feature request – we love each and every one!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions