Usage-Based Billing & Meters
Relevant source files
The following files were used as context for generating this wiki page:
- server/polar/meter/endpoints.py
- clients/apps/web/src/components/Landing/features/UsageBillingPage.tsx
- clients/apps/web/src/components/Landing/features/CreditsPage.tsx
- clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/new/ai/chat/route.ts
- clients/apps/web/src/components/Meter/MeterIngestionGuide.tsx
- clients/apps/web/src/components/Customer/CustomerUsageView.tsx
- clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/customers/[customerId]/meter/[meterId]/CustomerMeterPage.tsx
Usage-based billing in Polar allows organizations to charge customers based on actual product consumption. The system is built on three core primitives: Events, Meters, and Metered Prices. Events are immutable records of activity ingested from an application, which are then aggregated by Meters into numerical values per customer. These values are utilized by Metered Prices to calculate line items on invoices at the end of a billing cycle.
The architecture supports real-time updates, allowing both developers and customers to view current usage balances through the API or the Customer Portal. Additionally, Polar supports a "Credits" system where customers can prepay for usage, with meters drawing down from a credit balance before falling back to standard metered pricing.
Sources: UsageBillingPage.tsx:30-45, CreditsPage.tsx:28-40
Core Primitives & Data Flow
The usage billing pipeline follows a linear progression from data ingestion to invoice generation.
System Components
| Component | Description |
|---|---|
| Events | Immutable records of activity posted with a customer ID and metadata. |
| Meters | Filters and aggregation logic that transform event streams into customer metrics. |
| Metered Prices | Pricing configurations attached to products that read meter values for billing. |
| Credits | Prepaid balances that are consumed by meters before metered pricing applies. |
Sources: UsageBillingPage.tsx:30-45, CreditsPage.tsx:55-70
Logic Flow Diagram
The following diagram illustrates how raw events are transformed into billable line items.
Meter Configuration & Aggregation
Meters are defined by a set of filters and an aggregation function. Filters determine which ingested events belong to a specific meter (e.g., matching an event name like api_call), while aggregation functions compute the final value.
Aggregation Functions
Polar supports six primary aggregation functions:
- Count: Total number of matching events (e.g., API calls).
- Sum: Adds a metadata property value (e.g., total tokens or bytes).
- Average: Computes the mean of a metadata property.
- Min / Max: Identifies the lowest or highest recorded value (e.g., peak concurrency).
- Unique: Counts distinct values of a property (e.g., unique active users).
Sources: UsageBillingPage.tsx:75-100, chat/route.ts:80-88
API Operations
Meters are managed via the /v1/meters endpoint. Key operations include:
- GET
/: List meters for an organization with optional filters foris_archived. - POST
/: Create a new meter definition. - GET
/{id}/quantities: Retrieve aggregated quantities over a specific time period (hour, day, week, month, or year) with timezone support. - PATCH
/{id}: Update meter metadata or configuration.
Sources: endpoints.py:40-160
Event Ingestion Strategies
Events are ingested into Polar using the SDK or direct API calls. The platform provides specialized strategies for common use cases:
- LLM Strategy: Automatically counts prompt and completion tokens from OpenAI-compatible providers.
- Stream Strategy: Measures chunks, bytes, or duration for streaming responses.
- S3 Strategy: Meters file uploads/downloads via S3-compatible storage.
- Delta-Time: Measures wall-clock time spent within a specific function or session.
Sources: UsageBillingPage.tsx:105-135
Implementation Example (TypeScript)
Ingesting a basic event to trigger a meter:
await polar.events.ingest({
events: [
{
name: "api_call",
externalCustomerId: "cust_123",
metadata: {
route: "/api/v1/resource",
method: "GET"
}
}
]
});
Sources: MeterIngestionGuide.tsx:42-60
Customer Usage & Credits
Each customer maintains a live "Customer Meter" state. This state tracks consumed units, credited units, and the resulting balance.
Credit Drawdown Logic
When a product includes a "Credits Benefit," the meter behavior changes:
- Incoming events are matched to the meter.
- The system checks if the customer has a positive credit balance for that specific meter.
- Units are deducted from the credit balance.
- If the balance reaches zero, the system either stops (if no metered price is set) or begins accruing costs based on the metered price.
Sources: CreditsPage.tsx:28-45, CustomerUsageView.tsx:35-50
Monitoring Usage
The dashboard provides a detailed view of customer usage, including stacked charts and itemized rows for each active meter.
| Statistic | Description |
|---|---|
| Consumed | Total units aggregated from events. |
| Credited | Total units granted via prepayments or subscriptions. |
| Balance | Remaining units (Credited - Consumed). |
| Overages | Calculated cost for usage exceeding the credit balance. |
Sources: CustomerUsageView.tsx:145-185, CustomerMeterPage.tsx:180-210
Conclusion
Usage-Based Billing & Meters in Polar provide a flexible framework for consumption-based monetization. By decoupling event ingestion from pricing logic, developers can iterate on billing models (switching between count-based, sum-based, or prepaid credits) without re-instrumenting their core application code. The system prioritizes transparency, exposing real-time balances to both the service provider and the end customer.
Sources: UsageBillingPage.tsx:140-150, CreditsPage.tsx:115-130