Skip to content

bug: fix inconsistent prompt_tokens normalization across Anthropic providers#1566

Open
BillionClaw wants to merge 1 commit intoPortkey-AI:mainfrom
BillionClaw:clawoss/fix/prompt-tokens-normalization-anthropic
Open

bug: fix inconsistent prompt_tokens normalization across Anthropic providers#1566
BillionClaw wants to merge 1 commit intoPortkey-AI:mainfrom
BillionClaw:clawoss/fix/prompt-tokens-normalization-anthropic

Conversation

@BillionClaw
Copy link
Copy Markdown

Issue

Fixes #1564

The prompt_tokens count was inconsistent across Anthropic providers:

  • Bedrock: Included cache tokens in prompt_tokens
  • Direct Anthropic: Did NOT include cache tokens in prompt_tokens
  • Vertex AI: Did NOT include cache tokens in prompt_tokens

Root Cause

The Bedrock provider correctly added cache_creation_input_tokens and cache_read_input_tokens to prompt_tokens, but the direct Anthropic and Vertex AI providers only used input_tokens.

Fix

Updated both the Anthropic and Vertex AI providers to include cache tokens in prompt_tokens:

prompt_tokens = input_tokens + cache_creation_input_tokens + cache_read_input_tokens

Changes Made:

  • Anthropic provider (non-streaming): Fixed prompt_tokens calculation in response transform
  • Anthropic provider (streaming): Fixed prompt_tokens in message_start event
  • Vertex AI provider (non-streaming): Fixed prompt_tokens calculation in response transform
  • Vertex AI provider (streaming): Fixed prompt_tokens in message_start event

Verification

The fix ensures consistent token counting behavior across all Anthropic provider implementations, matching the existing (correct) behavior in the Bedrock provider.


I have read and understood the CLA and agree to it.

…oviders

The Bedrock provider correctly included cache tokens in prompt_tokens, but
the direct Anthropic and Vertex AI providers did not. This caused inconsistent
behavior when using prompt caching across different Anthropic provider
implementations.

Changes:
- Anthropic provider (non-streaming): prompt_tokens now includes
  cache_creation_input_tokens + cache_read_input_tokens
- Anthropic provider (streaming): message_start event now sets prompt_tokens
  to include cache tokens
- Vertex AI provider (non-streaming): prompt_tokens now includes
  cache_creation_input_tokens + cache_read_input_tokens
- Vertex AI provider (streaming): message_start event now sets prompt_tokens
  to include cache tokens

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