|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > AI - Artificial Intelligence Commands > AIU. - OpenAI API > !Response Analysis > AIU. - Artificial Intelligence Utility |
MiniRobotLanguage (MRL)
AIU.GetTotalTokens
Retrieve the Total Token Count Used in the Response
Intention
GetTotalTokens Command: Monitoring Token Usage
The GetTotalTokens command retrieves the total number of tokens (input + output) used in the AI’s last response, aiding in cost tracking and optimization.
This is key for managing API usage and budget within your scripts.
It’s part of the AIU - OpenAI API suite.
This command returns an integer representing the total tokens consumed by the last AI operation, including both prompt and response tokens.
You can store the result in a variable or, if no variable is specified, it will be placed on the Top of Stack (TOS).
Tracking token usage is essential for:
•Cost Management: Monitor API expenses based on token consumption.
•Optimization: Adjust prompts or max token limits to stay within budget.
•Analysis: Understand the resource impact of different operations.
Call this command after an AI operation (e.g., AIU.Chat) with an optional variable to store the result.
Token counts vary by model and input complexity, impacting costs as per OpenAI’s pricing.
Example Usage
AIU.Chat|Tell me a story|$$STO
AIU.GetTotalTokens|$$TOK
DBP.Total Tokens Used: $$TOK
This example retrieves the token count for a story generation task.
Illustration
┌────────────────────┐
│ Token Count │
├────────────────────┤
│ 45 │
└────────────────────┘
Retrieving the total tokens used in a response.
Syntax
AIU.GetTotalTokens[|P1]
AIU.Get_TotalTokens[|P1]
Parameter Explanation
P1 - (Optional) The variable where the total token count will be stored. If omitted, the count is placed on the Top of Stack (TOS).
Example
AIU.ChatVision|What’s this?|C:\image.jpg|$$RES
AIU.GetTotalTokens|$$NUM
DBP.Tokens Used: $$NUM
ENR.
Remarks
- Must follow an AI operation to retrieve a valid count.
- Returns 0 or an undefined value if no prior operation exists.
Limitations
- Accepts at most one parameter; additional parameters trigger an error.
- Only reflects the last operation’s token usage.
See also:
• AIU.Chat