# How to obtain token usage from langgraph?

**URL:** <https://forum.langchain.com/t/how-to-obtain-token-usage-from-langgraph/1727>\
**Category:** LangGraph\
**Tags:** intro-to-langgraph, python-help\
**Created:** [October 1, 2025, 11:49pm UTC](https://forum.langchain.com/t/how-to-obtain-token-usage-from-langgraph/1727 "2025-10-01T23:49:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerson-censys](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/jerson-censys/32/961_2.png) [@jerson-censys](https://forum.langchain.com/u/jerson-censys)\
**Post date:** [October 1, 2025, 11:49pm UTC](https://forum.langchain.com/t/how-to-obtain-token-usage-from-langgraph/1727/1 "2025-10-01T23:49:22Z")

</div>

Hi, I’m following this [guide](https://python.langchain.com/docs/how_to/chat_token_usage_tracking/) to track token usage in my agentic workflow, this is my implementation:

```python
        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.

```auto
Usage metadata: {}

```

Why is this happening and which other alternatives I have to track token usage?

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [October 2, 2025, 6:12pm UTC](https://forum.langchain.com/t/how-to-obtain-token-usage-from-langgraph/1727/2 "2025-10-02T18:12:43Z")

</div>

hi @jerson-censys

could you try to invoke the model directly? Like this

```auto
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?

---

<div class="post-metadata">

**Author:** ![jerson-censys](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/jerson-censys/32/961_2.png) [@jerson-censys](https://forum.langchain.com/u/jerson-censys)\
**Post date:** [October 2, 2025, 8:09pm UTC](https://forum.langchain.com/t/how-to-obtain-token-usage-from-langgraph/1727/3 "2025-10-02T20:09:47Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [October 2, 2025, 8:28pm UTC](https://forum.langchain.com/t/how-to-obtain-token-usage-from-langgraph/1727/4 "2025-10-02T20:28:35Z")

</div>

great job! thanks for notifying 🙂
