cusage

Playground

Compiles · 2 tests pass
schema.usage
/**
 * A small AI product: LLM calls priced in credits by model, seats on top.
 * Edit anything: errors, tests and prices update as you type.
 */

unit token, credit, seat
currency USD

/** A closed set: every rate table must price each one. */
type Model = "gpt-4o" | "claude-sonnet" | "small"

/** One LLM call: the usage event your service sends. */
struct llm_call {
    model: Model
    input_tokens: int<token>
    output_tokens: int<token>
    /** Prompt-cache hit: billed at 10%. */
    cached: bool = false
    user_id: string
}

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

table output_rate[Model]: credit/token {
    "gpt-4o": 0.01
    "claude-sonnet": 0.015
    "small": 0.0008
}

meter credits: credit = sum(
    (input_tokens * input_rate[model] + output_tokens * output_rate[model]) * (cached ? 0.1 : 1)
) from llm_call by model

meter seats: seat = count_distinct(user_id) from llm_call

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

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

plan team "Team" {
    fee "Team plan" 49 USD
    charge credits "AI credits" {
        included 50000 credit
        tiered graduated {
            up to 500000 credit at 0.0015 USD/credit
            above at 0.001 USD/credit
        }
    }
    charge seats "Seats" {
        price 5 USD/seat
    }
}

test "a small month" {
    plan starter
    events {
        llm_call { model: "gpt-4o", input_tokens: 12000, output_tokens: 3000, user_id: "ana" } x 40
        llm_call { model: "small", input_tokens: 2000, output_tokens: 500, user_id: "ben" } x 200
    }
    expect credits == 2560 credit
    expect seats == 2 seat
    expect total == 19.12 USD
}

test "a busy team" {
    plan team
    events {
        llm_call { model: "claude-sonnet", input_tokens: 40000, output_tokens: 8000, user_id: "ana" } x 1500
        llm_call { model: "claude-sonnet", input_tokens: 40000, output_tokens: 8000, user_id: "ben", cached: true } x 500
        llm_call { model: "small", input_tokens: 2000, output_tokens: 500, user_id: "cy" } x 3000
    }
    expect credits == 374400 credit
    expect charge credits == 486.60 USD
    expect total == 550.60 USD
}