AIX - Artificial Intelligence and X Platform Utility

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > AI - Artificial Intelligence Commands > AIX. - Grok / X - API > !Output Formatting and Delivery >

AIX - Artificial Intelligence and X Platform Utility

AIX.Get TotalTokens

Previous Top Next


MiniRobotLanguage (MRL)

 

AIX.Get TotalTokens
Retrieve the Total Token Count Used in the Response

 

Intention

 

Get TotalTokens Command: Access Token Usage Data
 
The Get TotalTokens command retrieves the total number of tokens used in the latest response from the Grok API, as calculated by the HTP_AnalyzeGrokResponse subroutine.

This is useful for monitoring usage and optimizing API calls.

It’s part of the AIX - Grok / X - API suite.

 

What is the Get TotalTokens Command?

 

The Get TotalTokens command returns the value of the AIX_TotalTokens global variable, which represents the total token count extracted from the JSON response’s usage.total_tokens field.

If unavailable, it falls back to usage.tokens or defaults to -1 if no token data is found.

 

Why Do You Need It?

 

Retrieving the total token count is valuable for:

Cost Management: Tracks token usage for billing purposes.

Optimization: Helps adjust query length to stay within limits.

Debugging: Identifies discrepancies in expected token usage.

 

How to Use the Get TotalTokens Command?

 

Use the command to retrieve the token count after executing an AI query with AIX.Ask or AIX.AskVision.

Store the result in a variable for further processing or display.

 

Example Usage

 

AIX.SetKey|""your_api_key_here""

AIX.SetModel|""grok-3""

AIX.Ask|""What is the weather like today?""

AIX.Get TotalTokens|$$TOKENS

DBP.Total tokens used: $$TOKENS

 

This example sets up the system, sends a query, retrieves the token count, and displays it.

 

Illustration

 

┌────────────────────┐

│ Token Count        │

├────────────────────┤

│ Input: Response    │

│ Process: Extract   │

│ Output: Token Num  │

└────────────────────┘

A visual of extracting the total token count from the response.

 

Syntax

 

AIX.Get TotalTokens|$$variable

AIX_Get_TotalTokens|$$variable

 

Parameter Explanation

 

$$variable - (Required) A variable (e.g., $$TOKENS) of type LONG where the token count will be stored.

 

Remarks

 

- The token count is extracted from the usage.total_tokens field in the JSON response, with fallbacks to usage.tokens or -1 if unavailable.

- Requires a prior call to AIX.Ask or AIX.AskVision to populate the response data.

 

Limitations

 

- Returns -1 if token usage data is missing or unparseable from the response.

- Reflects only the latest response analysis; previous token counts are overwritten.

 

See also:

 

AIX.Ask

AIX.AskVision

Response Analysis