Functional Weave
Code in Python

subscriptions.usage-tiered

Price metered usage against tiers, graduated or volume, with an optional flat fee per tier.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 25 tests, run in TypeScript, Python and Rust.

What it does

Tiered pricing for metered usage, in the two modes Stripe calls `graduated` and `volume`, with Stripe's `up_to` and `flat_amount` semantics. With tiers of 1-5 at 7.00, 6-10 at 6.50 and 11+ at 6.00 (Stripe's own worked example):

- **graduated**: each unit is priced in the tier it falls in. 6 units are 5 x 7.00 + 1 x 6.50 = 41.50; 20 units are 127.50. - **volume**: every unit is priced at the tier the whole quantity falls in. 6 units are 6 x 6.50 = 39.00, which is less than 5 units at 35.00 would suggest by extrapolation: volume totals can fall as usage rises across a boundary, and that is correct.

For example

  • price_usage(1, tiers ×3, volume) → lines ×1, total $7.00 Stripe volume example: 1 font at 7.00
  • price_usage(5, tiers ×3, volume) → lines ×1, total $35.00 Stripe volume example: 5 fonts, the top of the first tier
  • price_usage(6, tiers ×3, volume) → lines ×1, total $39.00 Stripe volume example: 6 fonts, all at the second tier's 6.50

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def price_usage(quantity: int, tiers: Sequence[UsageTier], mode: TierMode) -> UsageCharge
quantityintunits used in the period, 0 or more
tiersUsageTier[]in ascending order; only the last may be open-ended
modeTierModegraduated: each unit at its own tier's price; volume: every unit at the price of the tier the total falls in
returnsUsageCharge

The types it declares, generated into your project

TierMode = Literal["graduated", "volume"]

@dataclass(frozen=True)
class UsageTier:
    """One band of a tiered price, as Stripe's tiers spell it."""

    #: the last unit in this tier, inclusive; null for an open-ended top tier
    up_to: Optional[int]
    #: the price of each unit priced in this tier
    unit_price: Money
    #: charged once when usage reaches this tier; zero for none
    flat_fee: Money

@dataclass(frozen=True)
class TierCharge:
    """What one tier contributed to the charge."""

    #: 1-based position in the tiers list
    tier: int
    #: units priced in this tier
    quantity: int
    unit_price: Money
    flat_fee: Money
    #: quantity x unitPrice + flatFee
    amount: Money

@dataclass(frozen=True)
class UsageCharge:
    """The charge and the tiers it came from, ready to print as invoice lines."""

    lines: List[TierCharge]
    total: Money

Your code names it in one line, in the file that uses it

from fune.subscriptions.usage_tiered import price_usage  # subscriptions.usage-tiered@^1
impl/python.py · 60 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import List, Sequence

from .money_amount import assert_same_currency, money  ← from money.amount ^1.0.0 · built alongside by fune
from .money_sum import sum_money  ← from money.sum ^1.0.0 · built alongside by fune
from .subscriptions_usage_tiered_types import TierCharge, TierMode, UsageCharge, UsageTier


def _charge(tier: int, quantity: int, t: UsageTier) -> TierCharge:
    amount = money(quantity * t.unit_price.minor + t.flat_fee.minor, t.unit_price.currency)
    return TierCharge(tier=tier, quantity=quantity, unit_price=t.unit_price, flat_fee=t.flat_fee, amount=amount)


def price_usage(quantity: int, tiers: Sequence[UsageTier], mode: TierMode) -> UsageCharge:
    """Price ``quantity`` units against ascending tiers. Graduated prices each
    unit in its own tier and charges the flat fee of every tier reached; volume
    prices every unit at the tier the quantity falls in, plus that tier's flat
    fee. The first tier is always reached, so its flat fee applies at zero.
    """
    if isinstance(quantity, bool) or not isinstance(quantity, int) or quantity < 0:
        raise ValueError("quantity must be a whole number of 0 or more, received %r" % (quantity,))
    if mode not in ("graduated", "volume"):
        raise ValueError('unknown tier mode "%s": expected graduated or volume' % (mode,))
    if len(tiers) == 0:
        raise ValueError("tiered pricing needs at least one tier")
    currency = tiers[0].unit_price.currency
    previous = 0
    for i, t in enumerate(tiers):
        assert_same_currency(tiers[0].unit_price, t.unit_price)
        assert_same_currency(tiers[0].unit_price, t.flat_fee)
        if t.up_to is None:
            if i != len(tiers) - 1:
                raise ValueError("only the last tier may be open-ended (upTo null)")
        else:
            if isinstance(t.up_to, bool) or not isinstance(t.up_to, int) or t.up_to <= previous:
                raise ValueError(
                    "tier upTo values must be positive and strictly increasing, received %r after %d"
                    % (t.up_to, previous)
                )
            previous = t.up_to
    last = tiers[-1]
    if last.up_to is not None and quantity > last.up_to:
        raise ValueError("quantity %d exceeds the last tier, which ends at %d" % (quantity, last.up_to))

    lines: List[TierCharge] = []
    if mode == "volume":
        index = next(i for i, t in enumerate(tiers) if t.up_to is None or quantity <= t.up_to)
        lines.append(_charge(index + 1, quantity, tiers[index]))
    else:
        floor = 0
        for i, t in enumerate(tiers):
            # Tier 1 is always reached; a later tier only once usage passes the
            # previous tier's last unit.
            if i > 0 and quantity <= floor:
                break
            top = quantity if t.up_to is None else min(quantity, t.up_to)
            lines.append(_charge(i + 1, top - floor, t))
            if t.up_to is None:
                break
            floor = t.up_to
    return UsageCharge(lines=lines, total=sum_money([line.amount for line in lines], currency))

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add subscriptions.usage-tiered
Download for Python subscriptions.usage-tiered-1.0.0-python.fune · 28,681 bytes sha256 fc77a831e7d5d6ee6059e47367c74aaf18fbe5a7f03cb6ece725950101ee6b52

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./subscriptions.usage-tiered-1.0.0-python.fune, or fetch it from a terminal with fune pull subscriptions.usage-tiered@1.0.0:python.

