- Two metered features, input tokens and output tokens, fed by the events the SDK sends.
- A wrapped LLM client that reports usage after every call.
- A usage limit that is checked before every call.
- A Stigg server API key for your environment.
- Node.js 22 or later and an Anthropic or OpenAI API key.
- A customer in Stigg with an active subscription. See Provisioning customers.
1
Create metered features for tokens
The SDK reports each call as one event named after the vendor method, with the token counts as fields. Create one metered feature per counter you want to bill.
- In the Stigg app, go to Product catalog → Features and click Create feature.
- Name it Output tokens, with ID
feature-output-tokens, and choose Metered. - Choose Raw events as the meter type and configure:
- Filter:
eventNameequalsanthropic.messages.create(oropenai.chat.completions.create). - Aggregation:
SUMofoutput_tokens(orcompletion_tokensfor OpenAI).
- Filter:
- Repeat for Input tokens with
input_tokens(orprompt_tokens).
2
Price the features in a plan
Add both features to the customer’s plan, with the price per unit and the monthly limit you want. For example, a limit of 2,000,000 output tokens per month, and a per-unit price for overage. See Metered features.
3
Install the SDK
.env
4
Wrap your client
Create the SDK once per process, and wrap the client you already use.Because the wrapped client has no default customer, TypeScript requires every gated call to name one. That is the safest setup for a multi-tenant server.
5
Name the customer on each call
Add a
stiggContext to the parameters of each call. Everything else stays the same.stiggContext is removed before the request is sent, so the provider never sees it. When the response arrives, the SDK reports the token counts in the background.With no entitlement in the context, calls are only metered, never blocked. To block them once the customer reaches a limit, see the Gate calls on the limit step below.For streamed OpenAI calls, add
stream_options: { include_usage: true }. Otherwise OpenAI sends no usage to meter.6
Flush before exit
Reports are sent in the background. In a long-running server you do not need to do anything. In a serverless function, a queue worker or a script, wait for them before the process ends:
7
Verify the usage
Make a few calls, then open the customer in the Stigg app. Usage events appear under Usage events with the vendor event name and the token fields, and the feature usage on the customer’s page updates as the events are aggregated.If events arrive but usage stays at zero, check that the meter’s
eventName filter matches exactly. See Viewing usage events.8
Gate calls on the limit
Set
entitlement to the feature to check against, and calls are denied once the customer reaches the limit, before the request reaches the provider.decision.remedy tells you what to offer: subscribe when the plan does not include the feature, top-up or raise-budget when a limit is reached.Next steps
- Convert tokens into credits that drain in real time: Weighted credits.
- Bill real gateway cost instead of tokens: Bill LLM gateway spend.
- Give each agent its own budget: Set budgets for AI agents.