Aurora Meter Pro

Sell usage on plain Stripe, in Phoenix, without double charging and without giving credit away.

Pro is the commercial tier for Aurora Meter. The free core counts usage and enforces plans. Pro is the part that touches money, and it does two things nothing else in Elixir does for you:

  • Real-time prepaid credits. A customer buys credit through Stripe Checkout and spends it as they go, with the balance checked before the work runs, not reconciled at the end of the month. Top-ups, auto-recharge on the saved card when the balance runs low, promotional credit that expires, and refunds and disputes that take back exactly the credit they paid for (and give it back when you win the dispute or the refund fails). All of it on ordinary Stripe objects: PaymentIntents and Checkout Sessions, no Stripe Billing credit grants and no Metronome.
  • Metered usage that reaches the Stripe invoice once. Overage is staged from persisted counters, sent to Stripe Billing Meters under an identifier Stripe deduplicates, never resent outside Stripe’s idempotency window, and checked afterwards: a reconciler compares Stripe’s meter totals with yours and opens a finding for every disagreement, so a missing or extra charge is something you are told about rather than something a customer finds.

Every Stripe event can arrive twice, late, out of order or not at all, so every write Pro makes is keyed on something Stripe repeats (the PaymentIntent id, the dispute id, the meter event identifier). A redelivery finds the work done and changes nothing.

The common case: charging for AI usage

An LLM call costs you money in proportion to tokens, and you only know the real cost after it returns. Hold the worst case from the customer’s prepaid balance, run the call, and settle what it actually cost:

# Hold up to $0.05, run the model, settle the real cost in micro-dollars.
AuroraMeter.Credits.with_credits(org, 50_000, "chat:#{message.id}", fn ->
  with {:ok, reply} <- MyApp.LLM.chat(prompt) do
    {:ok, reply, MyApp.LLM.cost_micro(reply.usage)}
  end
end)
#=> {:ok, reply}, or {:error, :insufficient_credits} before the model is called

A customer who runs out is refused before you pay the model provider, not invoiced afterwards. When the balance crosses their threshold, Pro charges their saved card off-session and credits the ledger from Stripe’s webhook. The customer’s dashboard updates live as they spend.

Prefer to bill on the monthly invoice instead? Count the usage and let Pro report it:

AuroraMeter.track(org, :ai_tokens, reply.usage.total_tokens)

AuroraMeter.Pro.UsageReporter turns the counter into meter events on the customer’s subscription, and the graduated price on the Stripe side does the included-allowance arithmetic.

Install

Pro is served from the Aurora Meter package repository, not from hex.pm. Add the repository once on each machine (and in CI, see Installing Pro in CI, Docker and Fly) with the key from your account page:

mix hex.repo add aurorameter https://aurorameter.com/repo \
  --fetch-public-key SHA256:+g07CrUcsckpovkT9THeaiPWYLyyAP0p/OS+iZ1qTT8 \
  --auth-key <KEY>

Then declare both packages:

def deps do
  [
    {:aurora_meter, "~> 1.0"},
    {:aurora_meter_pro, "~> 1.2", repo: "aurorameter"}
  ]
end

Pro 1.x requires core 1.x. It declares aurora_meter as >= 1.0.0-rc.1 and < 2.0.0, so a host that upgrades Pro while its core is still on 0.4.x fails to resolve rather than compiling into something that does not work. Upgrade core first.

With core installed (mix aurora_meter.install), the installer writes the configuration (placeholders only, never a secret), adds AuroraMeter.Pro to your supervision tree, generates the migration and schedules the workers:

mix aurora_meter_pro.install --repo MyApp.Repo
mix ecto.migrate

This release uses core schema version 10 and Pro schema version 11. On an existing install, generate an upgrade migration from the first version you do not have yet (mix aurora_meter_pro.gen.migration -r MyApp.Repo --from N); the upgrading guide has the order.

Then mount the webhook in your endpoint, above Plug.Parsers, because Stripe signs the raw body:

# lib/my_app_web/endpoint.ex
plug AuroraMeter.Pro.Webhook, path: "/webhooks/stripe"

It handles only a POST to that path and passes every other request through. In Stripe, create the endpoint with the Snapshot payload style (not Thin, which Stripe suggests for new destinations and which carries no object for Pro to apply) and subscribe it to the events in Getting started.

Configure the Stripe side:

config :aurora_meter,
  provider: AuroraMeter.Pro.Stripe,
  period_source: AuroraMeter.Pro.Period,
  credits_low_balance_handler: &AuroraMeter.Pro.Credits.on_low_balance/1

