How to obtain token usage from langgraph?

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?

1 Like

hi @jerson-censys

could you try to invoke the model directly? Like this

llm = init_chat_model(model="openai:gpt-4o-mini")
cb = UsageMetadataCallbackHandler()
_ = llm.invoke("Hello", config={"callbacks": [cb]})
print(cb.usage_metadata)  # If this is {}, your provider/wrapper isn’t reporting usage

and check what’s the result?

Thanks for your help, I was able to find the problem, I just needed to add:

            llm = ChatOpenAI(
                ...
                stream_usage=True,
               )

To the llm that I have defined for my agents to use inside one of the graph nodes.

1 Like

great job! thanks for notifying :slight_smile: