Skip to content

API keys: per-key spend tracking and user-set budgets #1139

Open

Description

@AnthonyRonning

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:

  1. 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).
  2. 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

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's public_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

  1. 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_access in services/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.
  2. 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.
  3. 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.
  4. 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?
  5. Teams. Team usage is pooled. Whether a team admin can see or cap members' keys is a product question.
  6. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions