Skip to content

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

Open
Yanhu007 wants to merge 1 commit intoPortkey-AI:mainfrom
Yanhu007:fix/anthropic-prompt-tokens-include-cache
Open

fix: normalize prompt_tokens to include cache tokens across all Anthropic providers#1602
Yanhu007 wants to merge 1 commit intoPortkey-AI:mainfrom
Yanhu007:fix/anthropic-prompt-tokens-include-cache

Conversation

@Yanhu007
Copy link
Copy Markdown

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.

…opic providers

Anthropic direct and Vertex AI set prompt_tokens = input_tokens
(excluding cache), while Bedrock includes cache tokens. This
inconsistency means the same model with the same prompt reports
different prompt_tokens depending on which provider is used.

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

Add cache_creation_input_tokens and cache_read_input_tokens to
prompt_tokens in both non-streaming and streaming paths for:
- Anthropic direct (chatComplete.ts)
- Google Vertex AI (chatComplete.ts)

This makes them consistent with Bedrock's existing behavior.

Fixes Portkey-AI#1564
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.

bug: Inconsistent prompt_tokens normalization across Anthropic providers (direct API vs Vertex AI vs Bedrock)

1 participant