Token usage not included in output when stream_mode includes “messages” #927

Closed
opened 2026-02-20 17:42:24 -05:00 by yindo · 1 comment
Owner

Originally created by @AidanWade on GitHub (Aug 19, 2025).

Originally assigned to: @casparb on GitHub.

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

from langgraph.prebuilt import create_react_agent
from databricks_langchain import ChatDatabricks
from langchain_core.callbacks import UsageMetadataCallbackHandler
from langchain_core.callbacks import get_usage_metadata_callback

agent = create_react_agent(
    # using ChatDatabricks, tested with both Anthropic and OpenAI serving endpoints
    model=ChatDatabricks(endpoint=os.getenv('LLM_ENDPOINT_PRIMARY'), temperature=0.0, stream_usage=True),
    # tools=[get_weather],
    tools=[],
    prompt="You are a helpful assistant"
)

callback_handler = UsageMetadataCallbackHandler()
with get_usage_metadata_callback() as cb:
    for event in agent.stream(
        {"messages": [{"role": "user", "content": "hello!"}]},
        config={"callbacks": [callback_handler]},
        stream_mode=["updates"] # stream_mode=["updates","messages"]
    ):
        print(event)

Error Message and Stack Trace (if applicable)


Description

When streaming updates, the final update will include token usage information as expected:

