Repository navigation
Replies: 12 comments 10 replies
|
yes. correct |
|
I am also having same question. Looking into the code I didn't find any build-in methods. If anyone knows that how to do it please let us know. |
|
Has anyone figured out a solution for this? |
|
LiteLlm has cost counters |
|
I had the same problem since I wanted to track some costs/usage stats, so I tried to intercept with I finally came up with something similar: def track_llm_usage(res: ModelResponse | CustomStreamWrapper, model):
# Log token usage and cost before returning
try:
usage = res.get("usage", {})
prompt_tokens = usage.get("prompt_tokens", 0)
completion_tokens = usage.get("completion_tokens", 0)
total_tokens = usage.get("total_tokens", prompt_tokens + completion_tokens)
# Calculate cost if possible
cost = litellm.completion_cost(completion_response=res) or 0.0
print(f"[acompletion] Model: {model}")
print(f"[acompletion] Prompt tokens: {prompt_tokens}")
print(f"[acompletion] Completion tokens: {completion_tokens}")
print(f"[acompletion] Total tokens: {total_tokens}")
print(f"[acompletion] Cost: ${cost:.6f}")
print(f"[acompletion] Time: {(res._response_ms / 1000):.2f}s")
except Exception as e:
print(f"Error logging token usage: {e}")
return res
class KLiteLLMClient(LiteLLMClient):
async def acompletion(
self, model, messages, tools, **kwargs
):
return track_llm_usage(await super().acompletion(
model=model,
messages=messages,
tools=tools,
**kwargs,
), model=model)
root_agent = LlmAgent(
name="weather_time_agent",
model=LiteLlm(model="gemini/gemini-2.5-flash-preview-04-17",
llm_client=KLiteLLMClient()
),
# [...]
)hope it helps! |
|
search within terminal using ctrl f "total_tokens". hope that helps |
|
If you are using LiteLlm as an agent model, add LiteLlm(
'azure/gpt-4.1',
stream_options={"include_usage": True}
)and you can get usage tokens by events = runner.run_async(
session_id=session.id,
user_id=session.user_id,
new_message=self.__messages_to_agent_new_message(messages),
run_config=RunConfig(streaming_mode=StreamingMode.SSE),
)
async for event in events:
# cache_tokens_details=None cached_content_token_count=None candidates_token_count=None candidates_tokens_details=None prompt_token_count=172 prompt_tokens_details=[ModalityTokenCount(modality=<MediaModality.TEXT: 'TEXT'>, token_count=172)] thoughts_token_count=44 tool_use_prompt_token_count=None tool_use_prompt_tokens_details=None total_token_count=216 traffic_type=None
print(event.usage_metadata) |
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: You can track token usage directly in Python by inspecting the Hello! As an AI assistant, I can provide some information on how to track token and cost usage with the ADK in Python. Built-in Usage TrackingThe ADK provides a built-in mechanism to track token usage for each call to a language model. When you use Here is an example of how you can access this information in your Python code: from google.adk.runtime import runner
# Your ADK setup and agent definition here...
if __name__ == "__main__":
for event_type, event_data in runner.run_live(agent):
if event_type == "usage_metadata":
print(f"Token Usage: {event_data}")The {
"prompt_token_count": 120,
"candidates_token_count": 250,
"total_token_count": 370
}This allows you to programmatically track token usage without relying on the dashboard. You can then calculate the estimated cost based on the pricing of the models you are using. Advanced Monitoring and ObservabilityFor more advanced monitoring and analytics, you can explore the following options:
I hope this helps you understand how to track your ADK usage and costs! Footnotes: [1] https://google.github.io/adk-docs/streaming/dev-guide/part3/ |
|
https://google.github.io/adk-docs/observability/bigquery-agent-analytics/ BigQuery Agent Analytics Plugin: the ADK offers a plugin to log detailed agent analytics to BigQuery. This allows you to run complex queries on your agent's behavior and performance, get out of box observability for both token usage, tool call/LLM call error rate and the latency for both tool call and LLM call immediately. The plugin just need one line code change to add plugin into your APP. |
|
Cost per invocation is tokens times the price of the model that served the call; the price lookup is where people get stuck. adk-tracegauge does that lookup (disclosure: I maintain it). All 22 price-table entries carry a source URL and fetch date; 20 are re-checked against the vendor's page weekly, and By hand: 3,500 x $0.30/M + 420 x $2.50/M = $0.0021; $0.0056 + $0.0021 = $0.0077. Fake model, no API key. Anything it cannot price is flagged, not guessed: unknown models, audio input, image/audio/video output, and Google Search grounding. The total states what it leaves out.
|
|
Full disclosure: this is the SAIHM project's account. You're hitting a real gap—the ADK gives you model calls, but tracking the actual token flow through a multi-step agent workflow is harder than it looks. By the time you're in production, you need to know not just per-call costs, but which memory reads and context re-feeds are inflating your bill. SAIHM lets you measure this concretely: it logs every memory recall (which cells, how many tokens), so you can run your workflow against test data and see the token delta between "naive re-feed" and "selective recall." You own the logs; you can audit them. That gives you a real production cost estimate before you commit budget. More on how that works: https://t.saihm.coti.global/r/gh-tokensave |
Uh oh!
There was an error while loading. Please reload this page.
Are there any built-in functions to determine cost / use of queries using the ADK? At the moment, I am using the free exp models (gemini-2.0-flash-exp). I need to understand what the costs/usage will be for my flows when put into production.
All reactions