Attributing Model Spend When Everyone Shares One API Key

2026-12-03来源 dev.toEnglish

Shared bearer tokens cannot attribute spend or be revoked for one caller without breaking others. RFC 6750 says any party possessing a bearer token can use it without proof of a separate key; it requires TLS and warns that disclosure permits unintended use. Use per-caller credentials at a gateway.

Scope and assumptions

This is proposed practice, not a verified deployment; no part has been run. It assumes a TLS-terminating gateway, request metadata stored with caller identity, and the gateway as accounting truth. Direct provider paths are out of scope.

Steps

  1. Keep the provider key in the gateway's secret configuration; stop distributing it.
  2. Issue one gateway credential per person or service and map each to a stable identity.
  3. At request start, write identity, model, and timestamp. After the response, update token counts; a failed call leaves a row without them.
  4. Revoke per credential. Removing one should produce 401 for that caller only.

Test

Use two test identities and send one request from each through the gateway. Inspect the log store for two rows with distinct identities and one credential each. Revoke A, repeat both requests, and observe A receives 401 while B succeeds. Roll the versioned gateway configuration back one revision and confirm routing returns to the prior state.

If both callers appear under one identity, treat it as a mapping or forwarding bug and fix it before trusting cost reports; do not assume one cause. If revoking A breaks B, credentials are not separate. If the gateway runs as a Kubernetes Deployment, rolling back restores the Pod template but not unrelated data, so usage rows remain. See RFC 6750 and Kubernetes Deployment docs.

Limitations

Attribution depends on gateway coverage; logs prove requests, not intent. Direct provider paths create unattributed spend.

This article was created with AI assistance.

原文:https://www.a8q9.cn/en/article/attributing-model-spend-when-everyone-shares-one-api-key-0c9a59.html

相关文章