LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corecallbacksusageUsageMetadataCallbackHandler
    Class●Since v0.3

    UsageMetadataCallbackHandler

    Callback Handler that tracks AIMessage.usage_metadata.

    Copy
    UsageMetadataCallbackHandler(
        self,
    )

    Bases

    BaseCallbackHandler

    Example:

    from langchain.chat_models import init_chat_model
    from langchain_core.callbacks import UsageMetadataCallbackHandler
    
    llm_1 = init_chat_model(model="openai:gpt-4o-mini")
    llm_2 = init_chat_model(model="anthropic:claude-haiku-4-5-20251001")
    
    callback = UsageMetadataCallbackHandler()
    result_1 = llm_1.invoke("Hello", config={"callbacks": [callback]})
    result_2 = llm_2.invoke("Hello", config={"callbacks": [callback]})
    callback.usage_metadata
    {'gpt-4o-mini-2024-07-18': {'input_tokens': 8,
      'output_tokens': 10,
      'total_tokens': 18,
      'input_token_details': {'audio': 0, 'cache_read': 0},
      'output_token_details': {'audio': 0, 'reasoning': 0}},
     'claude-haiku-4-5-20251001': {'input_tokens': 8,
      'output_tokens': 21,
      'total_tokens': 29,
      'input_token_details': {'cache_read': 0, 'cache_creation': 0}}}

    Constructors

    constructor
    __init__

    Attributes

    attribute
    usage_metadata: dict[str, UsageMetadata]

    Methods

    method
    on_llm_end

    Collect token usage.

    Inherited fromBaseCallbackHandler

    Attributes

    Araise_error: bool
    —

    Whether to raise an error if an exception occurs.

    Arun_inline: boolAignore_llm: bool
    —

    Whether to ignore LLM callbacks.

    Aignore_retry: bool
    —

    Whether to ignore retry callbacks.

    Aignore_chain: bool
    —

    Whether to ignore chain callbacks.

    Aignore_agent: bool
    —

    Whether to ignore agent callbacks.

    Aignore_retriever: bool
    —

    Whether to ignore retriever callbacks.

    Aignore_chat_model: bool
    —

    Whether to ignore chat model callbacks.

    Aignore_custom_event: bool
    —

    Ignore custom event.

    Inherited fromLLMManagerMixin

    Methods

    Mon_llm_new_token
    —

    Run on new output token.

    Mon_llm_error
    —

    Handle an error for an LLM run.

    Inherited fromChainManagerMixin

    Methods

    Mon_chain_end
    —

    End a trace for a chain run.

    Mon_chain_error
    —

    Handle an error for a chain run.

    Mon_agent_action
    —

    Run on agent action.

    Mon_agent_finish
    —

    Run on the agent end.

    Inherited fromToolManagerMixin

    Methods

    Mon_tool_end
    —

    End a trace for a tool run.

    Mon_tool_error
    —

    Run when tool errors.

    Inherited fromRetrieverManagerMixin

    Methods

    Mon_retriever_error
    —

    Run when Retriever errors.

    Mon_retriever_end
    —

    Run when Retriever ends running.

    Inherited fromCallbackManagerMixin

    Methods

    Mon_llm_start
    —

    Start a trace for a (non-chat model) LLM run.

    Mon_chat_model_start
    —

    Start a trace for a chat model run.

    Mon_retriever_start
    —

    Run when Retriever starts running.

    Mon_chain_start
    —

    Start a trace for a chain run.

    Mon_tool_start
    —

    Start a trace for a tool run.

    Inherited fromRunManagerMixin

    Methods

    Mon_text
    —

    Run on an arbitrary text.

    Mon_retry
    —

    Run on retry.

    Mon_custom_event
    —

    Generate a custom astream event.

    View source on GitHub