config :aurora_meter_pro,
  stripe_account_id: {:system, "STRIPE_ACCOUNT_ID"},
  stripe_prices: %{pro: "price_flat_pro", scale: "price_flat_scale"},
  # usage-based prices on a Billing Meter, attached at checkout so overage is invoiced
  stripe_metered_prices: %{scale: ["price_scale_overage"]},
  stripe_meters: %{ai_tokens: "ai_tokens"},
  webhook_secret: {:system, "STRIPE_WEBHOOK_SECRET"},
  alert_handler: &MyApp.Billing.quota_alert/1

The installer schedules the workers for you. By hand, take the whole list from both packages:

config :my_app, Oban,
  queues: [aurora_meter: 5],
  plugins: [
    {Oban.Plugins.Cron,
     crontab: AuroraMeter.Oban.cron_entries() ++ AuroraMeter.Pro.cron_entries()},
    {Oban.Plugins.Lifeline, rescue_after: :timer.minutes(20)}
  ]

In Stripe, give each metered price a graduated tier with the included allowance at $0 and the overage unit price above it; the reporter sends total usage deltas and Stripe applies the tiers.

What else it adds

  • Subscriptions: AuroraMeter.Billing.checkout/3 and portal_url/2 via Stripe, plan versions mapped to prices, and plan changes Stripe applies first.
  • Dashboards: a styled, themeable usage_dashboard/1 component and a mountable LiveView, both live. A tenant with a prepaid ledger gets a money section first (balance, spend this period, runway, spend chart), then quota cards, daily charts and monthly history. Plus a LiveDashboard page for operators.
  • Request audit log: one row per API call with the actor, route, status, duration and cost, filtered and cursor-paged.
  • Alerts: quota-threshold telemetry and a handler callback.
  • Rollups: daily and monthly aggregates and CSV export.
  • Recovery: a guarded operator API and mix aurora_meter_pro.recovery for the payments and meter reports whose outcome nobody knows yet.

Top-ups and auto-recharge

# One-off top-up: send the customer to Stripe Checkout
{:ok, url} = AuroraMeter.Pro.Credits.checkout(org, 2_500,
  success_url: url(~p"/billing?topped_up=1"), cancel_url: url(~p"/billing"))

# Auto top-up: $25 whenever the balance drops below $5
{:ok, _account} = AuroraMeter.Pro.Credits.update_auto_top_up(org, %{
  auto_top_up_enabled: true, threshold_micro: 5_000_000, amount_cents: 2_500})

The ledger is credited from the webhook, keyed on the PaymentIntent id, so a redelivered event never funds an account twice. An auto top-up keeps its idempotency key until Stripe gives a definite answer, so a timed-out charge is retried under the same key rather than charged again; one whose outcome is still unknown after 23 hours is surfaced for reconciliation and never charged again automatically. Three declines in a row switch auto top-up off and tell you, with Stripe’s decline code as the reason.

Top-ups are in cents and require the Stripe and ledger currencies to match; Pro does no currency conversion. See Top-ups for the configuration, tax, refunds and disputes.

In tests, config :aurora_meter_pro, credits_stripe_client: AuroraMeter.Pro.Credits.StripeClient.Fake records every call and never touches the network.

Guides

Before you depend on it

  • Architecture: what Pro owns, which direction the dependency runs, and the export and payment paths end to end.
  • Correctness: per invariant, the prerequisites, the known limits and the tests.
  • Troubleshooting: symptom first, for when something is visibly not happening.
  • SECURITY.md, in the package: what Pro stores, how key material is handled, what the host is responsible for, and how to report a vulnerability.

The contract for everything below Pro is the free core’s guarantee page. It covers Pro as well, because the rows that matter here (usage export is at-least-once with provider-side idempotency, scheduled work is at-least-once with idempotent effects, a provider invoice is never reported as finalised or paid) are properties of the whole product.

Reference

  • API inventory: every supported function, behaviour, struct, configuration key, telemetry event and Mix task, each with a stability class. If it is not on that page, it is not supported, even where the code happens to be public.
  • Support policy: what SemVer covers here, how schema changes work in 1.x, how long a deprecated API lives, and what working against a payment provider does and does not let this package promise.

Supported versions

Requires Elixir 1.15 or later with Erlang/OTP 26 or later; the Stripe client’s HTTP stack pulls in quic, which declares {minimum_otp_vsn, "26"}, so OTP 25 cannot compile it. CI runs the whole test suite on each pair below, on every push:

Elixir Erlang/OTP
Floor 1.15.8 26.2.5.21
Supported 1.18.5 27.3.4.17
Current 1.20.4 29.0.6

The floor is a compatibility guarantee, not a recommendation: Elixir 1.15 is past its last release and Erlang/OTP 26 is past its upstream maintenance window.

Postgres: 16.13 is the tested version and 13 is the floor.

License

Commercial: see LICENSE.commercial, in the package. Plans, the free trial and your package key: https://aurorameter.com/pricing. Licence terms and refunds: https://www.phxtemplates.com/licensing. Aurora Meter is built by the team behind PhxTemplates.