Skip to content

refactor: extract cost estimation into standalone pricing module - #5541

Open
Meinianda-L wants to merge 1 commit into
Aider-AI:mainfrom
Meinianda-L:feat/pricing-module
Open

refactor: extract cost estimation into standalone pricing module#5541
Meinianda-L wants to merge 1 commit into
Aider-AI:mainfrom
Meinianda-L:feat/pricing-module

Conversation

@Meinianda-L

Copy link
Copy Markdown

Type of change

  • Refactor / code improvement (extracts existing cost estimation into a standalone module and adds unit tests)

What does this PR do?

Aider already estimates API cost from token usage (compute_costs_from_tokens, shown after each reply as Cost: $X message, $Y session, hidden when pricing is unknown). However, the cost logic is embedded in the Coder class and has no unit tests.

This PR extracts the cost calculation into a small standalone module aider/pricing.py:

  • ModelPricing class (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 tables
  • ModelPricing.from_model_info(): builds pricing from a model info dict (per-token → per-million conversion). Returns None when the input price is missing or zero — no guessing — matching the existing "tokens only, no cost" behavior
  • ModelPricing.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_tokens now delegates to the new module; behavior is unchanged

Adds tests/basic/test_pricing.py covering: 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 passed

Also 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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.
@CLAassistant

CLAassistant commented Aug 8, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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