Home / Function/ _get_usage_metadata_from_generations() — langchain Function Reference

_get_usage_metadata_from_generations() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  706241bd_703f_99a6_9c98_870ee029e748["_get_usage_metadata_from_generations()"]
  34ac5e02_6299_266f_403f_f48ad1969c05["langchain.py"]
  706241bd_703f_99a6_9c98_870ee029e748 -->|defined in| 34ac5e02_6299_266f_403f_f48ad1969c05
  0a3df31a_88bd_f701_6d60_8d6d287acc08["_on_llm_end()"]
  0a3df31a_88bd_f701_6d60_8d6d287acc08 -->|calls| 706241bd_703f_99a6_9c98_870ee029e748
  style 706241bd_703f_99a6_9c98_870ee029e748 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/tracers/langchain.py lines 74–96

def _get_usage_metadata_from_generations(
    generations: list[list[dict[str, Any]]],
) -> UsageMetadata | None:
    """Extract and aggregate `usage_metadata` from generations.

    Iterates through generations to find and aggregate all `usage_metadata` found in
    messages. This is typically present in chat model outputs.

    Args:
        generations: List of generation batches, where each batch is a list of
            generation dicts that may contain a `'message'` key with `'usage_metadata'`.

    Returns:
        The aggregated `usage_metadata` dict if found, otherwise `None`.
    """
    output: UsageMetadata | None = None
    for generation_batch in generations:
        for generation in generation_batch:
            if isinstance(generation, dict) and "message" in generation:
                message = generation["message"]
                if isinstance(message, dict) and "usage_metadata" in message:
                    output = add_usage(output, message["usage_metadata"])
    return output

Domain

Subdomains

Called By

Frequently Asked Questions

What does _get_usage_metadata_from_generations() do?
_get_usage_metadata_from_generations() is a function in the langchain codebase, defined in libs/core/langchain_core/tracers/langchain.py.
Where is _get_usage_metadata_from_generations() defined?
_get_usage_metadata_from_generations() is defined in libs/core/langchain_core/tracers/langchain.py at line 74.
What calls _get_usage_metadata_from_generations()?
_get_usage_metadata_from_generations() is called by 1 function(s): _on_llm_end.

Analyze Your Own Codebase

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

Try Supermodel Free