AIMessage(content=‘Hello! How can I assist you today?’, additional_kwargs={}, response_metadata={‘created’: 1754669019, ‘id’: ‘chatcmpl-C2Jr93ePJ2iEdqo5K0hbcZNxtg7v2’, ‘model’: ‘gpt-4o-2024-08-06’, ‘object’: ‘chat.completion’, ‘system_fingerprint’: ‘fp_ee1d74bde0’, 'usage’: {‘completion_tokens’: 10, ‘completion_tokens_details’: {‘accepted_prediction_tokens’: 0, ‘audio_tokens’: 0, ‘reasoning_tokens’: 0, ‘rejected_prediction_tokens’: 0}, ‘prompt_tokens’: 18, ‘prompt_tokens_details’: {‘audio_tokens’: 0, ‘cached_tokens’: 0}, ‘total_tokens’: 28}, ‘model_name’: ‘gpt-4o-2024-08-06’}, id=‘run–ad5a92fc-fd7c-43b1-a3ff-f88c46da786a-0’)

However, if I use stream_mode=[“updates”, “messages”] neither the chunks nor the final update includes the usage information, the final update type output is now abbreviated to:

AIMessage(content=‘Hello! How can I assist you today?’, additional_kwargs={}, response_metadata={‘finish_reason’: ‘stop’}, id=‘run–bb73d7a0-e585-4938-8b18-609f7e50ccbc’)

I have tried included both CallbackHandler methods (as callback or as context manager), together (as above) and individually but neither is populated following the call to the endpoint. Adding "stream_usage=True" and calling an Anthropic endpoint and streaming messages will cause an error presumably because the token counts are None:

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Tested in Databricks serverless environment v2 with:

langgraph==0.5.3 and langgraph==0.6.4
databricks-lanchain==0.6.0

System Info

System Information

OS: Linux
OS Version: #1 SMP PREEMPT_DYNAMIC Wed Jul 2 02:54:28 UTC 2025
Python Version: 3.11.10 (main, Sep 7 2024, 18:35:41) [GCC 11.4.0]

Package Information

langchain_core: 0.3.74
langchain: 0.3.27
langchain_community: 0.3.27
langsmith: 0.4.13
langchain_openai: 0.3.29
langchain_text_splitters: 0.3.9
langgraph_chatagent: Installed. No version info available.
langgraph_sdk: 0.2.0

Optional packages not installed

langserve

Other Dependencies

aiohttp<4.0.0,>=3.8.3: Installed. No version info available.
async-timeout<5.0.0,>=4.0.0;: Installed. No version info available.
dataclasses-json<0.7,>=0.5.7: Installed. No version info available.
httpx-sse<1.0.0,>=0.4.0: Installed. No version info available.
httpx<1,>=0.23.0: Installed. No version info available.
httpx>=0.25.2: Installed. No version info available.
jsonpatch<2.0,>=1.33: Installed. No version info available.
langchain-anthropic;: Installed. No version info available.
langchain-aws;: Installed. No version info available.
langchain-azure-ai;: Installed. No version info available.
langchain-cohere;: Installed. No version info available.
langchain-community;: Installed. No version info available.
langchain-core<1.0.0,>=0.3.66: Installed. No version info available.
langchain-core<1.0.0,>=0.3.72: Installed. No version info available.
langchain-core<1.0.0,>=0.3.74: Installed. No version info available.
langchain-deepseek;: Installed. No version info available.
langchain-fireworks;: Installed. No version info available.
langchain-google-genai;: Installed. No version info available.
langchain-google-vertexai;: Installed. No version info available.
langchain-groq;: Installed. No version info available.
langchain-huggingface;: Installed. No version info available.
langchain-mistralai;: Installed. No version info available.
langchain-ollama;: Installed. No version info available.
langchain-openai;: Installed. No version info available.
langchain-perplexity;: Installed. No version info available.
langchain-text-splitters<1.0.0,>=0.3.9: Installed. No version info available.
langchain-together;: Installed. No version info available.
langchain-xai;: Installed. No version info available.
langchain<1.0.0,>=0.3.26: Installed. No version info available.
langsmith-pyo3>=0.1.0rc2;: Installed. No version info available.
langsmith>=0.1.125: Installed. No version info available.
langsmith>=0.1.17: Installed. No version info available.
langsmith>=0.3.45: Installed. No version info available.
numpy>=1.26.2;: Installed. No version info available.
numpy>=2.1.0;: Installed. No version info available.
openai-agents>=0.0.3;: Installed. No version info available.
openai<2.0.0,>=1.86.0: Installed. No version info available.
opentelemetry-api>=1.30.0;: Installed. No version info available.
opentelemetry-exporter-otlp-proto-http>=1.30.0;: Installed. No version info available.
opentelemetry-sdk>=1.30.0;: Installed. No version info available.
orjson>=3.10.1: Installed. No version info available.
orjson>=3.9.14;: Installed. No version info available.
packaging>=23.2: Installed. No version info available.
pydantic-settings<3.0.0,>=2.4.0: Installed. No version info available.
pydantic<3,>=1: Installed. No version info available.
pydantic<3.0.0,>=2.7.4: Installed. No version info available.
pydantic>=2.7.4: Installed. No version info available.
pytest>=7.0.0;: Installed. No version info available.
PyYAML>=5.3: Installed. No version info available.
requests-toolbelt>=1.0.0: Installed. No version info available.
requests<3,>=2: Installed. No version info available.
requests>=2.0.0: Installed. No version info available.
rich>=13.9.4;: Installed. No version info available.
SQLAlchemy<3,>=1.4: Installed. No version info available.
tenacity!=8.4.0,<10,>=8.1.0: Installed. No version info available.
tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
tiktoken<1,>=0.7: Installed. No version info available.
typing-extensions>=4.7: Installed. No version info available.
vcrpy>=7.0.0;: Installed. No version info available.
zstandard>=0.23.0: Installed. No version info available.

Originally created by @AidanWade on GitHub (Aug 19, 2025). Originally assigned to: @casparb on GitHub. ### 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 from langgraph.prebuilt import create_react_agent from databricks_langchain import ChatDatabricks from langchain_core.callbacks import UsageMetadataCallbackHandler from langchain_core.callbacks import get_usage_metadata_callback agent = create_react_agent( # using ChatDatabricks, tested with both Anthropic and OpenAI serving endpoints model=ChatDatabricks(endpoint=os.getenv('LLM_ENDPOINT_PRIMARY'), temperature=0.0, stream_usage=True), # tools=[get_weather], tools=[], prompt="You are a helpful assistant" ) callback_handler = UsageMetadataCallbackHandler() with get_usage_metadata_callback() as cb: for event in agent.stream( {"messages": [{"role": "user", "content": "hello!"}]}, config={"callbacks": [callback_handler]}, stream_mode=["updates"] # stream_mode=["updates","messages"] ): print(event) ``` ### Error Message and Stack Trace (if applicable) ```shell ``` ### Description When streaming updates, the final update will include token usage information as expected: > AIMessage(content=‘Hello! How can I assist you today?’, additional_kwargs={}, response_metadata={‘created’: 1754669019, ‘id’: ‘chatcmpl-C2Jr93ePJ2iEdqo5K0hbcZNxtg7v2’, ‘model’: ‘gpt-4o-2024-08-06’, ‘object’: ‘chat.completion’, ‘system_fingerprint’: ‘fp_ee1d74bde0’, 'usage’: {‘completion_tokens’: 10, ‘completion_tokens_details’: {‘accepted_prediction_tokens’: 0, ‘audio_tokens’: 0, ‘reasoning_tokens’: 0, ‘rejected_prediction_tokens’: 0}, ‘prompt_tokens’: 18, ‘prompt_tokens_details’: {‘audio_tokens’: 0, ‘cached_tokens’: 0}, ‘total_tokens’: 28}, ‘model_name’: ‘gpt-4o-2024-08-06’}, id=‘run–ad5a92fc-fd7c-43b1-a3ff-f88c46da786a-0’) However, if I use `stream_mode=[“updates”, “messages”]` neither the chunks nor the final update includes the usage information, the final update type output is now abbreviated to: > AIMessage(content=‘Hello! How can I assist you today?’, additional_kwargs={}, response_metadata={‘finish_reason’: ‘stop’}, id=‘run–bb73d7a0-e585-4938-8b18-609f7e50ccbc’) I have tried included both CallbackHandler methods (as callback or as context manager), together (as above) and individually but neither is populated following the call to the endpoint. Adding "stream_usage=True" and calling an Anthropic endpoint and streaming messages will cause an error presumably because the token counts are None: > TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' Tested in Databricks serverless environment v2 with: langgraph==0.5.3 and langgraph==0.6.4 databricks-lanchain==0.6.0 ### System Info System Information ------------------ > OS: Linux > OS Version: #1 SMP PREEMPT_DYNAMIC Wed Jul 2 02:54:28 UTC 2025 > Python Version: 3.11.10 (main, Sep 7 2024, 18:35:41) [GCC 11.4.0] Package Information ------------------- > langchain_core: 0.3.74 > langchain: 0.3.27 > langchain_community: 0.3.27 > langsmith: 0.4.13 > langchain_openai: 0.3.29 > langchain_text_splitters: 0.3.9 > langgraph_chatagent: Installed. No version info available. > langgraph_sdk: 0.2.0 Optional packages not installed ------------------------------- > langserve Other Dependencies ------------------ > aiohttp<4.0.0,>=3.8.3: Installed. No version info available. > async-timeout<5.0.0,>=4.0.0;: Installed. No version info available. > dataclasses-json<0.7,>=0.5.7: Installed. No version info available. > httpx-sse<1.0.0,>=0.4.0: Installed. No version info available. > httpx<1,>=0.23.0: Installed. No version info available. > httpx>=0.25.2: Installed. No version info available. > jsonpatch<2.0,>=1.33: Installed. No version info available. > langchain-anthropic;: Installed. No version info available. > langchain-aws;: Installed. No version info available. > langchain-azure-ai;: Installed. No version info available. > langchain-cohere;: Installed. No version info available. > langchain-community;: Installed. No version info available. > langchain-core<1.0.0,>=0.3.66: Installed. No version info available. > langchain-core<1.0.0,>=0.3.72: Installed. No version info available. > langchain-core<1.0.0,>=0.3.74: Installed. No version info available. > langchain-deepseek;: Installed. No version info available. > langchain-fireworks;: Installed. No version info available. > langchain-google-genai;: Installed. No version info available. > langchain-google-vertexai;: Installed. No version info available. > langchain-groq;: Installed. No version info available. > langchain-huggingface;: Installed. No version info available. > langchain-mistralai;: Installed. No version info available. > langchain-ollama;: Installed. No version info available. > langchain-openai;: Installed. No version info available. > langchain-perplexity;: Installed. No version info available. > langchain-text-splitters<1.0.0,>=0.3.9: Installed. No version info available. > langchain-together;: Installed. No version info available. > langchain-xai;: Installed. No version info available. > langchain<1.0.0,>=0.3.26: Installed. No version info available. > langsmith-pyo3>=0.1.0rc2;: Installed. No version info available. > langsmith>=0.1.125: Installed. No version info available. > langsmith>=0.1.17: Installed. No version info available. > langsmith>=0.3.45: Installed. No version info available. > numpy>=1.26.2;: Installed. No version info available. > numpy>=2.1.0;: Installed. No version info available. > openai-agents>=0.0.3;: Installed. No version info available. > openai<2.0.0,>=1.86.0: Installed. No version info available. > opentelemetry-api>=1.30.0;: Installed. No version info available. > opentelemetry-exporter-otlp-proto-http>=1.30.0;: Installed. No version info available. > opentelemetry-sdk>=1.30.0;: Installed. No version info available. > orjson>=3.10.1: Installed. No version info available. > orjson>=3.9.14;: Installed. No version info available. > packaging>=23.2: Installed. No version info available. > pydantic-settings<3.0.0,>=2.4.0: Installed. No version info available. > pydantic<3,>=1: Installed. No version info available. > pydantic<3.0.0,>=2.7.4: Installed. No version info available. > pydantic>=2.7.4: Installed. No version info available. > pytest>=7.0.0;: Installed. No version info available. > PyYAML>=5.3: Installed. No version info available. > requests-toolbelt>=1.0.0: Installed. No version info available. > requests<3,>=2: Installed. No version info available. > requests>=2.0.0: Installed. No version info available. > rich>=13.9.4;: Installed. No version info available. > SQLAlchemy<3,>=1.4: Installed. No version info available. > tenacity!=8.4.0,<10,>=8.1.0: Installed. No version info available. > tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available. > tiktoken<1,>=0.7: Installed. No version info available. > typing-extensions>=4.7: Installed. No version info available. > vcrpy>=7.0.0;: Installed. No version info available. > zstandard>=0.23.0: Installed. No version info available.
yindo added the bug label 2026-02-20 17:42:24 -05:00
yindo closed this issue 2026-02-20 17:42:24 -05:00
Author
Owner

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

Hi @AidanWade, thank you for your clear MRE. Looks like this issue was just resolved in databricks/databricks-ai-bridge#176. I'd suggest to keep an eye out for a new release of databricks-langchain, and if you're still having trouble after, reply in this thread and I'll reopen.

@casparb commented on GitHub (Sep 8, 2025): Hi @AidanWade, thank you for your clear MRE. Looks like this issue was just resolved in databricks/databricks-ai-bridge#176. I'd suggest to keep an eye out for a new release of [databricks-langchain](https://pypi.org/project/databricks-langchain/), and if you're still having trouble after, reply in this thread and I'll reopen.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#927