fix: normalize prompt_tokens to include cache tokens across Anthropic providers#1568
Open
guoyangzhen wants to merge 1 commit intoPortkey-AI:mainfrom
Open
Conversation
… providers The OpenAI convention (which Portkey normalizes to) is that prompt_tokens includes cached tokens, with the breakdown available in prompt_tokens_details.cached_tokens. Previously, Bedrock correctly included cache tokens in prompt_tokens, but the Anthropic direct API and Vertex AI Anthropic paths did not. This caused inconsistent billing and usage tracking when the same Anthropic model was accessed through different providers. Changes: - Anthropic direct (non-stream): prompt_tokens now includes cache tokens - Anthropic direct (streaming): same fix in message_start handler - Vertex AI Anthropic (non-stream): same fix - Vertex AI Anthropic (streaming): same fix Fixes Portkey-AI#1564
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When Portkey normalizes Anthropic model responses to OpenAI schema,
prompt_tokenshas inconsistent semantics depending on which provider is used:prompt_tokensincludes cache tokens?anthropic(direct)vertex-ai(Anthropic)bedrock(Anthropic)The OpenAI convention (which Portkey normalizes to) is that
prompt_tokensincludes cached tokens, with the breakdown available inprompt_tokens_details.cached_tokens.This inconsistency means the same Anthropic model accessed through different providers reports different
prompt_tokensvalues, breaking billing and usage tracking.Fixes #1564
Solution
Normalize
prompt_tokensto always includecache_read_input_tokens + cache_creation_input_tokens, matching Bedrock's (correct) behavior and the OpenAI convention.Changes
src/providers/anthropic/chatComplete.ts:613prompt_tokensnow includes cache creation and read tokensmessage_starthandlersrc/providers/google-vertex-ai/chatComplete.ts:899Before
After
Testing
Verified the change matches Bedrock's existing behavior at
src/providers/bedrock/chatComplete.ts:551-554: