Skip to content

fix: correct pricing calculation for providers using $/token format#344

Merged
dwash96 merged 1 commit intocecli-dev:v0.92.0from
chrisnestrud:fix/pricing
Jan 1, 2026
Merged

fix: correct pricing calculation for providers using $/token format#344
dwash96 merged 1 commit intocecli-dev:v0.92.0from
chrisnestrud:fix/pricing

Conversation

@chrisnestrud
Copy link
Copy Markdown

Problem:

Commit c6a90d5 added division by DEFAULT_TOKEN_PRICE_RATIO (1000000) to convert pricing from $/M to $/token format. However, this was applied to ALL providers, but different providers use different pricing formats:

  • Synthetic and OpenRouter: Already return pricing in $/token format (e.g., "$0.00000055" per token)
  • LiteLLM's model_prices_and_context_window.json: Uses $/M format (e.g., "2.5e-06" = $2.5/M tokens)

This caused synthetic provider pricing to be divided by 1,000,000 twice, making costs appear 1,000,000x too small. For example:

  • synthetic/hf:zai-org/GLM-4.7: $0.00000055/token became $0.00000000000055/token
  • User reported: 17k sent, 10 received tokens showed cost of $0.0000000096

Solution:

Added a heuristic in ModelProviderManager._record_to_info() to detect pricing format before applying conversion:

  • If cost >= 0.001: Treat as $/M format, divide by 1,000,000
  • If cost < 0.001: Treat as $/token format, no division

This threshold (0.001) was chosen because:

  • $/M pricing is typically >= $0.001/M (e.g., $1.0/M, $2.5/M)
  • $/token pricing is typically < $0.001/token (e.g., $0.00000055/token)

Changes:

  1. aider/helpers/model_providers.py:

    • Added _normalize_cost() helper function to intelligently handle both formats
    • Replaced unconditional division with format-aware normalization
  2. tests/basic/test_model_provider_manager.py:

    • Added test_pricing_normalization_detects_token_format: Verifies $/token pricing is not divided
    • Added test_pricing_normalization_detects_million_format: Verifies $/M pricing is converted correctly

Impact:

  • Synthetic provider models now display correct pricing
  • Existing $/M format providers continue to work correctly
  • No breaking changes to API or behavior

Co-authored-by: aider-ce (synthetic/hf:zai-org/GLM-4.7)

Problem:
--------
Commit c6a90d5 added division by DEFAULT_TOKEN_PRICE_RATIO (1000000) to convert
pricing from $/M to $/token format. However, this was applied to ALL providers,
but different providers use different pricing formats:

- Synthetic and OpenRouter: Already return pricing in $/token format
  (e.g., "$0.00000055" per token)
- LiteLLM's model_prices_and_context_window.json: Uses $/M format
  (e.g., "2.5e-06" = $2.5/M tokens)

This caused synthetic provider pricing to be divided by 1,000,000 twice,
making costs appear 1,000,000x too small. For example:
- synthetic/hf:zai-org/GLM-4.7: $0.00000055/token became $0.00000000000055/token
- User reported: 17k sent, 10 received tokens showed cost of $0.0000000096

Solution:
---------
Added a heuristic in ModelProviderManager._record_to_info() to detect pricing
format before applying conversion:

- If cost >= 0.001: Treat as $/M format, divide by 1,000,000
- If cost < 0.001: Treat as $/token format, no division

This threshold (0.001) was chosen because:
- $/M pricing is typically >= $0.001/M (e.g., $1.0/M, $2.5/M)
- $/token pricing is typically < $0.001/token (e.g., $0.00000055/token)

Changes:
--------
1. aider/helpers/model_providers.py:
   - Added _normalize_cost() helper function to intelligently handle both formats
   - Replaced unconditional division with format-aware normalization

2. tests/basic/test_model_provider_manager.py:
   - Added test_pricing_normalization_detects_token_format: Verifies $/token
     pricing is not divided
   - Added test_pricing_normalization_detects_million_format: Verifies $/M
     pricing is converted correctly

Impact:
-------
- Synthetic provider models now display correct pricing
- Existing $/M format providers continue to work correctly
- No breaking changes to API or behavior

Co-authored-by: aider-ce (synthetic/hf:zai-org/GLM-4.7)
@dwash96 dwash96 changed the base branch from main to v0.92.0 January 1, 2026 17:27
@dwash96 dwash96 merged commit 7a06d89 into cecli-dev:v0.92.0 Jan 1, 2026
8 checks passed
@chrisnestrud chrisnestrud deleted the fix/pricing branch January 5, 2026 12:51
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.

2 participants