Count AI Request tokens

The Tokens Counter feature allows tracking the number of tokens consumed in each request made to the AI service. This information can be used to monitor usage, implement custom logging, or enforce consumption limits per user or session. The feature is available from version WorkWithPlus 16 Upgrade #1 onwards.

When enabled, WorkWithPlus invokes a user-defined procedure after each AI request, passing the token usage data as a Properties parameter. The implementation is entirely controlled by the developer, allowing integration with any auditing or billing system.

Setup

Enabling the Tokens Counter requires three steps: activating the session flag, creating the counter procedure, and ensuring the procedure is included in the build.

Step 1: Enable the session flag

The token counting must be explicitly activated at runtime by setting a session variable. Add the following line at the beginning of the WWP_AIAssistant procedure and in the Start event of the WWP_SearchInNLWC Web Component:

&WebSession.Set(WWP_AIAssistantSessionKeys.ApiRequestsTokensCounterEnabled, !'T')

This flag controls whether token data is collected and the counter procedure is invoked. When not set, no token tracking occurs.

Step 2: Create the counter procedure

Create a procedure named AIApiRequestTokensCounter inside the WorkWithPlus.AI module. The procedure signature must declare a single parameter:

Parameter Type Direction
&Tokens Properties IN

The body of this procedure is fully developer-controlled. It can write to a data store, call an external API, update a counter variable, or perform any custom logic required. See Procedure example for a reference implementation.

Step 3: Force procedure compilation

Because AIApiRequestTokensCounter is invoked dynamically by WorkWithPlus at runtime, GeneXus may not include it in the build unless it is explicitly referenced. To force compilation, add the following line inside the existing if 1=0 block in WWP_AIAssistant:

&Link = AIApiRequestTokensCounter.Link(&ConfigParameters)

This reference is never executed at runtime but ensures the procedure is compiled and deployed as part of the application.

Token data reference

After each AI request, WorkWithPlus populates the &Tokens parameter with the following properties before calling AIApiRequestTokensCounter:

Property Type Description
prompt_tokens Numeric Number of tokens used in the prompt sent to the AI service.
completion_tokens Numeric Number of tokens used in the response generated by the AI service.
total_tokens Numeric Total tokens consumed in the request (prompt_tokens + completion_tokens). This is the value that counts against the client's token quota.