Per-project LLM budgets: how to cap AI API spend per product, team and person
A provider account has one bill. Your company has several products, a few teams and a dozen people with API keys. When the invoice jumps, you have to work out which of them caused it, and by then the money is spent.
Per-project LLM budgets close that gap. Every product gets its own monthly limit, and it's enforced on each request before the request reaches OpenAI, Anthropic or Google. This guide covers how to set up budgets for products, departments and individual people, what your code sees when a limit is reached, and how to handle it, with examples for the OpenAI SDK.
The examples use ONEWAY, an OpenAI-compatible LLM gateway. The patterns carry over to any gateway that enforces limits per key.
Why provider-level limits aren't enough
Provider consoles set spending limits on the account or organisation. That is useful as a last line of defence, but it has three gaps:
- One limit covers everything. A runaway batch job in one product can use up the limit your customer-facing chatbot depends on.
- No per-product view. The provider sees API keys, not “the support bot” or “the onboarding flow”.
- One limit per provider. If a product calls Claude and GPT, you're managing two limits in two consoles, and neither knows about the other.
A gateway sits in front of every provider, so it can enforce one limit per product, whichever model the product calls.
The model: a project per product
In ONEWAY, an API key belongs to a project, and a project is the product making the call. Spend, limits and request history all follow the project. That gives you three layers of limits, and the tightest one wins:
| Layer | Set as | When it's reached |
|---|---|---|
| Project | A monthly budget per project | Soft: a warning header. Hard cap: 402 budget_exceeded |
| Department | A monthly allocation per team | 402 department_budget_exceeded |
| Person | A personal monthly limit on personal keys | 402 personal_limit_exceeded |
Limits are checked before the request reaches the provider. A request that would break a hard limit is never sent upstream, so it can't add to the bill. The full reference is in the docs under budgets and limits.
Step 1: point your code at the gateway
If you use the OpenAI SDK, changing the base URL and the key is enough. Request bodies stay the same.
import OpenAI from "openai";
const ai = new OpenAI({
baseURL: "https://oneway-gateway.oneway-gateway.workers.dev/v1",
apiKey: process.env.ONEWAY_API_KEY, // a project-scoped key
});
const reply = await ai.chat.completions.create({
model: "anthropic/claude-sonnet-5-5",
messages: [{ role: "user", content: "Summarise this inbox" }],
});Model IDs are provider/model, such as openai/gpt-4.1 or google/gemini-3.1-pro, so the provider is always visible in your code. Today ONEWAY runs on your own provider keys: you add an OpenAI, Anthropic, Google Gemini or AWS Bedrock key and that provider bills you directly. The switching guide covers Python, streaming and tools.
One project per product, and test keys separate from live
Create a project for each product, then create keys inside it. Use test keys for development and live keys for production. Usage is reported separately, and you can revoke a leaked development key without touching production. Revoking a key takes effect at the gateway at once.
Step 2: choose a soft budget or a hard cap
Soft budget: keep serving, raise a flag
Without a hard cap, requests keep succeeding after the budget is passed, and every response carries x-oneway-budget-warning: over-monthly-budget. Use a soft budget for customer-facing features, where an outage costs more than the overspend, and alert on the header:
const { data, response } = await ai.chat.completions
.create({ model: "anthropic/claude-sonnet-5-5", messages })
.withResponse();
if (response.headers.get("x-oneway-budget-warning")) {
alertOncall("Support bot is over its monthly LLM budget");
}Hard cap: stop the spend
With a hard cap, requests stop once the budget is reached:
HTTP/1.1 402
{ "error": { "type": "insufficient_quota", "code": "budget_exceeded",
"message": "This project has reached its monthly budget." } }Use a hard cap for internal tools, batch jobs, experiments and anything where an unexpected loop is a bigger risk than a pause.
Step 3: add department and per-person limits
Project budgets answer “which product spends what”. Companies usually also need “which team” and “which person”.
- Departments get a monthly allocation. When it's used up, requests from that department fail with
402 department_budget_exceeded. - People get a personal monthly limit on their personal keys. When it's reached, requests fail with
402 personal_limit_exceeded.
This suits teams where engineers use personal keys in coding assistants and scripts: each person has a ceiling, the department has a ceiling, and the tightest one applies. The workspace demo shows departments and a usage leaderboard with sample data.
Step 4: attribute spend inside a project
A budget tells you that a product overspent. Attribution headers tell you why. Add them once as default headers, or per request when they change:
const ai = new OpenAI({
baseURL: "https://oneway-gateway.oneway-gateway.workers.dev/v1",
apiKey: process.env.ONEWAY_API_KEY,
defaultHeaders: {
"x-oneway-feature": "support-bot",
"x-oneway-release": process.env.GIT_SHA,
},
});
await ai.chat.completions.create(
{ model: "anthropic/claude-sonnet-5-5", messages },
{ headers: { "x-oneway-customer": customer.id } },
);x-oneway-customer: your end customer's IDx-oneway-feature: the feature that made the callx-oneway-agent: the agent or job that made the callx-oneway-release: your git commit or tag, for cost per deploy
Values can be 1–128 letters, digits and . _ : @ / -. Usage then shows spend by customer, feature, agent and release, so “spend doubled after Tuesday's deploy” turns into a specific release you can look at. See attribution headers.
Step 5: handle limit errors in code
Errors use OpenAI's shape (Anthropic's on /v1/messages), with ONEWAY's reason in error.code. Branch on the code: it's stable, while messages may be reworded.
try {
return await ai.chat.completions.create({ model, messages });
} catch (err) {
switch (err.code) {
case "budget_exceeded":
case "department_budget_exceeded":
case "personal_limit_exceeded":
// Don't retry: the limit holds until it's raised or the month resets.
return fallbackResponse("AI features are paused for this workspace.");
default:
throw err; // 429s: back off and retry
}
}Don't retry a 402: a 4xx other than 429 won't succeed on a retry. A 429 means slow down. The gateway allows 600 a minute per API key and 1,200 a minute per client IP.
If you send x-oneway-fallback with up to three backup models, or x-oneway-retries from 0 to 3, only the call that answered counts towards spend, at that model's price. Budgets apply the same way on /v1/chat/completions, /v1/responses and /v1/messages.
A sensible default setup
- Customer-facing product: soft budget plus an alert. An outage costs more than the overspend.
- Internal tools and scripts: hard cap. A loop should stop, not run all weekend.
- Experiments and prototypes: a low hard cap. Cheap to raise, expensive to forget.
- Engineers' personal keys: a personal limit, visible to their lead.
Start strict and loosen limits as you learn real usage. Raising a budget is one change in the dashboard. Getting money back after a bad month isn't possible.
Summary
Provider limits protect the account. Per-project budgets protect each product, each team and each person, and attribution headers make every overspend traceable to a feature, a customer or a release. The setup is four changes: a base URL, a project-scoped key, a budget on the project, and a few headers. ONEWAY is free during the beta, with no inference markup; see pricing.
FAQ
What is a per-project LLM budget?
A per-project LLM budget is a monthly spending limit attached to one product's API keys, not to your whole provider account. A gateway checks it on every request, before the request reaches the model provider. It can warn you with a response header, or stop requests with a 402 error once the limit is reached.
What happens when an LLM budget is exceeded?
With a soft budget, requests keep working and each response carries x-oneway-budget-warning: over-monthly-budget, so you can alert on it. With a hard cap, the gateway returns HTTP 402 with the code budget_exceeded and doesn't send the request to the provider, so no further spend is incurred.
Can I limit LLM spend per developer or per team?
Yes. In ONEWAY, departments get a monthly allocation and people get a personal monthly limit on their personal keys. Reaching them returns 402 department_budget_exceeded or 402 personal_limit_exceeded. Project budgets, department allocations and personal limits stack, and the tightest one wins.
Do budgets work with Claude and Gemini, or only OpenAI?
Budgets apply to every model behind the gateway, including Claude, GPT, Gemini and Bedrock models, on all three endpoints: /v1/chat/completions, /v1/responses and /v1/messages. Your own custom OpenAI-compatible providers have no price in ONEWAY, so a key under a hard limit can't call them.