Repository navigation
Open
Description
Context
Follow-up to #1138, which gives each API key a non-secret public_id and has the billing server record which key made each billed request. Once that exists, the same records can answer bigger questions users have asked about: how much each key has spent, and how to stop one key from spending too much.
This is a future enhancement. It doesn't need to ship with #1138 or all at once; the two parts below can land separately.
User intent:
- Spend per key. On the API keys page, each key shows what it has used this billing period and in total, in credits (the unit API users already buy).
- Budgets per key. A user can set a monthly credit limit on a key. Once the key reaches it, requests with that key are refused with a clear "key budget exceeded" error, while the user's other keys keep working.
Likely approach
Ownership stays as in #1138: the enclave keeps the key secret, name and authentication; the billing server owns usage, budgets and the allow/deny answer.
Spend per key
- The billing server sums each key's stored usage for the current period and all time, and returns it from the same per-key usage endpoint API keys: show when each key was last used #1138 adds.
- The UI shows it beside "last used".
Budgets per key
- Users create, change and remove a key's budget through the billing server (e.g.
/v1/maple/api-keys/{public_id}/budget), behind the existing user token auth. - The enclave reads budgets only through its existing admin path: OpenSecret already asks the billing server whether a user may proceed before each chat and System One request (
chat_billing_access,services/opensecret/src/billing.rs). It would pass the key'spublic_id, and the billing server would fold the key's budget into the answer and return a reason. - OpenSecret maps that reason to a distinct API error, separate from "out of credits".
Decisions to make
- Fail-open vs fail-closed. If the billing check fails or times out, OpenSecret currently lets the request through on free models (
AppState::chat_billing_accessinservices/opensecret/src/main.rs). For a plan quota that's a business choice; for a limit a user set themselves, they may expect a hard stop. Options: call budgets "soft caps" and document it, or fail closed for API-key requests when the check fails. - Budgets are approximate. Usage is recorded asynchronously after each request, so concurrent requests can overshoot a limit. Other providers' budgets behave the same way; we would document it rather than build reservations.
- Route coverage. Only chat completions and System One run the billing check. Embeddings, speech and transcription only check guest status today, so a key budget wouldn't stop them until they do.
- Key deletion. Usage and budget rows outlive a deleted key. Should OpenSecret tell the billing server when a key is deleted, as it already does for account deletion?
- Teams. Team usage is pooled. Whether a team admin can see or cap members' keys is a product question.
- Later ideas, not this issue: spend alerts, key expiry dates, per-key model allowlists. An allowlist is authorization, so the enclave would enforce it even if the setting is stored in billing.
Acceptance criteria
Spend per key
- Each key shows credits used this period and in total.
- No key secret, hash or name leaves the enclave.
Budgets per key
- A user can set, change and remove a monthly credit limit on a key.
- Over the limit, that key's requests are refused with a distinct error; other keys are unaffected.
- The behaviour when the billing server is unavailable matches the decision above and is documented for API users.
Non-goals
- Changing how plan quotas or API credits work for the account as a whole.
- Any change to where key secrets are stored or how keys authenticate.
Metadata
Metadata
Assignees
Labels
No labels
Activity