Blog
Keys1 min read

Spend caps and model lists on every key

Give each project its own key, a budget it cannot exceed, and the list of models it is allowed to call. When a key reaches its cap, it stops on its own.

A balance is shared by every key on an account. A spend cap is not: it belongs to one key, and once the key has spent that much it is refused until you raise the cap or create a new key.

One key per thing

Create a key for each client, environment or job. Each shows its own spend in the dashboard, and each can be revoked without touching the others. A revoked key is refused with a 401 on its next request, and a hold can never be taken for it: revocation is re-checked in the same transaction that takes credit.

The cap is checked before the request runs

Before a request is forwarded, the gateway holds the most it could cost against the key’s cap as well as against your balance. A request that could take the key past its cap is refused with HTTP 402 and the code spend_cap_exceeded, and nothing is charged. Parallel requests on the same key cannot slip past the cap together, because the hold is taken atomically.

The model list

A key can carry a list of model IDs it may call. A request for any other model is refused with HTTP 403 and the code model_not_allowed. Leave the list empty and the key can call every enabled model.

HTTP/1.1 402 Payment Required
{"error":{"code":"spend_cap_exceeded","message":"This key reached its spend cap."}}

HTTP/1.1 403 Forbidden
{"error":{"code":"model_not_allowed","message":"The key's model list excludes this model."}}

The key is shown once

Only a hash of the key is stored. The full key appears once, when it is created, and cannot be recovered after that. Store it when you see it; if it is lost, revoke it and create another.