omnigate

Prepaid credit

A key with a total budget is a prepaid key: what it holds is credit, bought in advance and used up by its requests. This page is the rule that credit follows. It is the same rule whoever paid and however: a top-up through the admin API, a payment through a payment provider, a bonus.

With nothing configured the rule changes nothing: a prepaid key is billed what the providers cost, as before, and earns no bonus.

The rule

1 credit      = 1 US dollar, at the moment a top-up is applied
balance       = top-ups + bonuses − burns
burn          = what the provider cost × the burn multiplier
weekly bonus  = average bonus-eligible balance over the week
                × the annual bonus rate ÷ 52

A key at or below zero is blocked: its requests are answered 402 budget_exceeded until it is credited again. The request that crosses zero is completed and paid for; the balance then stands a little below zero, and the next credit pays that off first.

Credit cannot be withdrawn or refunded. There is no call that pays any out, to anyone. It can only be spent on requests through this gateway. An operator can lower a key's budget by hand (PATCH /admin/keys/{name}), which takes credit away; nothing sends it anywhere.

Amounts are IEEE-754 doubles in US dollars, like every cost in the ledger. Nothing is rounded when it is stored or compared; listings round to six decimals. A bonus of less than a millionth of a dollar is not credited.

Which keys

A key is prepaid when it has budget_usd and its budget_period is total (the default period). Everything on this page applies to such keys and to no others:

Key Burn multiplier Bonus Top-ups
budget_usd set, period total applies applies, to eligible credit managed keys only
budget_usd set, period monthly or daily no: billed the provider cost no refused (409 not_prepaid)
no budget_usd no no refused (409 not_prepaid)

A key in the config file can be prepaid (its budget is then fixed in the file, and the burn multiplier applies to it), but it cannot be topped up or earn a bonus: both change a key's budget, and the gateway does not write the config file.

The burn multiplier

credits.burn_multiplier says how much credit one dollar of provider cost uses up. At 1.2, a request that cost the provider $0.50 burns $0.60 of the key's credit; a key holding $1.20 is blocked once the providers have cost $1.00.

Worked example at 1.2:

Provider cost Burned Balance
Top-up of $10 10.00
A request costing $0.25 0.25 0.30 9.70
A media job costing $2.00 2.00 2.40 7.30
Requests costing $6.10 in all 6.10 7.32 −0.02 (blocked)

Two kinds of credit

Every top-up says whether it is bonus-eligible. A prepaid key therefore holds two balances: eligible credit, and the rest ("plain" credit).

Example: a key holds $10 plain and $10 eligible. Requests burn $15: the plain $10 goes first, then $5 of the eligible credit. The key now holds $5, all of it eligible.

The weekly bonus

With credits.bonus_annual_rate above zero, every prepaid managed key earns a bonus on its eligible credit once a week:

bonus = average eligible balance over the week × rate ÷ 52

At 5% a year, $100 of eligible credit held all week earns 100 × 0.05 ÷ 52 = $0.0962.

Worked example at 5% (0.0962% a week), multiplier 1.2:

When What Eligible Plain
Monday $100 arrives through a payment provider 100.00 0
Wednesday Requests costing the providers $20 burn $24 76.00 0
Next Monday 00:00 Average was (100×2 + 76×5) ÷ 7 = 82.86; bonus 82.86 × 0.05 ÷ 52 = 0.0797 76.0797 0

Settings

In the config file, all optional:

"credits": {
  "burn_multiplier": 1.2,
  "bonus_annual_rate": 0.05,
  "sample_interval_s": 900,
  "admin_topups_eligible": false
}
Field Default Meaning
burn_multiplier 1 Credit burned per dollar of provider cost, for prepaid keys. 0.01 to 100.
bonus_annual_rate 0 The weekly bonus's rate per year, as a fraction: 0.05 is 5%. 0 to 10. 0 switches the bonus off.
sample_interval_s 900 How often eligible balances are sampled while a bonus rate is set. 1 to 86,400.
admin_topups_eligible false Whether a top-up through the admin API is bonus-eligible when the call does not say.

An unknown field in credits is refused when the config is loaded. Per key: burn_multiplier (see above).

The config file is re-read when it changes, so a new multiplier or rate takes effect without a restart: the multiplier for the usage that arrives from then on, the rate for the weeks closed from then on.

Seeing it

What it does not do

This page on GitHub