Skip to content

Conversation

@carlos-marchal-ph
Copy link
Contributor

Langchain internally transforms Anthropic token counts to an OpenAI like format (input tokens include in cached tokens). This was causing errors in our logic since we already undo this transformation in the plugin server. A fix for this was merged in #346.

Unfortunately the fix was too eager and it also started applying to other, non-Anthropic providers, like OpenAI itself. This causes incorrect costs (even negative ones) for generations with cache usage.

PR #346 introduced cache token subtraction for all providers, causing
double subtraction for OpenAI/OpenRouter and resulting in negative costs.

The plugin-server only subtracts cache tokens for Anthropic providers
(exact match on provider name or substring match on model name). This
fix aligns the Python SDK with that behavior.

Changes:
- Only subtract cache tokens when provider="anthropic" OR model contains "anthropic"
- Passes provider and model metadata to usage parsing functions
- Updates tests to reflect correct behavior (no subtraction for OpenAI)
- Adds test for Anthropic provider subtraction

Fixes negative cost calculations for users on OpenAI/OpenRouter with cached tokens.
@carlos-marchal-ph carlos-marchal-ph requested a review from a team November 10, 2025 19:24
@carlos-marchal-ph carlos-marchal-ph self-assigned this Nov 10, 2025
@carlos-marchal-ph carlos-marchal-ph added bug Something isn't working team/llm-analytics labels Nov 10, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@carlos-marchal-ph carlos-marchal-ph merged commit 6c815df into master Nov 11, 2025
12 checks passed
@carlos-marchal-ph carlos-marchal-ph deleted the fix(llma)/cache-token-double-subtraction branch November 11, 2025 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working team/llm-analytics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants