guides/billing + sub-accounts

Mint per-customer API keys with sub-accounts

5 min readpublished 2026-04-22category · Billing + sub-accounts

Give each of your end users their own scoped AIgateway key with a spend cap, rate limit, default cost tag, and isolated analytics. One POST per customer.

If you're shipping an AI product to multiple end users, you eventually face four problems: (1) one customer burning through your monthly budget, (2) another customer wanting their own usage report, (3) suspect activity you can't trace to a tenant, (4) a billing page that needs per-customer cost data.

The standard fix is to build it: tag every request internally, query a usage table, run nightly aggregation. Six months of work, plus on-call.

AIgateway's sub-account API replaces all of that with one POST. Each call mints a scoped key with its own spend cap, rate limit, default tag, and per-customer analytics.

Mint a key per customer

Call this once per customer signup. Hand the returned key to your customer or use it server-side per-request — your choice.

curl -X POST https://api.aigateway.sh/v1/sub-accounts \
  -H "Authorization: Bearer sk-aig-..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "acme-corp",
    "spend_cap_cents": 50000,
    "rate_limit_rpm": 300,
    "default_tag": "acme"
  }'

# => { "id": "sa_9f3k...",
#      "key": "sk-aig-...",
#      "spend_cap_cents": 50000 }

Use the scoped key

From there, you (or your customer) call the gateway with the new key. Spend, rate limits, and analytics are all isolated.

from openai import OpenAI

# This is the customer-scoped key from /v1/sub-accounts.
client = OpenAI(
    base_url="https://api.aigateway.sh/v1",
    api_key="sk-aig-...",   # sub-account key
)

resp = client.chat.completions.create(
    model="anthropic/claude-sonnet-4.6",
    messages=[{"role": "user", "content": "..."}],
)

Read per-customer usage

Pull a single customer's spend by sub-account ID, broken down by model + day.

curl https://api.aigateway.sh/v1/sub-accounts/sa_9f3k.../usage?window=30d \
  -H "Authorization: Bearer sk-aig-..."

# => { "summary": { "requests": 14210, "cost_cents": 3402, ... },
#      "by_model": [ { "model_id": "anthropic/claude-sonnet-4.6", ... } ],
#      "by_day":   [ { "date": "2026-04-21", "requests": 612, ... } ] }

Adjust caps mid-month

Bump or revoke a customer's cap any time. Enforcement runs before dispatch — they can never silently exceed their cap.

curl -X PATCH https://api.aigateway.sh/v1/sub-accounts/sa_9f3k... \
  -H "Authorization: Bearer sk-aig-..." \
  -d '{ "spend_cap_cents": 100000 }'
Once you're billing a meaningful number of customers through sub-accounts, hit /v1/usage/by-tag to see your top spenders, top features, and cost trend per tag. Most teams discover their per-feature cost is 3x what they assumed.
READY TO SHIP?
Get an AIgateway key in 30 seconds. Free Kimi K2.6 through Apr 30; everything else is pass-through.
Get a key →API reference

Related guides