Home / Class/ UsageMetadata Class — langchain Architecture

UsageMetadata Class — langchain Architecture

Architecture documentation for the UsageMetadata class in ai.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  742158b5_6038_8e1e_9a58_bce4889295e4["UsageMetadata"]
  49c04e35_a53c_8d1b_eb09_1203ad7cdacf["ai.py"]
  742158b5_6038_8e1e_9a58_bce4889295e4 -->|defined in| 49c04e35_a53c_8d1b_eb09_1203ad7cdacf

Relationship Graph

Source Code

libs/core/langchain_core/messages/ai.py lines 104–157

class UsageMetadata(TypedDict):
    """Usage metadata for a message, such as token counts.

    This is a standard representation of token usage that is consistent across models.

    Example:
        ```python
        {
            "input_tokens": 350,
            "output_tokens": 240,
            "total_tokens": 590,
            "input_token_details": {
                "audio": 10,
                "cache_creation": 200,
                "cache_read": 100,
            },
            "output_token_details": {
                "audio": 10,
                "reasoning": 200,
            },
        }
        ```

    !!! warning "Behavior changed in `langchain-core` 0.3.9"

        Added `input_token_details` and `output_token_details`.

    !!! note "LangSmith SDK"

        The LangSmith SDK also has a `UsageMetadata` class. While the two share fields,
        LangSmith's `UsageMetadata` has additional fields to capture cost information
        used by the LangSmith platform.
    """

    input_tokens: int
    """Count of input (or prompt) tokens. Sum of all input token types."""

    output_tokens: int
    """Count of output (or completion) tokens. Sum of all output token types."""

    total_tokens: int
    """Total token count. Sum of `input_tokens` + `output_tokens`."""

    input_token_details: NotRequired[InputTokenDetails]
    """Breakdown of input token counts.

    Does *not* need to sum to full input token count. Does *not* need to have all keys.
    """

    output_token_details: NotRequired[OutputTokenDetails]
    """Breakdown of output token counts.

    Does *not* need to sum to full output token count. Does *not* need to have all keys.
    """

Frequently Asked Questions

What is the UsageMetadata class?
UsageMetadata is a class in the langchain codebase, defined in libs/core/langchain_core/messages/ai.py.
Where is UsageMetadata defined?
UsageMetadata is defined in libs/core/langchain_core/messages/ai.py at line 104.

Analyze Your Own Codebase

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

Try Supermodel Free