[feat] Add optional token pricing and cost estimation to session/run metrics #5566
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.
Summary
This PR introduces a lightweight, opt-in mechanism for users to define model pricing (per million tokens) and automatically calculate cost estimates within Agno session metrics.
Currently, Agno tracks token usage but not the associated costs. Since pricing varies frequently by provider and date, this PR avoids hardcoding price tables. Instead, it provides a PricingConfig registry where users can define their own rates.
The new method works perfectly with both string model IDs and Agno Model objects.
Type of change
Checklist
./scripts/format.shand./scripts/validate.sh)Additional Notes
Key Changes:
agno/utils/pricing.py): AddedPricingConfig, a singleton registry to store model pricing, currency, and calculate costs.agno/models/metrics.py):input_cost,output_cost,cache_read_cost,cache_write_cost,total_cost, andcurrency.__add__to correctly sum costs when aggregating metrics.to_dictto format costs as decimal strings (e.g.,"0.000025") to prevent scientific notation (e.g.,2.5e-05) in logs.agno/models/base.py): Hookedcalculate_cost()into the message population logic for both standard and streaming responses.Features & DX Improvements:
PricingConfig.set_price()method works perfectly with both string IDs and Model objects.set_price(model="gpt-5.1", ...)works.set_price(model=my_gemini_instance, ...)also works (automatically extracts.id).Noneand are excluded from the metrics dictionary, ensuring no clutter for users who don't need this feature.