usage_metadata is None when stream=True in ainvoke #913

Closed
opened 2026-02-20 17:42:21 -05:00 by yindo · 3 comments
Owner

Originally created by @himanshu17287 on GitHub (Aug 12, 2025).

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

response = await self.model.ainvoke(messages, config, stream=True)
print(response.usage_metadata)

Error Message and Stack Trace (if applicable)


Description

When using ainvoke with stream=True in LangGraph, the returned BaseMessage object’s usage_metadata field is always None.
In non-streaming mode (stream=False), usage_metadata is populated correctly.

System Info

langgraph = "^0.4.8"
langchain-openai = "^0.3.27"

Originally created by @himanshu17287 on GitHub (Aug 12, 2025). ### Checked other resources - [x] This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/). - [x] I added a clear and detailed title that summarizes the issue. - [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python response = await self.model.ainvoke(messages, config, stream=True) print(response.usage_metadata) ``` ### Error Message and Stack Trace (if applicable) ```shell ``` ### Description When using ainvoke with stream=True in LangGraph, the returned BaseMessage object’s usage_metadata field is always None. In non-streaming mode (stream=False), usage_metadata is populated correctly. ### System Info langgraph = "^0.4.8" langchain-openai = "^0.3.27"
yindo added the bugpendingawaiting author revision labels 2026-02-20 17:42:21 -05:00
yindo closed this issue 2026-02-20 17:42:21 -05:00
Author
Owner

@eyurtsev commented on GitHub (Aug 12, 2025):

Hi @himanshu17287 could you include a full MRE? which model are you using what does your code look like?

@eyurtsev commented on GitHub (Aug 12, 2025): Hi @himanshu17287 could you include a full MRE? which model are you using what does your code look like?
Author
Owner

@himanshu17287 commented on GitHub (Aug 18, 2025):

Hi @eyurtsev

Model name : gpt-4o

self.model = await ModelsProvider().get_chat_model("gpt-4o")

response = await self.model.ainvoke(
messages, config, stream=true
)
print(response.usage_metadata)

When I set stream = false, I am able to fetch usage metadata, but not when it is set to true

@himanshu17287 commented on GitHub (Aug 18, 2025): Hi @eyurtsev Model name : gpt-4o self.model = await ModelsProvider().get_chat_model("gpt-4o") response = await self.model.ainvoke( messages, config, stream=true ) print(response.usage_metadata) When I set stream = false, I am able to fetch usage metadata, but not when it is set to **true**
Author
Owner

@casparb commented on GitHub (Sep 15, 2025):

Hi @himanshu17287. OpenAI's Chat Completions API does not stream token usage statistics by default. To recover token counts when streaming with OpenAI models, set stream_usage=True as an initialization parameter or on invocation:

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o", stream_usage=True)

response = await llm.ainvoke("What is the capital of France?", stream=True)

See the docs.

@casparb commented on GitHub (Sep 15, 2025): Hi @himanshu17287. OpenAI's Chat Completions API does not stream token usage statistics by default. To recover token counts when streaming with OpenAI models, set `stream_usage=True` as an initialization parameter or on invocation: ```python from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4o", stream_usage=True) response = await llm.ainvoke("What is the capital of France?", stream=True) ``` [See the docs](https://docs.langchain.com/oss/python/integrations/chat/openai#streaming-usage-metadata).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#913