Skip to content

fix: correct Anthropic provider inputOther token counting#2

Open
hobostay wants to merge 1 commit into
MoonshotAI:mainfrom
hobostay:fix/anthropic-token-counting
Open

fix: correct Anthropic provider inputOther token counting#2
hobostay wants to merge 1 commit into
MoonshotAI:mainfrom
hobostay:fix/anthropic-token-counting

Conversation

@hobostay
Copy link
Copy Markdown

Summary

  • inputOther was set to raw input_tokens which already includes cache tokens, causing double-counting of non-cache input tokens
  • Subtract cache_read_input_tokens and cache_creation_input_tokens from the total, matching the OpenAI provider's calculation pattern (promptTokens - cached)

Root Cause

In packages/kosong/src/providers/anthropic.ts, _extractUsage assigned usage.input_tokens directly to inputOther. Per the Anthropic API, input_tokens is the total billed input tokens which already includes cache-read and cache-creation tokens. This inflated the reported non-cache input count.

Test plan

  • Verify existing Anthropic provider tests still pass
  • Confirm inputOther + inputCacheRead + inputCacheCreation now sums correctly to input_tokens

🤖 Generated with Claude Code

inputOther was set to raw input_tokens which already includes cache
tokens, causing double-counting. Subtract cache_read_input_tokens and
cache_creation_input_tokens to get the actual non-cache input count,
matching the OpenAI provider's calculation pattern.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant