Fluxio
← Volver al blog

How to Set Per-User Budgets in a Chat Product

Cuánto cuestan los tokensEN

Setting per-user budgets in a chat product starts with a server-side spending ledger, not a message counter. Build the request flow around three actions: reserve an allowance before generation, stream the response, and settle the actual charge afterward. This walkthrough uses an illustrative $5 monthly allowance, not a provider price or recommended subscription tier.

§1 Step 1: Define Each User’s Spending Allowance

Choose a currency, reset schedule, and enforcement policy. For this example, give each user $5 per calendar month, reset at midnight UTC on the first day, and reject new requests when their available allowance cannot cover another reservation.

Keep the product allowance separate from your upstream account balance. Define whether the allowance measures your API costs or customer-facing credits. If you use credits, document the conversion explicitly rather than presenting them as dollars.

§2 Step 2: Create the Budget Ledger

Store one budget record per user and period. Use integer microdollars or a fixed-precision decimal type for monetary amounts. Create request records alongside the aggregate balance so every reservation and settlement has an audit trail.

  • – Record the authenticated user identifier, period identifier, spending limit, settled spend, and reserved spend.
  • – Store a unique application request identifier, upstream request identifier, selected model, and pricing version.
  • – Track request states such as reserved, running, settled, and awaiting reconciliation.

Calculate available allowance = limit minus settled spend minus reserved spend. Derive the user identifier from the authenticated session rather than accepting a browser-supplied billing identity.

§3 Step 3: Calculate a Conservative Request Reservation

Before calling the model, assemble the complete prompt and estimate its input tokens. Set an enforceable output-token maximum. Calculate the reservation using the applicable input and output rates, plus any other billable categories for that route.

Store rates centrally and snapshot the pricing version on each request. For a strict cap, use a defensible upper bound rather than an average response cost. If tokenization or billable usage cannot be bounded reliably, describe the limit as a soft cap and keep a separate platform safety margin.

§4 Step 4: Reserve Funds Atomically Before Streaming

Run admission inside a short database transaction. Lock the user’s budget row, check available allowance, create the reservation, and commit before opening the upstream connection. PostgreSQL row locks can block conflicting updates until the transaction ends. ([postgresql.org](https://www.postgresql.org/docs/current/explicit-locking.html))

Require a unique idempotency key so repeated submissions return the existing request state instead of creating another charge. Keep the database transaction closed during generation. Apply the same admission check to retries, fallback calls, and additional model turns.

§5 Step 5: Connect Fluxio Behind Your Budget Gate

Fluxio fits this architecture as a streaming-first, pay-per-token AI API relay. Use its shared access layer for model calls while keeping per-user authorization and accounting in your own backend. Keep the API key server-side and open the stream only after the reservation succeeds.

Choose an allowed model, such as Gemini 2.5 Flash, after checking its current route pricing and output-limit behavior. Treat SDK compatibility as a starting point: verify token limits, usage reporting, and cancellation behavior for the specific route before relying on a hard spending guarantee.

§6 Step 6: Settle Usage and Reconcile Interrupted Streams

For OpenAI Chat Completions, requesting stream_options.include_usage adds a final usage chunk. Verify equivalent support on your chosen relay route rather than assuming it. ([platform.openai.com](https://platform.openai.com/docs/api-reference/chat/create))

When authoritative usage arrives, calculate the charge from the recorded rates, release the reservation, and add settled spend in one idempotent transaction. If the charge exceeds the reservation, record the full amount and flag the estimation failure.

Do not treat a disconnected browser as proof of zero usage. OpenAI documents that interrupted streams may never deliver their final usage chunk. Keep uncertain requests pending and reconcile through available provider records; do not automatically release their reservations on timeout. ([platform.openai.com](https://platform.openai.com/docs/api-reference/chat/create))

§7 Step 7: Test Budget Boundaries and Explain Limits

Test simultaneous requests, duplicate submissions, cancellations, missing usage, exhausted balances, and month-end transitions. Attribute delayed settlements to the original budget period. Display remaining allowance and reset time, and explain budget rejections before starting generation.

Sign up for Fluxio.

Crear una cuenta de Fluxio →

Más artículos de Corrientes