Usage Billing & Metering
Relevant source files
The following files were used as context for generating this wiki page:
- server/internal/metering/ch_writer_test.go
- server/internal/usage/meter_usage_test.go
- client/dashboard/src/components/billing/meter-usage-section.tsx
- server/internal/admin/customer_usage.go
- client/dashboard/src/sdk/src/funcs/usageGetMeterUsage.ts
- client/dashboard/src/sdk/src/funcs/usageGetPaygBillingSummary.ts
- client/admin/src/sdk/src/funcs/adminGetMeterUsage.ts
The Usage Billing & Metering system tracks, aggregates, and reports resource consumption across the Speakeasy platform. It provides high-granularity observability into token usage, bandwidth, and risk scanning events to support Pay-As-You-Go (PAYG) billing and organizational cost management.
This system consumes raw meter readings from various service sources, enriches them with billing identity metadata, and stores them in ClickHouse for high-performance time-series analysis. The dashboard utilizes this data to render usage charts, daily rates, and billing cycle summaries.
Sources: README.md, client/dashboard/src/components/billing/meter-usage-section.tsx:162-167
Metering Architecture & Data Flow
The metering architecture relies on a decoupled ingestion pipeline where service events are transformed into standard meter readings before being written to the telemetry database.
Ingestion Pipeline
- Production: Services generate
UsageInputorAdjustmentInputbased on activities like chat sessions or bandwidth consumption. - Writing: The
MeterReadingCHWriterreceives batches of ProtobufMeterReadingmessages. - Enrichment: The writer queries the PostgreSQL database to resolve billing user attributes (e.g., email, department, cost center) based on the
AttributeBillingUserID. - Persistence: The enriched data is written to the
billing_meter_readings_by_timetable in ClickHouse.
This diagram illustrates the flow of usage data from the initial service activity through enrichment in Postgres to final storage in ClickHouse for retrieval by the API.
Sources: server/internal/metering/ch_writer_test.go:102-126, server/internal/metering/ch_writer_test.go:214-230
Reading Types
The system distinguishes between two primary types of meter readings:
- Usage: Represents incremental consumption of a resource.
- Adjustment: Corrects existing usage records, often for reconciliation. These records include a
CorrectsReadingIDand anAdjustmentReason.
Sources: server/internal/metering/ch_writer_test.go:49-62
Usage Families and Dimensions
Usage is categorized into "Families," which define the specific resource being metered. Each family supports different breakdown dimensions for analysis.
| Family | Description | Units | Default Breakdown |
|---|---|---|---|
agent_session_storage | Volume of chat/agent session data | Tokens | project |
mcp_bandwidth | Traffic to/from the MCP gateway | Bytes | direction |
risk_content_scans | Content processed by risk policies | Tokens | policy |
Sources: client/dashboard/src/components/billing/meter-usage-section.tsx:28-44
Dimension Aggregation
The backend supports aggregating data across various dimensions including project, billing_user, model, and direction. The system handles "marker" series like unset and remainder to ensure total usage sums correctly even when metadata is missing.
Sources: server/internal/usage/meter_usage_test.go:76-88, client/dashboard/src/components/billing/meter-usage-section.tsx:109-117
Data Retention and Windowing
The system enforces strict windowing logic for usage queries to maintain performance and accuracy.
- Three-Month Clamp: The
ResolveMeterUsageWindowfunction rejects or clamps queries exceeding three calendar months. - UTC Day Alignment: Queries must align with full UTC days. Partial days (sub-day timestamps) are rejected to ensure bucket consistency.
- Billing Cycle Anchoring: Organizations define a
BillingCycleAnchorDay(e.g., the 1st of the month) used to generate theBillingCyclePeriod.
Sources: server/internal/usage/meter_usage_test.go:148-164, server/internal/admin/customer_usage.go:30-34
Admin & Customer Usage Reporting
The Admin API provides a multi-organization view of consumption for operational oversight.
Customer Usage Summary
The GetCustomerUsage service generates a report containing:
- Current Cycle: The start and end timestamps for the organization's current billing period.
- Product Costs: Estimated cost in USD based on current PAYG list prices.
- Previous Period: Comparison data for the prior billing cycle.
The sequence diagram shows the administrative flow for retrieving usage reports across multiple organizations.
Sources: server/internal/admin/customer_usage.go:17-48, client/admin/src/sdk/src/funcs/adminGetMeterUsage.ts:60-70
API Endpoints
The following table summarizes the primary RPC endpoints used for billing and metering.
| Endpoint | Method | Scope | Description |
|---|---|---|---|
/rpc/usage.getMeterUsage | GET | Organization | Returns aggregated daily meter usage for a specific family and window. |
/rpc/usage.getPaygBillingSummary | GET | Organization | Returns exact billable usage and estimated costs for the live Stripe period. |
/admin/organizations.getMeterUsage | GET | Admin | Returns totals-only usage for an organization over a bounded window. |
Sources: client/dashboard/src/sdk/src/funcs/usageGetMeterUsage.ts:74, client/dashboard/src/sdk/src/funcs/usageGetPaygBillingSummary.ts:72, client/admin/src/sdk/src/funcs/adminGetMeterUsage.ts:60
Data Model: Meter Reading (ClickHouse)
The billing_meter_readings_by_time table uses the following schema for storage:
| Field | Type | Description |
|---|---|---|
id | UUID | Unique identifier for the reading. |
organization_id | String | Target organization for billing. |
project_id | UUID | Optional project-level scoping. |
meter_id | String | Identifier for the specific meter (e.g., agent-session-storage). |
reading_kind | Enum | usage or adjustment. |
value | Int64 | The quantity measured (tokens/bytes). |
unit | String | Unit of measurement (e.g., tokens, bytes). |
measurement_method | String | Method used (e.g., tiktoken_o200k_base). |
occurred_at | DateTime | Timestamp of the actual resource consumption. |
attributes | Map | Enriched metadata (e.g., billing_user_email, cost_center). |
Sources: server/internal/metering/ch_writer_test.go:71-90, server/internal/metering/ch_writer_test.go:183-195
The Usage Billing & Metering system ensures that every interaction within the AI Control Plane is accounted for. By integrating ClickHouse for scale and Postgres for metadata enrichment, the system provides both the high-level cost summaries needed for billing and the granular logs required for security and impact analysis.