*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to track token usage

On this page

How to track token usage
========================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [LLMs](/v0.2/docs/concepts/#llms)

This notebook goes over how to track your token usage for specific LLM calls. This is only implemented by some providers, including OpenAI.

Here's an example of tracking token usage for a single LLM call via a callback:

tip

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

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { OpenAI } from "@langchain/openai";const llm = new OpenAI({  model: "gpt-3.5-turbo-instruct",  callbacks: [    {      handleLLMEnd(output) {        console.log(JSON.stringify(output, null, 2));      },    },  ],});await llm.invoke("Tell me a joke.");/*  {    "generations": [      [        {          "text": "\n\nWhy don't scientists trust atoms?\n\nBecause they make up everything.",          "generationInfo": {            "finishReason": "stop",            "logprobs": null          }        }      ]    ],    "llmOutput": {      "tokenUsage": {        "completionTokens": 14,        "promptTokens": 5,        "totalTokens": 19      }    }  }*/

#### API Reference:

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

If this model is passed to a chain or agent that calls it multiple times, it will log an output each time.

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You've now seen how to get token usage for supported LLM providers.

Next, check out the other how-to guides in this section, like [how to implement your own custom LLM](/v0.2/docs/how_to/custom_llm).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to track token usage

](/v0.2/docs/how_to/chat_token_usage_tracking)[

Next

How to pass through arguments from one step to the next

](/v0.2/docs/how_to/passthrough)

*   [Next steps](#next-steps)