Replies: 1 comment
|
Hey @almyazin, good question, and the line references made this easy to confirm. Sorry it took so long to get you an answer; this one slipped through the cracks. You're right about the ordering: There is a direct way to get the current call's tokens now, though. Since strands-agents 1.36.0 the event loop attaches usage to the assistant message's metadata before the hook is invoked, so you can read it straight off the event: def on_after_model_call(event: AfterModelCallEvent) -> None:
if event.stop_response: # None when the model call raised
usage = event.stop_response.message["metadata"]["usage"]
print(usage["inputTokens"], usage["outputTokens"], usage["totalTokens"])That gives you exactly the usage of the model call the event describes, no accumulation involved. The aggregated view via |
Uh oh!
There was an error while loading. Please reload this page.
Problem
As a Strands Agent SDK user I'd like to have usage statistic for current execution loop via
event.agent.event_loop_metrics.latest_agent_invocationproperty.But according to the current implementation, the hook is invoked before the statistic is updated:
hook invocation:
https://github.com/strands-agents/sdk-python/blob/e7d3eb97c81eb45da486cb8266464ee60d76b20f/src/strands/event_loop/event_loop.py#L346-L355
usage update:
https://github.com/strands-agents/sdk-python/blob/e7d3eb97c81eb45da486cb8266464ee60d76b20f/src/strands/event_loop/event_loop.py#L412-L414
Question
Is there a way to access used tokens count of the current loop from the callback?
All reactions