!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
        *   [Quickstart](/v0.1/docs/modules/model_io/quick_start/)
        *   [Concepts](/v0.1/docs/modules/model_io/concepts/)
        *   [Prompts](/v0.1/docs/modules/model_io/prompts/)
            
        *   [LLMs](/v0.1/docs/modules/model_io/llms/)
            
        *   [Chat Models](/v0.1/docs/modules/model_io/chat/)
            
            *   [Quick Start](/v0.1/docs/modules/model_io/chat/quick_start/)
            *   [Streaming](/v0.1/docs/modules/model_io/chat/streaming/)
            *   [Caching](/v0.1/docs/modules/model_io/chat/caching/)
            *   [Custom chat models](/v0.1/docs/modules/model_io/chat/custom_chat/)
            *   [Tracking token usage](/v0.1/docs/modules/model_io/chat/token_usage_tracking/)
            *   [Cancelling requests](/v0.1/docs/modules/model_io/chat/cancelling_requests/)
            *   [Dealing with API Errors](/v0.1/docs/modules/model_io/chat/dealing_with_api_errors/)
            *   [Dealing with rate limits](/v0.1/docs/modules/model_io/chat/dealing_with_rate_limits/)
            *   [Tool/function calling](/v0.1/docs/modules/model_io/chat/function_calling/)
            *   [Response metadata](/v0.1/docs/modules/model_io/chat/response_metadata/)
            *   [Subscribing to events](/v0.1/docs/modules/model_io/chat/subscribing_events/)
            *   [Adding a timeout](/v0.1/docs/modules/model_io/chat/timeouts/)
        *   [Model I/O](/v0.1/docs/modules/model_io/)
        *   [Output Parsers](/v0.1/docs/modules/model_io/output_parsers/)
            
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Modules](/v0.1/docs/modules/)
*   [Model I/O](/v0.1/docs/modules/model_io/)
*   [Chat Models](/v0.1/docs/modules/model_io/chat/)
*   Tracking token usage

On this page

Tracking token usage
====================

This notebook goes over how to track your token usage for specific calls.

Using AIMessage.response\_metadata[​](#using-aimessageresponse_metadata "Direct link to Using AIMessage.response_metadata")
---------------------------------------------------------------------------------------------------------------------------

A number of model providers return token usage information as part of the chat generation response. When available, this is included in the [AIMessage.response\_metadata](/v0.1/docs/modules/model_io/chat/response_metadata/) field. Here's an example with OpenAI:

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { ChatOpenAI } from "@langchain/openai";const chatModel = new ChatOpenAI({  model: "gpt-4-turbo",});const res = await chatModel.invoke("Tell me a joke.");console.log(res.response_metadata);/*  {    tokenUsage: { completionTokens: 15, promptTokens: 12, totalTokens: 27 },    finish_reason: 'stop'  }*/

#### API Reference:

*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`

And here's an example with Anthropic:

*   npm
*   Yarn
*   pnpm

    npm install @langchain/anthropic

    yarn add @langchain/anthropic

    pnpm add @langchain/anthropic

    import { ChatAnthropic } from "@langchain/anthropic";const chatModel = new ChatAnthropic({  model: "claude-3-sonnet-20240229",});const res = await chatModel.invoke("Tell me a joke.");console.log(res.response_metadata);/*  {    id: 'msg_017Mgz6HdgNbi3cwL1LNB9Dw',    model: 'claude-3-sonnet-20240229',    stop_sequence: null,    usage: { input_tokens: 12, output_tokens: 30 },    stop_reason: 'end_turn'  }*/

#### API Reference:

*   [ChatAnthropic](https://api.js.langchain.com/classes/langchain_anthropic.ChatAnthropic.html) from `@langchain/anthropic`

Using callbacks[​](#using-callbacks "Direct link to Using callbacks")
---------------------------------------------------------------------

You can also use the `handleLLMEnd` callback to get the full output from the LLM, including token usage for supported models. Here's an example of how you could do that:

    import { ChatOpenAI } from "@langchain/openai";const chatModel = new ChatOpenAI({  model: "gpt-4-turbo",  callbacks: [    {      handleLLMEnd(output) {        console.log(JSON.stringify(output, null, 2));      },    },  ],});await chatModel.invoke("Tell me a joke.");/*  {    "generations": [      [        {          "text": "Why did the scarecrow win an award?\n\nBecause he was outstanding in his field!",          "message": {            "lc": 1,            "type": "constructor",            "id": [              "langchain_core",              "messages",              "AIMessage"            ],            "kwargs": {              "content": "Why did the scarecrow win an award?\n\nBecause he was outstanding in his field!",              "tool_calls": [],              "invalid_tool_calls": [],              "additional_kwargs": {},              "response_metadata": {                "tokenUsage": {                  "completionTokens": 17,                  "promptTokens": 12,                  "totalTokens": 29                },                "finish_reason": "stop"              }            }          },          "generationInfo": {            "finish_reason": "stop"          }        }      ]    ],    "llmOutput": {      "tokenUsage": {        "completionTokens": 17,        "promptTokens": 12,        "totalTokens": 29      }    }  }*/

#### API Reference:

*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Custom chat models

](/v0.1/docs/modules/model_io/chat/custom_chat/)[

Next

Cancelling requests

](/v0.1/docs/modules/model_io/chat/cancelling_requests/)

*   [Using AIMessage.response\_metadata](#using-aimessageresponse_metadata)
*   [Using callbacks](#using-callbacks)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.