Skip to content

Reasoning tokens not reported in usage_metadata #1928

Description

Package (Required)

  • langchain-google-genai
  • langchain-google-vertexai
  • langchain-google-community
  • Other / not sure / general

Checked other resources

  • I added a descriptive title to this issue
  • I searched the LangChain documentation and API reference (linked above)
  • I used the GitHub search to find a similar issue and didn't find it
  • I am sure this is a bug and not a question or request for help

Example Code (Python)

from langchain_google_vertexai.model_garden import ChatAnthropicVertex


def run_test(llm, stream: bool):
    prompt = "Road runner leaves A at 13:37 travelling 87 km/h toward B, 210 km away. When does he arrive? Answer with a single line giving the time in HH:mm format. No explanation."

    if stream:
        message = None
        for chunk in llm.stream(prompt):
            message = chunk if message is None else message + chunk
    else:
        message = llm.invoke(prompt)

    print("Output:", message.text)
    print("Usage:", message.usage_metadata)


llm_vertexai = ChatAnthropicVertex(
    model_name="claude-sonnet-5",
    location="eu",
    stream_usage=True,
    model_kwargs={"output_config": {"effort": "high"}},
)

print("---invoke---")
run_test(llm_vertexai, stream=False)
print("---stream---")
run_test(llm_vertexai, stream=True)

"""
> ---invoke---
> Output: 16:02
> Usage: {'input_tokens': 70, 'output_tokens': 188, 'total_tokens': 258, 'input_token_details': {'cache_read': 0, 'cache_creation': 0}}
> ---stream---
> Output: 16:02
> Usage: {'input_tokens': 70, 'output_tokens': 148, 'total_tokens': 218, 'input_token_details': {'cache_creation': 0, 'cache_read': 0}}
"""

Error Message and Stack Trace (if applicable)

Description

Reasoning tokens (message.usage_metadata["output_token_details"]["reasoning"]) are not reported in ChatAnthropicVertex messages, even tough they are returned from the underlying SDK and reasoning/thinking has obviously taken place (see output token count in the MRE output vs. the actual text length).

Same bug in langchain-anthropic: langchain-ai/langchain#39249
Proposed fix: https://github.com/philemon-otera/langchain-google/tree/add-reasoning-token-count

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingvertexaiGenerative AI on Google Cloud's Vertex AI Platform

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions