Usage Meter

TsgcAIUsageMeter counts the tokens of every request your application sends to a language model and turns them into money. Assign it to TsgcAIChat.UsageMeter, give it a price list and a budget, and a user who reaches the budget is refused before the next request is sent.

TsgcAIUsageMeter

Prices per provider and model, running totals per user and session, total, daily or monthly budgets that can block a request, and exports to CSV, any TDataSet and an OpenTelemetry collector.

Component class

TsgcAIUsageMeter

Purpose

Token costs, budgets and usage export

Family

Chat & agents

Platforms

Windows, macOS, Linux, iOS and Android

Editions. TsgcAIUsageMeter ships in sgcAI, in sgcWebSockets Enterprise and in All-Access, the same editions as TsgcAIChat. New in 2026.11.0. sgcAI is self-contained: it includes the sgcWebSockets Core runtime the component is built on. Available for Delphi 7 to Delphi 13 and C++ Builder.

Price every request, cap every user

Add a price, add a budget, assign the meter to the chat. Every request of the chat, and of an agent that uses it, is counted.

uses
  sgcAI, sgcAI_Chat, sgcAI_UsageMeter;

procedure TForm1.MeterUsage;
var
  oPrice: TsgcAIUsagePrice;
  oBudget: TsgcAIUsageBudget;
begin
  oPrice := sgcAIUsageMeter1.Prices.Add; // or Prices.LoadFromFile('prices.json')
  oPrice.Provider := 'openai';
  oPrice.Model := 'gpt-5*';
  oPrice.InputPerMillion := 1.25;
  oPrice.OutputPerMillion := 10;
  oPrice.CachedPerMillion := 0.125;

  oBudget := sgcAIUsageMeter1.Budgets.Add; // every user can spend 2 USD a day
  oBudget.Scope := aubsUser;
  oBudget.Period := aubpDay;
  oBudget.MaxCost := 2;
  sgcAIUsageMeter1.OnBudgetExceeded := DoBudgetExceeded;

  // optional: one OpenTelemetry span per request
  sgcAIUsageMeter1.OpenTelemetry.Enabled := True;
  sgcAIUsageMeter1.OpenTelemetry.Endpoint := 'http://localhost:4318';

  sgcAIChat1.UserId := 'user-1';
  sgcAIChat1.UsageMeter := sgcAIUsageMeter1;
  Memo1.Lines.Add(sgcAIChat1.Chat('Hello'));

  Memo1.Lines.Add(Format('user-1 spent %.6f %s and %d tokens',
    [sgcAIUsageMeter1.TotalCost('user-1'), sgcAIUsageMeter1.Currency,
    sgcAIUsageMeter1.TotalTokens('user-1')]));
  sgcAIUsageMeter1.SaveToCSV('usage.csv');
end;

procedure TForm1.DoBudgetExceeded(Sender: TObject;
  const aBudget: TsgcAIUsageBudget; const aKey: string; aCost: Double;
  aTokens: Int64);
begin
  Memo1.Lines.Add(Format('Budget "%s" reached by %s: %.4f USD',
    [aBudget.DisplayName, aKey, aCost]));
end;

Key properties & methods

The members you reach for most often.

Prices

Prices holds the price per million input, output and cached tokens of each provider and model. Model patterns such as gpt-5* are allowed and the most specific match wins. Prices.LoadFromFile and LoadFromJSON load a price list. Cached tokens are billed with CachedPerMillion.

Totals

TotalCost and TotalTokens return the totals of everything, of one user, of one session or of one user in one session. Records and RecordCount list the last MaxRecords requests without changing the totals, Clear resets everything.

Budgets

Each item of Budgets has a Scope (aubsGlobal, aubsUser or aubsSession), an optional Key, a Period (aubpTotal, aubpDay or aubpMonth) and a MaxCost or MaxTokens limit. With BlockWhenExceeded the next request is refused before it is sent.

Export

SaveToCSV and ToCSV write one line per request with timestamp, provider, model, user, session, tokens and cost. ExportToDataSet appends the records to any TDataSet with matching field names.

OpenTelemetry

With OpenTelemetry.Enabled every request becomes an OTLP/HTTP span with the GenAI semantic conventions: provider, model, input, output and cached tokens, user and session. A background thread sends it, so the chat never waits for the collector.

Events

OnRecord fires for every priced request. OnBudgetExceeded fires once per budget, key and period when the limit is reached. Requests answered by a fallback provider are priced with the provider and model that answered.

Specifications & references

Authoritative sources for the services and protocols this component uses.

Keep exploring

Online HelpFull API reference and usage guide for the sgcAI components.
All sgcAI ComponentsBrowse the feature matrix of every sgcAI component.
Download Free TrialTry it with your own API keys.
PricingSingle, Team and Site licenses, full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and know what every user of your AI features costs.