|
|
ADK Agent Cost Tracking
Author: Venkata Sudhakar
Controlling API costs is critical in production ADK deployments. Each Gemini API call consumes tokens and incurs charges, and without visibility into per-agent spend, costs can escalate quickly. ShopMax India runs over a dozen specialised agents across support, inventory, and analytics - tracking which agent is spending what allows the team to optimise prompts and set hard budget limits.
The Gemini API response includes usage_metadata with prompt_token_count and candidates_token_count. ADK agent callbacks provide a hook to intercept every model response and accumulate token counts. Combining this with Cloud Monitoring custom metrics and budget alerts gives end-to-end cost visibility without any third-party tooling.
The below example shows an ADK agent with a response callback that tracks token usage per session and reports cumulative cost.
It gives the following output,
{
"session_id" : "sess_cost_001",
"input_tokens" : 4812,
"output_tokens" : 687,
"cost_usd" : 0.000567,
"cost_inr" : 0.0473
}
For production, push token counts as custom metrics to Cloud Monitoring using the monitoring_v3 client, then create a budget alert in the Google Cloud Billing console tied to the Vertex AI / Gemini API service. Set an alert at 80% of the monthly budget to give the team time to react before hard limits are hit. Group metrics by agent name label to identify which agent drives the highest spend.
|
|