gateway icon indicating copy to clipboard operation
gateway copied to clipboard

fix: normalize prompt_tokens to include cache tokens across all Anthropic providers

Open Yanhu007 opened this issue 7 months ago • 1 comments

Summary

Fixes #1564

prompt_tokens normalization is inconsistent across Anthropic access paths:

Provider prompt_tokens includes cache?
Anthropic direct ❌ No (input_tokens only)
Vertex AI ❌ No (input_tokens only)
Bedrock ✅ Yes (inputTokens + cache*)

Per OpenAI convention, prompt_tokens should include cached tokens, with breakdown in prompt_tokens_details.cached_tokens.

Changes

Updated 4 code paths (non-streaming + streaming × 2 providers):

Anthropic direct (src/providers/anthropic/chatComplete.ts):

// Before
prompt_tokens: input_tokens

// After
prompt_tokens: input_tokens + (cache_creation_input_tokens ?? 0) + (cache_read_input_tokens ?? 0)

Vertex AI (src/providers/google-vertex-ai/chatComplete.ts): same change.

Both non-streaming and streaming paths updated. total_tokens was already correct.

Yanhu007 avatar Apr 12 '26 10:04 Yanhu007

We’ve encountered this issue on our end and would greatly appreciate it if someone could share the expected ETA for when this PR will be merged. This information will help us avoid adding unnecessary complexity on our end.

Thank you so much for your assistance!

ddg1508 avatar May 11 '26 12:05 ddg1508