Aurora Meter - real-time usage metering and plan entitlements for Phoenix

Every SaaS I have built on Phoenix ended up with the same three hand-rolled pieces: count what each customer uses, stop them at their plan’s limit, and bill for the rest. There is no Elixir equivalent of Laravel Cashier for the billing half, and OpenMeter ships SDKs for Node, Python and Go but not for us. So I extracted the layer I kept rewriting into a library.

Aurora Meter does the counting and gating in a free MIT core:


# count: an ETS increment, nothing touches the database
AuroraMeter.track(org, :ai_generations)

# gate, run and meter atomically
AuroraMeter.with_quota(org, :ai_generations, fn ->
  generate_report()
end)
# => {:ok, result} | {:error, :limit_exceeded} | {:error, :not_entitled}

Plans are a small compile-time DSL with hard caps, metered overage and feature flags:

defmodule MyApp.Plans do
  use AuroraMeter.Plans

  plan :free do
    limit :ai_generations, 50, :hard
  end

  plan :scale do
    metered :ai_generations, included: 1_000, unit_price: 2
    feature :api_access, true
  end
end

How it is built. Increments hit a shared ETS table with :ets.update_counter/4, so there is no GenServer per tenant and nothing on the database write path. A single flusher persists snapshots to Postgres on an interval and on shutdown, and a broadcaster fans live values over PubSub. Two LiveView components read those counters, so a usage bar in your admin updates within a second of the increment on the server. Here it is with a burst of traffic driven from the BEAM:

usage-meter

with_quota/4 reserves first (increment, compare, roll back on breach), runs the function, and releases the reservation if it raises. The reservation is the usage, so two concurrent calls cannot both squeeze through the last unit of a hard limit.

The bundled benchmark (mix aurora_meter.bench 8 500000, dev laptop, Elixir 1.20 / OTP 29) does about 7.9M increments/s across distinct counters, and about 53k/s when all eight processes hammer one counter. The run is in the repo under docs/evidence.

What it does not do yet, honestly:

  • Buffered by default: a hard crash can lose up to one flush interval of increments. Durable features exist for anything you invoice.
  • Periods are UTC calendar months in the core. Postgres only.

Links: Hex · HexDocs · GitHub:

The core is MIT forever, no email gate. There is a paid Pro package for when Stripe should start charging (checkout, webhook sync, usage reported to Billing Meters, dashboards), but everything above is in the free library.

I would genuinely like to hear what metering shapes people have hand-rolled in their own apps, and what you would want gated that this does not cover.

6 Likes

Aurora Meter 1.0 is out (and 1.0.1 with it)

Since the last update here, Aurora Meter has reached 1.0. If you haven’t seen it: it’s an MIT library for the three jobs every Phoenix SaaS ends up hand-rolling. It counts what each customer uses, stops them at their plan’s limit, and bills for the rest.

# count: an ETS increment, nothing touches the database
AuroraMeter.track(org, :ai_generations)

# gate, run and meter in one step
AuroraMeter.with_quota(org, :ai_generations, fn -> generate_report() end)
# => {:ok, result} | {:error, :limit_exceeded} | {:error, :not_entitled}

Plans are a small DSL, checked at compile time:

plan :pro do
  price 2_000
  limit :ai_generations, 1_000, :hard
  feature :seats, 5
end

And <.usage_meter quota={@quota} /> gives you a meter that updates live over PubSub.

What 1.0 means

  • The public API is stable and follows SemVer. The support policy says what a patch, a minor and a major may change, and which Elixir and OTP versions are covered.
  • The schema is versioned (AuroraMeter.Migration.latest_version() is 10). Coming from 0.4 there’s a step-by-step upgrade guide that covers what each step locks and does to your data.
  • New in 1.0: a prepaid credits ledger with holds and settles (lots, expiry, refunds), durable events with idempotent ids, and plan versions, so editing a plan doesn’t silently reprice customers who are pinned to an older version.

What 1.0.1 fixes

  • The live meter now updates when you follow the README. Before this, subscribe/1 was enough to wire it but the meter stayed at zero.
  • There’s a recipe for Phoenix 1.8 phx.gen.auth scopes, and an unknown tenant struct now gives a clear error instead of a protocol error.
  • The installer writes your real module names and test config, and adds the plans DSL to your formatter.
  • It compiles again on the documented floor, Elixir 1.15.
  • It ships a usage-rules.md, so if you use AI coding tools, mix usage_rules.sync teaches them the API.

Charging for LLM tokens

This is the use case I hear about most, so 1.0.1 includes a guide: Charge customers for LLM tokens with ReqLLM. It holds an estimate before the call and settles from the usage the response reports. A customer at zero is refused before any request leaves your server. It works with the free core alone.

What isn’t there yet

Being honest about this matters more to me than the feature list:

  • Postgres only.
  • Daily or hourly limits per feature (“20 a day on Free”) aren’t built in yet. Today you’d pair Aurora Meter with Hammer for that.
  • There’s no non-resetting gauge for seats or projects yet.

These three are what I’m looking at for 1.1, and they’ll stay in the MIT core. If one of them would decide whether you use Aurora Meter, please say so here; it changes the order.

Aurora Meter Pro

If you need to charge for usage through Stripe, there’s a commercial add-on. It does Stripe checkout and subscriptions, and prepaid credit top-ups with auto-recharge, with refunds and disputes taken back off the balance. Metered usage goes to Stripe Billing Meters and reaches the invoice once, with reconciliation. Its docs are public at aurorameter.com/docs/aurora-meter-pro, and pricing is at aurorameter.com/pro. The free core has no limits, no email gate and no trial, and nothing in it calls home.

Links: Hex · Docs · GitHub · Changelog

Questions, bug reports and “this doesn’t fit my app because…” are all welcome. I’ll post an update here with each release.

2 Likes