|
|
ADK Agent Token Cost Estimation Before Deployment
Author: Venkata Sudhakar
ShopMax India runs thousands of customer queries each day across order tracking, stock checks, and returns processing. Before deploying a new ADK agent to production, the engineering team needs to predict daily API costs using realistic token usage samples. Token cost estimation lets teams catch over-budget agents in pre-deployment testing and avoid surprise invoices.
The Google Generative AI SDK records input and output token counts in usage_metadata on every response. By sampling token usage across representative queries and multiplying by the per-token price, you can project daily and monthly costs. This approach lets you compare agent versions, set cost budgets as test assertions, and block deploys that exceed the acceptable spend threshold.
The example below builds a cost estimator using five representative ShopMax India queries. Two pytest tests validate that the projected daily cost stays within 15% of the baseline budget and that no single query exceeds the per-query cost ceiling.
It gives the following output,
Estimated daily cost: Rs 521.66
Cost vs baseline: 14.3%
.. (2 passed in 0.04s)
In production, replace the hard-coded token samples with actual usage logs collected from a staging run. Store price constants in environment variables so they can be updated without code changes when the provider changes rates. Set the cost_increase_pct threshold in your CI pipeline as a required gate so that any prompt change that inflates token usage by more than 15% blocks the merge automatically.
|
|