Agent Wranglers

USG-15Required

Let customers cap their spend

Let every customer on a usage or overage plan set a monthly spend cap. Refuse metered work that would pass it, before the work starts.

How to do it

An agent stuck in a loop or a leaked API key can run up a bill of thousands in a night. The customer then disputes the charge, and you have paid the model provider already. Set a default cap on new accounts, such as twice the plan fee, and let the owner change it. Check the cap in the same function as the balance in Decide in writing what happens at a zero balance. Count open holds as spent. Refuse with a clear error, such as HTTP 402 with a link to the billing page. This is the customer's cap. Your own caps per user and feature stay as in Price every call and cap the spend.

Examples

JavaScript example

export async function checkSpendCap(customerId, maxCostMicroUsd) {
  const { capMicroUsd } = await billing.settings(customerId)
  const spent = await billing.spentThisPeriodMicroUsd(customerId)
  const held = await billing.openHoldsMicroUsd(customerId)
  if (spent + held + maxCostMicroUsd > capMicroUsd) {
    throw new HttpError(402, 'Monthly spend cap reached', { billingUrl: '/settings/billing' })
  }
}

Python example

def check_spend_cap(customer_id: str, max_cost_micro_usd: int) -> None:
    cap = billing.settings(customer_id).cap_micro_usd
    spent = billing.spent_this_period_micro_usd(customer_id)
    held = billing.open_holds_micro_usd(customer_id)
    if spent + held + max_cost_micro_usd > cap:
        raise HttpError(402, "Monthly spend cap reached", billing_url="/settings/billing")

How to check

A test sets a cap of 10 USD on a customer who has spent 9.50 USD this month. A call that can cost up to 1 USD is refused with status 402 and no provider call. With a cap of 20 USD the same call runs.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/usage-billing/spend-caps-and-alerts/customer-spend-cap">Agent Wranglers usage-based billing playbook</a>. In code you write, add a comment next to it with the same link.