Skip to content

Virtual keys: per-team budgets and rate limits for LLMs

Virtual keys are gateway-issued credentials that stand in for provider keys. How to structure them, enforce AI budgets and rate limits, and fail callers safely.

Published
Reading time
10 min read

In short

  • A virtual key is a credential a gateway issues in place of a provider key. Applications hold the virtual key, and the real provider keys never leave the gateway.
  • Place every virtual key in a hierarchy, such as customer, team, and key, and check each level on every request. Any level that is out of budget blocks the call.
  • Budget caps always overshoot a little. Cost is known only after the response, so a check made before the call lets the last request run past the limit.
  • Return a budget error that clients cannot mistake for a rate limit. Providers often answer both with 429, and a retry loop that sees 429 will keep trying against a budget that resets in hours.

Virtual keys are credentials that an LLM gateway issues to applications in place of real provider API keys. An application authenticates to the gateway with its virtual key. The gateway checks that key's permissions, budget, and rate limits, then forwards the request with a provider key the application never sees.

The idea solves two problems at once. Provider keys stop spreading across repositories, CI secrets, and laptops. And every request now carries an identity you can meter, so spend has an owner before the invoice arrives instead of after.

What a virtual key replaces

A virtual key replaces the provider key in every place an application would otherwise hold one. That turns three hard operations into config changes: revoking one caller, rotating a provider key, and attributing spend to a team or feature.

Compare the two models when something goes wrong:

  • A provider key leaks from a notebook. With shared provider keys, you rotate the key and redeploy every service that uses it. With virtual keys, you revoke the one virtual key that leaked, and no other caller notices.
  • Finance asks which feature spent the money. With shared provider keys, the provider's dashboard shows one account. With virtual keys, every request is already tagged with the key, and so with its team and application.
  • A provider retires a model. With virtual keys, the gateway's model list changes in one place.

Most gateways accept the virtual key in the header the client SDK already sends, so the application change is a base URL and an environment variable.

client.ts
import Anthropic from "@anthropic-ai/sdk";
 
// The SDK sends this as x-api-key. The gateway reads it as a virtual key.
const anthropic = new Anthropic({
  baseURL: "https://llm-gateway.internal.example.com/anthropic",
  apiKey: process.env.SUPPORT_BOT_VIRTUAL_KEY,
});

What provider consoles already give you

Providers offer coarse versions of the same controls, and you should use them as a backstop. They stop at the boundary of one provider, and they are sized for a handful of projects, not for one key per application per environment.

OpenAI defines rate limits at the organization level and the project level, measured in requests and tokens per minute and per day. Its rate limit guide separates spend alerts, which notify you and let traffic continue, from hard spend limits, which make requests fail once reached.

Anthropic's workspaces carry a monthly spend cap and per-model limits on requests, input tokens, and output tokens per minute. Its documentation notes two limits that matter for planning: an organization gets 100 workspaces by default, and the Default Workspace cannot have a spend limit at all.

ControlOpenAI projectsAnthropic workspacesGateway virtual keys
ScopeOne providerOne providerEvery provider behind the gateway
Spend capPer project, alert or hard limitPer workspace, monthlyPer key, team, or customer, any period
Rate limitsRequests and tokens, per modelRequests, input and output tokens, per modelRequests and tokens, per key and per level above it
Error when out of budget429400 for a limit you set, 429 at the tier capWhatever the gateway chooses

Keep a provider-side spend cap set somewhat above the sum of your gateway budgets. If the gateway has a bug, the provider still stops the bleeding.

How to structure the key hierarchy

A key hierarchy groups virtual keys under the teams and customers that own them, and checks a budget at every level. One request on one key is charged to the key, its team, and its customer, and it is blocked if any of them is out of budget.

CUSTOMERTEAMSVIRTUAL KEYSPROVIDER KEYSCustomer ASupportResearchsupport-prodBUDGET · TPM · MODELSsupport-devresearch-notebooksProvider key AProvider key BA request on support-prod is checked against the key, the team, and the customer
A request on one virtual key is charged against the key, its team, and its customer. Only the gateway holds the provider keys behind it.

