refactor: extract cost estimation into standalone pricing module - #5541
Open
Meinianda-L wants to merge 1 commit into
Open
refactor: extract cost estimation into standalone pricing module#5541Meinianda-L wants to merge 1 commit into
Meinianda-L wants to merge 1 commit into
Conversation
Move the per-token cost formula out of Coder.compute_costs_from_tokens into a new aider.pricing.ModelPricing class with per-million prices. Pricing is built from model info via from_model_info(), returning None when the input price is unknown so costs are never guessed. Behavior is unchanged; add unit tests covering known/unknown models, zero tokens, and calculation correctness including cache pricing.
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.
Type of change
What does this PR do?
Aider already estimates API cost from token usage (
compute_costs_from_tokens, shown after each reply asCost: $X message, $Y session, hidden when pricing is unknown). However, the cost logic is embedded in theCoderclass and has no unit tests.This PR extracts the cost calculation into a small standalone module
aider/pricing.py:ModelPricingclass (input_cost_per_million/output_cost_per_million/input_cost_per_million_cache_hit), using per-million-token prices — the same unit as provider price tablesModelPricing.from_model_info(): builds pricing from a model info dict (per-token → per-million conversion). ReturnsNonewhen the input price is missing or zero — no guessing — matching the existing "tokens only, no cost" behaviorModelPricing.estimate_cost(): preserves the full cache-pricing logic (Anthropic cache write ×1.25, cache hit ×0.10; DeepSeek dedicated cache-hit unit price)base_coder.compute_costs_from_tokensnow delegates to the new module; behavior is unchangedAdds
tests/basic/test_pricing.pycovering: known model (incl. cache-hit price field), unknown model (missing / zero price / empty info), zero tokens, calculation correctness (plain + cache write/hit branches), and equivalence with the previous formula.How did you verify your code works?
pytest tests/basic/test_pricing.py tests/basic/test_coder.py \ tests/basic/test_models.py tests/basic/test_openrouter.py # 79 passed, 41 subtests passedAlso verified with the project's pre-commit tooling: black (line-length 100), isort, flake8 and codespell all pass on the changed files.
No external APIs, no database, no UI changes, no new dependencies. The only price source remains the litellm model pricing table (cached locally at
~/.aider/caches/model_prices_and_context_window.json), and cache tokens are priced.Checklist