|
<< 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.GetCompletionTokens
Gets the token count for the completion from the last response.
Intention
The AIU.GetCompletionTokens command (aliased as AIU.gctk) retrieves the number of tokens that the AI model generated in its most recent response (the "completion"). This value is crucial for monitoring API usage, calculating costs, and understanding the length of the AI's output.
It’s your robot’s expense tracker—AIU.GetCompletionTokens tells you exactly how much the AI "wrote," which directly relates to the cost of the API call.
Illustration
┌─────────────────────────┐
│ AI API Response (JSON) │
├─────────────────────────┤
│ ... │
│ "usage": { │
│ "completion_tokens": 25 │
│ } │
└─────────────┬───────────┘
│ AIU.GetCompletionTokens
▼
┌─────────────┐
│ $$TKS = 25 │
└─────────────┘
Extracting the completion token count from a response.
Syntax
AIU.GetCompletionTokens[|$$RET]
Parameter Explanation
P1 - $$RET - (Variable, Numeric, Optional)
The variable where the completion token count will be stored. If omitted, the value is placed on the top-of-stack (TOS).
Examples
'***********************************
' AIU.GetCompletionTokens - Sample 1: Get token count after a chat call
'***********************************
AIU.Ask|Tell me a short story.|$$RES
' Retrieve the token usage details
AIU.GetCompletionTokens|$$TKN
MBX.AI Response:|$$RES
MBX.Tokens Used by AI Response:|$$TKN
END.
Remarks
- This command must be called after a successful API call that returns usage information (like AIU.Ask or AIU.Chat).
- The value is reset to -1 after each new API call is initiated.
- If the API response does not include usage information, this command will return -1.
See also: