A user opens an AI assistant in two browser tabs and submits a request from each. The workspace has enough budget for one response. Both tabs show the same remaining balance, and both requests reach the backend before either one finishes. If the server checks the balance and waits until generation ends to deduct usage, both requests can pass.
That race condition explains why a usage meter in React is only one part of the feature. The backend should reserve capacity before it calls the model, then reconcile that reservation against reported usage. React shows the outcome and tells the user what they can do next. A Flutter app signed into the same workspace can use the same API and share the same allowance.
Which AI usage limits should a React product enforce?
A single limit rarely serves every purpose. A requests-per-minute rule can slow a burst of traffic, but it treats a short rewrite and a long document analysis as equivalent. A token allowance captures more of that difference. A spend budget goes further by accounting for model prices, caching, and billable tools.
| Control | What it governs | Where it belongs |
|---|---|---|
| Request rate limit | How often a user or workspace can start a request | API or gateway |
| Per-request token budget | How much context and generated output one model call can use | Model orchestration layer |
| Workspace quota | How many credits or tokens a workspace can use in a billing period | Product backend |
| Concurrency limit | How many generations can run at once | API or job runner |
| Spend budget | How much a feature, workspace, or project can spend | Product usage ledger, with provider controls as a backstop |
Provider limits protect provider capacity and apply to the scopes they define. For example, Anthropic meters requests, input tokens, and output tokens per minute for its Messages API. A product serving multiple customer workspaces through one provider account still needs its own usage policy.
Estimate the full request before reserving a budget
The text typed into a React input is only part of the payload. A model request can also contain system instructions, conversation history, retrieved documents, tool definitions, and attachments. Estimate the assembled request after the server has resolved those inputs. Anthropic’s token-counting endpoint accepts structured inputs such as system prompts, tools, images, and PDFs. OpenAI also offers an input-token counting endpoint for Responses API requests.
Next, set a maximum output size appropriate to the task. A classification feature and a document analysis need different limits. OpenAI documents that max_output_tokens includes generated tokens beyond visible text, including reasoning tokens where applicable. Test the limit against real examples before choosing the default, especially when the product expects complete structured output.
For example, at illustrative rates of $2 per million input tokens and $6 per million output tokens, an 8,000-token input plus a 1,000-token output allowance would reserve $0.022 before any separate tool fees. With agents, apply the budget to the whole run. A cap on one model response still allows subsequent tool calls and model steps to add cost.
Reserve usage atomically, then reconcile the stream
Avoid a separate balance read followed by an unconditional write. In PostgreSQL, a conditional UPDATE can reserve capacity on a single workspace budget row. Under the default Read Committed isolation level, PostgreSQL rechecks the condition after a concurrent update changes that row.
UPDATE workspace_budgets
SET reserved_units = reserved_units + $3
WHERE workspace_id = $1
AND period_start = $2
AND spent_units + reserved_units + $3 <= limit_units
RETURNING reserved_units;This query assumes a unique budget row per workspace and period, and a positive reservation amount calculated on the server. Store the request ID and reservation in the same database transaction. If no row is returned, do not call the model. Make the request ID unique within the workspace so that retrying the same operation cannot reserve funds twice or start a second generation.
When the provider reports usage, settle the request once. In the same transaction, release its reservation, record the actual usage, and update the workspace balance. Settle against the period recorded when the request started, even if the stream finishes after a reset. If usage is missing after an abort or timeout, mark the reservation for reconciliation. A browser disconnect alone cannot establish how much work the provider completed. Vercel’s AI SDK exposes aggregate totalUsage for completed generations and makes completed steps available through its abort handler.
Give React and Flutter distinct states for distinct failures
The backend should return a stable error code, the affected scope, and a reset or retry time when one applies. Both clients can then render a consistent message and preserve the user’s draft.
| API state | What the interface should do |
|---|---|
RATE_LIMITED | Preserve the draft and show when the user can retry. |
QUOTA_EXHAUSTED | Show whether the user or workspace reached its allowance and when it resets. |
INPUT_TOO_LARGE | Offer a concrete way to continue, such as dropping an attachment or starting with less history. |
OUTPUT_LIMIT_REACHED | Keep the partial response, label it as incomplete, and offer a continuation subject to the remaining budget. |
Refresh the balance after settlement or when the user returns to the app. A React component can disable Send while its own request is pending, while the server still handles requests from other tabs and devices. Flutter can use the same quota endpoint and error codes. For the mobile-specific UX, see Flutter Geek Hub’s guide to setting AI usage limits in Flutter.
How five companies approach AI usage and cost controls
| Company | What it publicly documents |
|---|---|
| GeekyAnts | A documented clinical education project records per-call token usage, model, latency, retries, and finish reason. Its internal engineering projects use run ledgers to track costs and the configuration behind each run. This is an example of usage attribution and post-launch cost monitoring. |
| OpenAI | The API platform supports monthly organization and project spend alerts and optional hard spend limits. OpenAI notes that enforcement can lag tracked spend, allowing some additional usage before requests are blocked. |
| Anthropic | Claude API workspaces can have custom spend and rate limits. The Messages API separately meters request, input-token, and output-token throughput. |
| Vercel | AI Gateway supports budgets at team, project, API key, and user scopes. It checks applicable budgets at request start, so a request that crosses a threshold can still finish. |
| Microsoft | Azure API Management can enforce token rates or periodic quotas by a configured key, such as a tenant identifier. Microsoft documents that token counters are tracked independently by gateway and that concurrent requests can temporarily exceed a configured limit. |
Test the race before launch
Give a workspace enough budget for one request and submit two at the same time from different clients. Verify that exactly one reservation succeeds and only one generation starts. Then test duplicate submissions, stream cancellation with missing usage, and a response that finishes after the billing period resets. Those cases establish whether the displayed allowance matches the policy the backend actually enforces.





















Add Comment