The whole function, every language, is one file too: subscriptions.usage-tiered-1.0.0.fune, 36,389 bytes, sha256 a95d14505f62a99c7864d1763d9cb2cef7fb5c178ad87c07060a7fe269a41a97. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before subscriptions.usage-tiered

after — your function gets the result and the arguments, and returns the final result.

# fune: after subscriptions.usage-tiered

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

# fune: replace money.amount in subscriptions.usage-tiered
# fune: replace money.sum in subscriptions.usage-tiered

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show subscriptions.usage-tiered --steps.

# fune: step subscriptions.usage-tiered after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
Stripe volume example: 1 font at 7.00 1, tiers ×3, volume → lines ×1, total $7.00
Stripe volume example: 5 fonts, the top of the first tier 5, tiers ×3, volume → lines ×1, total $35.00
Stripe volume example: 6 fonts, all at the second tier's 6.50 6, tiers ×3, volume → lines ×1, total $39.00
Stripe volume example: 20 fonts, all at 6.00 20, tiers ×3, volume → lines ×1, total $120.00
Stripe volume example: 25 fonts 25, tiers ×3, volume → lines ×1, total $150.00
Stripe graduated example: 5 fonts 5, tiers ×3, graduated → lines ×1, total $35.00
Stripe graduated example: 6 fonts is 35.00 plus one at 6.50 6, tiers ×3, graduated → lines ×2, total $41.50
Stripe graduated example: 20 fonts 20, tiers ×3, graduated → lines ×3, total $127.50
Stripe graduated example: 25 fonts 25, tiers ×3, graduated → lines ×3, total $157.50
Stripe flat-rate example, volume: 12 x 3.00 + 30.00 12, tiers ×5, volume → lines ×1, total $66.00
Show the other 15 tests
CaseArgumentsExpected
Stripe flat-rate example, graduated: three tiers and three flat fees 12, tiers ×5, graduated → lines ×3, total $111.00
no usage still bills the first tier's flat fee, graduated 0, tiers ×5, graduated → lines ×1, total $10.00
no usage still bills the first tier's flat fee, volume 0, tiers ×5, volume → lines ×1, total $10.00
usage of exactly a tier's upTo does not reach the next tier or its flat fee 10, tiers ×5, graduated → lines ×2, total $75.00
one unit past a boundary reaches the next tier and its flat fee 11, tiers ×5, graduated → lines ×3, total $108.00
a free allowance, then 2p a unit, then 1p a unit 25,000, tiers ×3, graduated → lines ×3, total £330.00
a single open-ended tier is plain per-unit pricing 42, tiers ×1, graduated → lines ×1, total £10.50
a closed last tier prices usage right up to its end 200, tiers ×2, volume → lines ×1, total £10.00
usage past a closed last tier is an error 201, tiers ×2, graduated → error: exceeds the last tier
a negative quantity is an error -1, tiers ×3, graduated → error: quantity must be a whole number of 0 or more
no tiers is an error 5, , volume → error: at least one tier
tiers out of order are an error 5, tiers ×3, graduated → error: strictly increasing
an open-ended tier before the last is an error 5, tiers ×2, graduated → error: only the last tier may be open-ended
tiers in two currencies are an error 5, tiers ×2, graduated → error: currency mismatch
an unknown mode is an error 5, tiers ×3, stairstep → error: unknown tier mode

More from the author

A tier's `upTo` is the last unit in it, inclusive, so usage of exactly 10 against tiers ending at 5 and 10 is wholly inside the first two tiers and does not reach the third, flat fee included. The last tier may be open-ended (`upTo` null); if it is not, usage beyond it is an error rather than being silently priced at the last rate.

Flat fees. In graduated mode a tier's flat fee is charged once when usage reaches the tier; in volume mode only the flat fee of the tier the quantity falls in is charged. The first tier is always reached: usage of 0 still charges the first tier's flat fee, in both modes, which is what Stripe does ("Stripe always bills the first flat rate tier when quantity=0"). To charge nothing for no usage, make the first tier `upTo` 1 with a unit price instead of a flat fee.

The result lists one line per tier that was charged, in tier order, with the units priced in it, so an invoice can show the breakdown; `total` is their sum. Everything is exact integer arithmetic in minor units - no rounding happens anywhere. A per-unit price smaller than one minor unit (0.1p per API call) cannot be written as Money; meter such usage in packages (per 1,000 calls) instead.

Errors: no tiers, a negative quantity, `upTo` values that are not positive and strictly increasing, an open-ended tier before the last, usage beyond a closed last tier, and prices in more than one currency.

Source: Stripe, "Set up tiered pricing", https://docs.stripe.com/subscriptions/pricing-models/tiered-pricing (the vectors reproduce its volume, graduated and flat-rate examples).

Files

PathBytes
README.md2,226
impl/python.py2,984
impl/rust.rs4,621
impl/typescript.ts2,809
vectors.json17,155