liveBook · Manning

Token count

As explained in Build AI-Enhanced Web Apps

Token usage is the quantity of input and output tokens consumed by a language-model interaction.

Token count is the number of input and output tokens consumed by a language-model interaction. It matters when a large document requires multiple calls: each call adds usage and potential cost. In conversations, counting the tokens in message history helps keep requests within a model’s limits. section 7.1.4 section 8.2.2

Listing 7.4 Tracking usage of tokens
import TokenTracker from "./TokenTracker.js";    #1
const tokenTracker = new TokenTracker();
const model = new ChatGoogleGenerativeAI({
  apiKey: apiKey,
  model: "gemini-2.0-flash",
  streaming: false,
  callbacks: [
    {
      handleLLMEnd: (output) => {    #2
        const { message } = output.generations[0][0];
        tokenTracker.
updateTokens(message.usage_metadata);    #3
        console.log('Current Token Usage:', tokenTracker.getCurrentUsage());#4
        console.log('Token Differences:', tokenTracker.getTokenDiff());
      },
    },
  ],
});

Attach a LangChain.js `handleLLMEnd` callback to record usage after each call. The example reads `message.usage_metadata`, updates a `TokenTracker`, and logs current usage and the change in tokens used. section 7.1.4

Use the count to make two different decisions:

  • Review input and output usage across calls when estimating the cost of document summarization. The tracker shown monitors a session and could be extended to export metrics to an analytics service. section 7.1.4
  • Monitor conversation-history tokens before sending a request. Limit generated output with `maxTokens` and retain only a rolling set of past messages when the history grows. section 8.2.2

Choose a counting method with the model in mind. `js-tiktoken` can encode messages to count conversation-history tokens, but it may not support every model. For completed calls, message usage metadata is used in the callback example; the alternative `output.llmOutput?.tokenUsage` is limited to certain models and may be unreliable across providers. section 7.1.4 section 8.2.2

How Build AI-Enhanced Web Apps covers it

How deep it goes
Shows callback-based usage tracking and outlines history counting, but does not implement a tokenizer or calculate prices.
Examples
Uses a LangChain.js `TokenTracker` callback, a `streamText` output cap, and a rolling ten-message history.
What it assumes
Assumes a JavaScript model integration; introduces usage tracking through callbacks without requiring a tokenizer implementation.
What you can do after
Operate/configure token monitoring to inspect per-call usage and adjust output or retained history against limits.
How it connects
Connects token count to context windows, document summarization, conversation history, and `maxTokens`.

Also explained in

Compare how 4 books explain token count →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.