AIG. - AI Google Gemini Integration

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > AI - Artificial Intelligence Commands > AIG. - Google AI > 8. Results & Analysis >

AIG. - AI Google Gemini Integration

AIG.GetTokens

Previous Top Next


MiniRobotLanguage (MRL)

 

AIG.Get Tokens
Retrieve the Total Token Count

 

Intention

 

GetTokens Command: Measure API Usage
 
The GetTokens Command retrieves the total number of tokens (Input + Output) consumed by the most recent Google Gemini API transaction.

This value is extracted from the usageMetadata field returned by Google and stored in the internal AIG_TotalTokens variable.

It is crucial for monitoring costs and ensuring you stay within the model's context window limits.

 

What is the GetTokens Command?

 

The GetTokens Command simply reads the integer value representing the "Total Token Count" of the last operation.

This includes:

Prompt Tokens: The cost of your question and any uploaded images/files.

Candidate Tokens: The cost of the text generated by the AI.

 

Why Do You Need It?

 

Monitoring tokens is important for:

Cost Control: Paid plans charge per 1 million tokens. This helps you calculate costs.

Optimization: Identifying overly verbose prompts or responses.

Logic Flow: Deciding if a conversation history is getting too large to send again.

 

How to Use the GetTokens Command?

 

Call the command immediately after AIG.Ask, AIG.AskVision, or AIG.GenerateImage.

You can provide a variable to store the count, or omit it to leave the value on the Top of Stack (TOS).

 

Example Usage

 

AIG.Ask|Write a short poem about coding.

AIG.GetTokens|$$TOK

AIG.GetContent|$$RES

DBP.Poem: $$RES

DBP.Cost in tokens: $$TOK

 

This script asks the AI a question, then retrieves both the answer and the "cost" (tokens used) to generate that answer.

 

Illustration

 

Prompt: "Hi" (1 token)

Reply: "Hello!" (2 tokens)

--------------------------

AIG.GetTokens -> 3

 

Syntax

 

AIG.GetTokens[|P1]

AIG.gtk[|P1]

 

Parameter Explanation

 

P1 - (Optional) Variable to store the numeric token count. If omitted, the value is placed on the Top of Stack (TOS).

 

Remarks

 

- Requires a prior API call. Returns 0 if no call has been made yet.

- Images count as a fixed token amount (approx. 258 tokens per image for Gemini 1.5 models).

 

Limitations

 

- Only reflects the usage of the single most recent request. It does not sum up session totals.

 

See also:

 

? AIG.Ask

? AIG.Set MaxToken

? AIG.Get Content