Installation
Overview
The@ai-billing/polar package provides a destination for sending calculated AI costs and token usage directly to Polar.
It leverages Polar’s Events API to track usage and automatically syncs it with your Polar customers based on the tags provided during generation. The destination ensures usage data conforms to Polar’s requirements, including token dimensions, tag values, and precise fractional cost formatting (cost_nanos and cost_currency).
Usage
To use the Polar destination, create it with your Polar access token and event name, then pass it to any billing middleware.Initialize the Polar destination
First, set up the Polar destination using your Polar access token and the name of the event you want to report usage to. You can optionally define a type for your billing tags to ensure type safety.
Set up price resolution
In order for the Polar destination to send costs, the
cost property on the generated events must be populated.If you are using a provider that returns pricing natively (like OpenRouter or Anthropic), the cost is automatically calculated and you can skip this step.However, if you are using a provider that doesn’t return pricing natively (such as Groq or local models), you must supply a price resolver like Narev or a local object resolver. Without this, the cost will be undefined for those providers.Create the billing middleware
Initialize the billing middleware for your preferred provider and include the
polarDestination in the destinations array.Wrap the model
Use
wrapLanguageModel from the ai package to apply the billing middleware to your model.Pass tags during generation
Finally, use the wrapped model with AI SDK functions like
generateText or streamText.Understanding Billing Tags and Identity
Theai-billing-tags object is critical when using the Polar destination. Polar requires an identity to map the event to a customer:- Internal vs External ID: The destination looks for specific tag keys to determine the customer. By default, it checks for
customerId,polarCustomerId, orcustomer_idfor an internal Polar ID. For an external ID, it checks foruserId,externalId, oruser_id. You can override these keys in the destination options usingcustomerIdKeyandexternalCustomerIdKey. - Missing Identity: If neither an internal nor an external ID is found in the tags, the destination will log a warning and skip sending the event to Polar.
- Metadata: Any other tags (like
org_name) will be included in the Polar event metadata alongside token usage dimensions and cost (cost_nanosandcost_currency).
Advanced Configuration
Dynamic Event Names
You can use a function to derive the event name from the billing event. This is useful when you want to ingest different Polar events per model or provider while keeping one destination configuration.Custom Metadata Mapping
By default, the destination builds metadata from the event dimensions, tags, and cost. If you need to override the metadata payload sent to Polar, you can provide amapMetadata function.