Issue one key per application per environment. A key shared by two services hides which one is spending, and a key shared by staging and production lets a load test spend the production budget.

Bifrost, an open-source AI gateway by Maxim AI, builds its governance on this model. A virtual key belongs to a team, to a customer, or to neither, and a team can belong to a customer.

Access is deny by default: a key can reach only the providers listed in its provider_configs, each with its own allowed models, keys, budget, and rate limit. Model restrictions still apply when a key is allowed every provider. Clients can send the key as x-bf-vk or in the header their SDK already uses, such as Authorization, x-api-key, or x-goog-api-key.

Deny by default is the detail to look for in any gateway. A key that can call any model the gateway knows about will, eventually, call the most expensive one.

Budgets in dollars, rate limits in tokens

Set budgets in currency and rate limits in tokens and requests. Budgets answer a finance question, how much may this caller spend, and only dollars compare across models. Rate limits answer a capacity question, how fast may this caller consume shared quota, and only tokens and requests map onto provider limits.

A tokens-per-minute limit matters more than a requests-per-minute limit for LLM rate limiting. One request can carry a few hundred tokens or a hundred thousand. A key limited to 60 requests a minute can still exhaust a shared provider quota with a handful of long-context calls.

Decide whether cached input counts. Anthropic's own input-tokens-per-minute limit counts only uncached tokens, so a gateway limit that counts every token will throttle a well-cached workload sooner than the provider would.

Set the per-key token limits so their sum stays below the provider account's limit, minus headroom for the callers that matter most. Otherwise one busy key pushes every other key on the same provider account into 429 errors that its owners did not cause.

Rolling windows or calendar resets

A budget resets either on a rolling window, such as the last 24 hours, or at calendar boundaries, such as the first of the month. Rolling windows smooth spend. Calendar resets match invoices and finance reporting, and they allow a burst on each side of the boundary.

Provider rate limits already behave like rolling windows. Anthropic's documentation says its API uses a token bucket that refills continuously instead of resetting at fixed intervals.

The burst is the cost of calendar alignment. A key that spends its whole monthly budget on the last day can spend a full budget again the next morning. If that would hurt, pair a monthly calendar budget with a daily rolling one.

Bifrost supports both. Budgets take a dollar limit and a reset duration from one minute to one year, and an optional calendar-aligned setting resets daily, weekly, monthly, quarterly, and yearly budgets at UTC calendar boundaries instead of on a rolling window.

Why a budget cap always overshoots

Every budget cap lets the last request run past the limit, because the gateway knows a request's cost only after the response. The check happens before the call, against the balance remaining. The charge happens after, against the tokens actually used.

Bifrost budgets behave exactly this way, and the arithmetic is simple. A provider budget of $5 has $4 spent. A $2 request arrives, passes the check because $1 remains, and leaves the budget at $6 of $5. The next request is blocked. Concurrency widens the gap: fifty requests that arrive in the same second all see the same remaining balance. Streaming widens it again, since the final usage arrives with the last chunk.

You cannot remove the overshoot without blocking requests that would have fit, so bound it instead:

  • Set max_tokens on every request. The worst-case cost of one call is then known in advance.
  • Alert at 80% of a budget, not at 100%. The alert is the control people act on; the hard cap is the backstop.
  • Limit concurrent requests per key, so a burst cannot all pass the same balance check.
  • Tell budget owners the cap is approximate by roughly one request per concurrent caller.

This is why AI budget control works best as a circuit breaker, not as an exact invoice guarantee.

What to return when a key is over its limit

A gateway should answer an exhausted budget and an exceeded rate limit with different errors, because a client must react to them differently. A rate limit clears in seconds, so the client should wait and retry. A budget clears at the next reset, which may be hours or weeks away, so the client should stop.

