Home / Function/ _update_token_usage() — langchain Function Reference

_update_token_usage() — langchain Function Reference

Architecture documentation for the _update_token_usage() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c5c2540e_8622_6410_cb0a_4b60c4805659["_update_token_usage()"]
  2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"]
  c5c2540e_8622_6410_cb0a_4b60c4805659 -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda
  e723f22f_4b01_a370_0d2f_ecea13f9c923["_combine_llm_outputs()"]
  e723f22f_4b01_a370_0d2f_ecea13f9c923 -->|calls| c5c2540e_8622_6410_cb0a_4b60c4805659
  style c5c2540e_8622_6410_cb0a_4b60c4805659 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/chat_models/base.py lines 425–450

def _update_token_usage(
    overall_token_usage: int | dict, new_usage: int | dict
) -> int | dict:
    # Token usage is either ints or dictionaries
    # `reasoning_tokens` is nested inside `completion_tokens_details`
    if isinstance(new_usage, int):
        if not isinstance(overall_token_usage, int):
            msg = (
                f"Got different types for token usage: "
                f"{type(new_usage)} and {type(overall_token_usage)}"
            )
            raise ValueError(msg)
        return new_usage + overall_token_usage
    if isinstance(new_usage, dict):
        if not isinstance(overall_token_usage, dict):
            msg = (
                f"Got different types for token usage: "
                f"{type(new_usage)} and {type(overall_token_usage)}"
            )
            raise ValueError(msg)
        return {
            k: _update_token_usage(overall_token_usage.get(k, 0), v)
            for k, v in new_usage.items()
        }
    warnings.warn(f"Unexpected type for token usage: {type(new_usage)}")
    return new_usage

Domain

Subdomains

Frequently Asked Questions

What does _update_token_usage() do?
_update_token_usage() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is _update_token_usage() defined?
_update_token_usage() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 425.
What calls _update_token_usage()?
_update_token_usage() is called by 1 function(s): _combine_llm_outputs.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free