Concepts
Budget states
Periods:
daily, weekly (ISO, Monday), monthly, quarterly (calendar quarters) and lifetime (never resets). All windows are UTC. Currency is USD.
How enforcement works
Every request from an end user with an enforced budget reserves its worst-case cost before the provider is called — the prompt tokens plus the request’smax_tokens (or the configured default output estimate) at the model’s price. The reservation is atomic, so concurrent requests from one user see each other’s reservations and cannot collectively overspend — across replicas when the gateway runs with Redis; without Redis each process keeps its own counters. When the response completes (streaming or not), the reservation is reconciled to the real cost. A failed request — a provider error, a timeout, a refusal — costs nothing and its reservation is fully refunded. Retries and failover inside one request are settled once. The reservation fails open: if the counter backend errors or cannot be seeded for a reason other than loading, the request is admitted rather than refused.
Organization, workspace, key and end-user budgets all apply: a request must fit every enabled budget that covers it.
Send requests as an end user
API
Base path/v1/end-users, authenticated with your ManyLayers API key exactly like an inference call. {id} is either the ManyLayers id or your own user id (customer_001).
Any Gateway key in the workspace can read. The key must be bound to a workspace, otherwise the call answers
403 forbidden (“this credential is not bound to a workspace”). Writing budgets and registering users needs an admin-role key (or a workspace administrator in the console). An id from another organization answers 404.
Usage endpoints accept from and to (RFC 3339 or YYYY-MM-DD; to exclusive), model, provider, status (success or failed) and application_id.
Register a user with a budget
201 with the user and their budget; 200 if the user already existed (registration is idempotent on external_id; a budget in the body is applied to them).
The budget body (here and on PUT /v1/end-users/{id}/budget) takes limit (required, above 0 and at most 1,000,000), currency (USD only), period (default monthly), enforced (default true), enabled, alert_thresholds (percentages 1–100) and alert_emails.
Change the budget
Read usage
from/to the window is the budget’s current period, so budget.used and usage.total_cost are the same figure. budget is null when no budget applies.
Request history
id, request_id, timestamp, external_id, application_id, model, provider, endpoint, http_status, status, input_tokens, output_tokens, total_tokens, cost_usd, latency_ms and streamed — never prompts, completions or credentials. When has_more is true, pass next_cursor as after for the next page.
Budget exceeded
A refused request returns 402 before any provider is called. Theerror object is the standard OpenAI envelope (SDKs raise it as an error); budget sits beside it:
error.reason is budget_exhausted when the allowance is spent, or insufficient_remaining_budget when some remains but not enough for this request’s reservation — lower max_tokens, or raise the budget. period_end and next_reset_at are null for a lifetime budget. A budget whose counter is still being loaded answers 503 budget_initializing with Retry-After: 1; retry.
Sample integration
A minimal Node.js backend that proxies chat for a signed-in user and exposes their budget to your frontend:server.js
AiBudget.tsx
Limitations
- Custom date-range budget periods are not supported; use
daily,weekly,monthly,quarterlyorlifetime. - The identity, and so the per-end-user hard cap, applies only to chat completions, completions, responses, messages and embeddings. Image, audio, realtime, moderation, rerank, file, MCP and knowledge-base requests, and
/v1/asyncjobs, are attributed to the key, not to the end user: they are not metered against the user at all, never count toward a per-user budget, and do not appear in the usage API. - Usage rows are written asynchronously; a request typically appears in the usage API within a few seconds of completing, with no guaranteed bound. Enforcement does not wait for it.