Providers do not make that distinction cleanly. OpenAI's guide says hard spend limits return 429, the same status as a temporary rate limit. Anthropic returns 429 with no retry-after header when an organization reaches its tier's spend cap, and 400 when it reaches a spend limit you set yourself. Three shapes for "out of money" is the ambiguity a gateway should remove. What an LLM gateway does explains how a retry loop that treats every 429 alike spins until it times out.

ConditionStatus to returnWhat the client should do
Rate limit exceeded429 with retry-afterWait the stated time, then retry
Budget exhausted402, or 429 with a distinct error type and no retry-afterStop, surface the error, alert the owner
Model not allowed for this key403Do not retry. Fix the config
Key revoked or unknown401Do not retry. Rotate the credential

Bifrost rejects requests over budget with HTTP 402 and an error type of budget_exceeded. Whichever gateway you run, check that its budget error cannot be confused with a rate limit by the retry logic in your SDKs.

How to enforce AI budgets without breaking callers

Enforce AI budgets in stages. Issue keys and record spend before you block anything, set the first budgets from observed spend with generous headroom, and tighten them once owners have seen a few weeks of their own numbers.

A rollout order that works:

  1. Issue a virtual key to each application and environment, with no budget, and route all traffic through them.
  2. After two to four weeks, set each budget at about twice the highest spend you observed, with an alert at 80%.
  3. Add team and customer budgets above the keys once owners agree on them.
  4. Lower the headroom only for keys whose spend is stable.

Most gateways implement the same core idea. LiteLLM's proxy issues virtual keys with a max_budget, a budget_duration, and tpm_limit and rpm_limit settings, and it needs a Postgres database for key management.

Bifrost covers the same ground with the deny-by-default model access, the customer and team hierarchy, and the distinct 402 described above, which is why it is the stronger default for platform teams that hand out keys to many internal callers. The same virtual keys and budgets can also govern the AI tools employees run on their own machines, through Bifrost Edge.

Once spend has an owner per key, the next questions are which model each key should reach and when. LLM routing covers that.

Frequently asked questions

What is a virtual API key?

A virtual API key is a credential issued by a gateway rather than by a model provider. The application sends it to the gateway, which looks up the key's permissions, budget, and limits, then calls the provider with a real key that only the gateway holds. Revoking a virtual key affects one caller and never requires rotating the provider key.

How is a virtual key different from a provider API key?

A provider API key authenticates you to one provider and carries that provider's account limits. A virtual key authenticates a caller to your gateway and carries limits you define: which providers and models it may use, how much it may spend, and how fast it may send tokens. One virtual key can reach several providers, and many virtual keys can share one provider key.

How do you rate limit LLM API calls?

Rate limit LLM calls on tokens per minute as well as requests per minute, because request sizes vary by orders of magnitude. Apply the limits per virtual key at the gateway, keep the sum of per-key limits below the provider account's own limit, and return 429 with a retry-after header so well-behaved clients wait instead of retrying at once.

How do you enforce AI budgets per team?

Put each team's virtual keys under a team entity with its own budget, and have the gateway check the key's budget and the team's budget on every request. A request that would pass the key's budget but not the team's is blocked. Set alerts below the hard limit, since the alert is what lets a team react before work stops.

Should LLM budgets be set in tokens or dollars?

Set budgets in dollars and rate limits in tokens. A dollar budget compares across models with different prices and matches what finance tracks. A token limit protects shared provider capacity, which is measured in tokens. A token budget alone breaks the first time a team switches to a model that costs ten times as much per token.

What happens when a virtual key runs out of budget?

The gateway blocks new requests on that key until the budget resets, and requests already in flight usually complete, which is why caps overshoot slightly. The error should be distinct from a rate limit so clients stop rather than retry. Bifrost, for example, returns HTTP 402 with the error type budget_exceeded.

Sources

  1. 01Rate limitsOpenAI API docs
  2. 02WorkspacesClaude API docs
  3. 03Rate limitsClaude API docs
  4. 04Virtual keysLiteLLM docs
  5. 05Virtual keysBifrost docs
  6. 06Budget and limitsBifrost docs