Skip to main content
Updated in 4.92 In Cognigy.AI, you can use the api.getLLMTokenUsageForSession() API call in a Code Node or an Extension Node to fetch the Large Language Model (LLM) total token usage in a conversation or chat session. This way, you can track your LLM providers costs. The LLM Session Token Counter retrieves the token usage information via LLM provider-specific API calls to return the exact number of tokens the LLM uses. If the information isn’t available by the provider, Cognigy.AI estimates the token usage.

Prerequisite

Limitations

  • The LLM Session Token Counter doesn’t support embedding models from Aleph Alpha.

Fetching Session-Wide Token Usage

  1. Add a Code Node after the Node that uses an LLM, for example, after an AI Agent Node.
  2. Add the following code to the Code Node:
This Code Node fetches the total token usage in the session and stores it in the token variable.
The following JSON example shows an object returned by api.getLLMTokenUsageForSession():
For testing in the Interaction Panel, you can output the JSON object by adding api.say() at the end of your Code Node.

More Information

Last modified on April 21, 2026