A token tracking system collects token-consumption measurements from language-model calls so applications can manage costs across multi-call workflows. It is especially useful when documents exceed a model’s context window and require several requests. section 7.1.4
A practical implementation combines a TokenTracker instance with an LLM callback. When a call completes, handleLLMEnd reads usage metadata from the returned message, updates the tracker, and exposes current usage and token differences. The tracker can later export metrics to an analytics service. section 7.1.4
Tracking Gemini usage
Create a TokenTracker, attach handleLLMEnd to a ChatGoogleGenerativeAI model, pass message.usage_metadata to updateTokens, and log getCurrentUsage() and getTokenDiff().
Message metadata provides broader provider coverage than output.llmOutput?.tokenUsage, which may be model-limited; js-tiktoken currently supports OpenAI models but not Google Gemini models. section 7.1.4
