Capping OpenAI Spending Per User in ASP.NET Core via Budget Reservations
The author describes an ASP.NET Core sample that enforces per-user OpenAI spending limits before requests are sent, rather than only logging usage afterward. The server identifies the user from a bearer token and ignores any user ID supplied in the request body, so callers cannot select another user's budget. Before contacting the provider, the application reserves the worst-case estimated cost, including every permitted retry attempt, based on prompt size, output limits, and configured prices. A lock-protected in-memory ledger prevents concurrent requests from reserving the same remaining balance, and exhausted budgets return HTTP 429. The approach has limits: it can reject requests that would have been cheap, the single-process lock does not coordinate multiple instances, and in-memory records are lost on restart.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in