Commit 0ae64d8
committed
fix: correct pricing calculation for providers using $/token format
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)1 parent a64b563 commit 0ae64d8
2 files changed
Lines changed: 82 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
449 | 449 | | |
450 | 450 | | |
451 | 451 | | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
452 | 464 | | |
453 | 465 | | |
454 | 466 | | |
455 | 467 | | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | | - | |
| 468 | + | |
| 469 | + | |
460 | 470 | | |
461 | 471 | | |
462 | 472 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
282 | 282 | | |
283 | 283 | | |
284 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
285 | 353 | | |
286 | 354 | | |
287 | 355 | | |
| |||
0 commit comments