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



