An editor's budget status
User reads one backend user's own usage today and
this month against the limits of their budget record, the numbers the AI
Tasks module shows (ADR-232). It reads the same windows and
the same record that Budget compares: today
from 00:00, this month from the first at 00:00, both up to now.
use Netresearch\NrLlm\Service\Budget\UserBudgetStatusService;
use Netresearch\NrLlm\Service\Formatting\LocalizedNumberFormatter;
final readonly class MyBudgetStatusReader
{
public function __construct(
private UserBudgetStatusService $budgetStatus,
) {}
/**
* @param int $beUserUid the logged-in backend user, never a request value
*/
public function read(int $beUserUid): array
{
return $this->budgetStatus
->forUser($beUserUid, LocalizedNumberFormatter::forBackendUser())
->toArray();
}
}
Obtain the service from the DI container; its constructor is not part of the
promise. Pass the uid of the logged-in backend user. A uid of 0 or below
reads no usage, because be_ holds the usage of every caller
without a backend user (command line, scheduler).
UserBudget Status $hasis true only when enforcement applies the record: it exists, is active and sets at least one limit above 0. Otherwise every limit reads 0 and the usage is still filled in.Personal Limit $todayand$monthareBudgetobjects.Window Status BudgetWindow Status $requests,$tokensand$cost, oneBudgeteach.Meter BudgetMeter $usedand$limitas numbers; a limit of 0 means no limit on this axis (isis false).Limited () getis the share of the limit used, 0 to 100, rounded down.Percent () getandUsed Formatted () getformat with the formatter passed toLimit Formatted () for: cost as USD with two decimals, counts without.User ()
to on each of them returns the formatted values, for a JSON
answer: has, then today and month, each with
requests, tokens and cost as
{used: string, limit: string, limited: bool, percent: int}.
Localized formats numbers and USD amounts in a locale
through ICU. for takes the locale of the current backend
user and falls back to en- without one; `new
Localized` fixes one. Amounts stay in USD, the
currency budgets and prices are stored in.