Hi, I’m following this guide to track token usage in my agentic workflow, this is my implementation:
callback = UsageMetadataCallbackHandler()
config: RunnableConfig = {
"configurable": {
"thread_id": f"chat_{thread_id}_{run_id}",
},
"run_id": run_id,
"recursion_limit": 100,
"callbacks": [callback],
}
try:
async for _namespace, stream_mode, chunk in self.graph.astream(
initial_state,
config=config,
subgraphs=True,
stream_mode=["messages", "updates"],
):
if stream_mode == "messages":
async for event in self._handle_stream_messages(
chunk=chunk,
):
yield event
elif stream_mode == "updates":
async for event in self._handle_stream_updates(
chunk=chunk,
initial_state=initial_state,
):
yield event
logger.debug(f"Usage metadata: {callback.usage_metadata}")
...
For some reason I always get an empty value.
Usage metadata: {}
Why is this happening and which other alternatives I have to track token usage?