Get(LastStepTokensUsed)

Returns the tokens used in the last AI script step.

Format 

Get ( LastStepTokensUsed )

Parameters 

None

Data type returned 

text

Originated in version 

21.0

Description 

This function returns a result for the last AI script step that ran in the current FileMaker client session. (AI script steps are those that specify an AI account name and model to use.) The result is a JSON object in this format:

Copy
{
  "model": "<ModelName>",
  "usage": {
    "prompt_tokens": <number>,
    "total_tokens": <number>
  }
}
  • model is the name of the model that was last used by an AI script step.

  • usage.total_tokens is the total number of tokens sent to the model by the last AI script step (for example, the Insert Embedding script step).

Depending on the model used, additional types of token counts may be reported that add up to usage.total_tokens.

Notes 

  • This function returns a result only when OpenAI models are used.

  • Use of the GetEmbedding function has no effect on this function's result.

Example 1 

JSONGetElement ( Get ( LastStepTokensUsed ) ; "usage.total_tokens" ) returns 177 if calculated after an Insert Embedding script step sent 177 tokens to an OpenAI model.

Example 2 

Gets embedding vectors for the Meeting::Note field for all records in the Meetings table, then gets the number of tokens used in that script step and adds it to the running total for the current session stored in the $$totalTokensUsed global variable.

Copy
Configure AI Account [ Account Name: "my-account" ; Model Provider: OpenAI ; API key: "sk-RZCtpWT..." ]

Go to Layout [ "Meeting Details" (Meetings) ; Animation: None ]
Show All Records

Insert Embedding in Found Set [ Account Name: "my-account" ; Embedding Model: "text-embedding-3-small" ; Source Field: Meetings::Note ; Target Field: Meetings::Note_Embedding ; Replace target contents ]

Set Variable [ $$totalTokensUsed ; Value: $$totalTokensUsed + 
    JSONGetElement ( Get ( LastStepTokensUsed ) ; "usage.total_tokens" ) ]
Show Custom Dialog [ "Total tokens used this session:" & $$totalTokensUsed ]