cusage

The language

A .usage file says what your product counts, how it adds up and what each plan charges. Money is exact: decimals with 18 places, never floats.

Try it in the playground

Describe usage

What your product counts, and the events your services send when it happens.

unit

What numbers count. Units are checked like dimensions: credit/token times token is credit, and credits plus seats doesn't compile.

unit token, credit, seat
currency

The money unit. Each invoice line is rounded to its minor unit (cents for USD), half away from zero.

currency USD
type

A closed set of values, such as models, countries or GPU types.

type Model = "gpt-4o" | "small"
struct

One usage event: its fields with their types, units and defaults.

struct llm_call {
    model: Model
    input_tokens: int<token>
    cached: bool = false
    user_id: string
}

Measure it

How events add up over a billing period.

table

A rate card keyed by a type. Every value needs a rate (or a default), or the schema doesn't compile.

table input_rate[Model]: credit/token {
    "gpt-4o": 0.0025
    "small": 0.0002
}
meter

Adds events up: sum, count, count_distinct (people, seats), max, min, avg and latest. Over one struct, several (a | b) or all (*), with where filters and by breakdowns.

meter credits: credit = sum(input_tokens * input_rate[model]) from llm_call where cached == false by model
metric

A formula over meters, such as a free allowance, with the same unit checks.

metric billable: credit = max(credits - 1000 credit, 0 credit)

Price it

What a plan charges for what was measured.

plan

Turns meters and metrics into an invoice: flat fees, charges and a minimum spend.

plan pro "Pro" {
    fee "Pro subscription" 49 USD
    charge credits "AI credits" { price 0.002 USD/credit }
    minimum 99 USD
}
charge

A price per unit, a package (per started 1,000), graduated tiers (each tier its own rate) or volume tiers (one rate set by the total), with an included allowance and a minimum or maximum per charge.

charge credits "AI credits" {
    included 50000 credit
    tiered graduated {
        up to 500000 credit at 0.0015 USD/credit
        above at 0.001 USD/credit
    }
}
limit

A cap on a meter or metric: a spend cap, a rate limit, seats or workspaces. Per minute, hour, day or month (UTC calendar windows), or over the whole history.

limit credits 200000 credit per month

Pin it down

Months of made-up usage and the numbers they must produce.

test

Run by cusage test, by every server before it starts, and in the playground on each change. The calculators offer them as starting points.

test "a quiet month" {
    plan pro
    events {
        llm_call { model: "small", input_tokens: 2000, user_id: "ana" } x 100
    }
    expect total == 99 USD
}

A whole schema

Everything above in one file that compiles and whose test passes.

unit token, credit, seat
currency USD

type Model = "gpt-4o" | "small"

struct llm_call {
    model: Model
    input_tokens: int<token>
    user_id: string
}

table input_rate[Model]: credit/token {
    "gpt-4o": 0.0025
    "small": 0.0002
}

meter credits: credit = sum(input_tokens * input_rate[model]) from llm_call by model
meter seats: seat = count_distinct(user_id) from llm_call

/** The first 1,000 credits are free. */
metric billable: credit = max(credits - 1000 credit, 0 credit)

plan starter "Starter" {
    charge billable "AI credits" { price 0.002 USD/credit }
    charge seats "Seats" { price 8 USD/seat }
    limit credits 200000 credit per month
}

test "a small month" {
    plan starter
    events {
        llm_call { model: "gpt-4o", input_tokens: 400000, user_id: "ana" } x 2
    }
    expect credits == 2000 credit
    expect total == 10 USD
}
Open it in the playground