Budget meter
How much of its budget an agent has used: calls, tokens and cost, for the hour, the day and the month.
Runs on sample data, with the neutral theme of shadcn/ui.
A meter for agent dashboards. It makes a limit visible before the agent reaches it. The meter takes the budget object of the SDK as it is, and shows a row for each limit that is set.
Installation
- CLI
- Manual
The command adds the file to your project, with the shadcn/ui components that it uses. Your project needs shadcn/ui.
- pnpm
- npm
- yarn
- bun
pnpm dlx shadcn@latest add https://examples.lumoauth.dev/r/budget-meter.json
npx shadcn@latest add https://examples.lumoauth.dev/r/budget-meter.json
yarn dlx shadcn@latest add https://examples.lumoauth.dev/r/budget-meter.json
bunx --bun shadcn@latest add https://examples.lumoauth.dev/r/budget-meter.json
Add the shadcn/ui components that the pattern uses.
npx shadcn@latest add cardInstall the packages.
npm install lucide-reactCopy the code into your project.
components/budget-meter.tsximport * as React from 'react';import { CircleDollarSign, OctagonX } from 'lucide-react';import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';import { cn } from '@/lib/utils';/*** The budget of an agent, as `agent.budget()` returns it. Every limit is* optional: the meter shows a row for each limit that is set.*/export interface AgentBudget {max_api_calls_per_hour?: number;max_api_calls_per_day?: number;max_tokens_per_hour?: number;max_tokens_per_day?: number;max_cost_per_day_usd?: number;max_cost_per_month_usd?: number;api_calls_this_hour?: number;api_calls_today?: number;tokens_used_this_hour?: number;tokens_used_today?: number;cost_today_usd?: number;cost_this_month_usd?: number;exhausted?: boolean;}const ROWS = [{ label: 'Calls this hour', used: 'api_calls_this_hour', max: 'max_api_calls_per_hour', kind: 'count' },{ label: 'Calls today', used: 'api_calls_today', max: 'max_api_calls_per_day', kind: 'count' },{ label: 'Tokens this hour', used: 'tokens_used_this_hour', max: 'max_tokens_per_hour', kind: 'count' },{ label: 'Tokens today', used: 'tokens_used_today', max: 'max_tokens_per_day', kind: 'count' },{ label: 'Cost today', used: 'cost_today_usd', max: 'max_cost_per_day_usd', kind: 'usd' },{ label: 'Cost this month', used: 'cost_this_month_usd', max: 'max_cost_per_month_usd', kind: 'usd' },] as const;const count = new Intl.NumberFormat('en-US', { notation: 'compact', maximumFractionDigits: 1 });const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });export interface BudgetMeterProps {budget: AgentBudget;/** The name of the agent, for the title. */agentName?: string;/** The share of a limit at which a row turns to a warning. */warnAt?: number;className?: string;}export function BudgetMeter({ budget, agentName, warnAt = 0.8, className }: BudgetMeterProps) {const rows = ROWS.filter((row) => typeof budget[row.max] === 'number' && (budget[row.max] as number) > 0).map((row) => {const max = budget[row.max] as number;const used = (budget[row.used] as number | undefined) ?? 0;return { ...row, max, used, share: Math.min(1, used / max) };});const stopped = budget.exhausted === true || rows.some((row) => row.share >= 1);const format = (kind: 'count' | 'usd', value: number) => (kind === 'usd' ? usd.format(value) : count.format(value));return (<Card className={cn('w-full max-w-sm gap-4', className)}><CardHeader><CardTitle className="flex items-center gap-2 text-base"><CircleDollarSign className="size-4" aria-hidden="true" />{agentName ? `Budget of ${agentName}` : 'Budget'}</CardTitle>{stopped && (<CardDescription className="flex items-center gap-1.5 font-medium text-red-700 dark:text-red-400"><OctagonX className="size-4" aria-hidden="true" />The agent is stopped until a period starts again.</CardDescription>)}</CardHeader><CardContent className="grid gap-4 text-sm">{rows.length === 0 ? (<p className="text-muted-foreground">No budget is set. This agent has no limit on calls, tokens or cost.</p>) : (rows.map((row) => {const full = row.share >= 1;const near = !full && row.share >= warnAt;return (<div key={row.label} className="grid gap-1.5"><div className="flex items-baseline justify-between gap-2"><span>{row.label}</span><span className={cn('text-muted-foreground tabular-nums', near && 'text-amber-700 dark:text-amber-400', full && 'font-medium text-red-700 dark:text-red-400')}>{format(row.kind, row.used)} of {format(row.kind, row.max)}</span></div><divrole="progressbar"aria-label={row.label}aria-valuemin={0}aria-valuemax={100}aria-valuenow={Math.round(row.share * 100)}className="h-2 w-full overflow-hidden rounded-full bg-primary/15"><divclassName={cn('h-full rounded-full bg-primary transition-[width]', near && 'bg-amber-500', full && 'bg-red-600')}style={{ width: `${row.share * 100}%` }}/></div></div>);}))}</CardContent></Card>);}Change the import paths if your project keeps its components in another place.
Usage
Pass what agent.budget() returns.
app/agents/[id]/budget.tsx
import { BudgetMeter, type AgentBudget } from '@/components/budget-meter';
export function Budget({ name, budget }: { name: string; budget: AgentBudget }) {
return <BudgetMeter agentName={name} budget={budget} warnAt={0.8} />;
}
Where the data comes from
| What the pattern shows | Call |
|---|---|
| Limits and usage | agent.budget() |
| Report what a model call used | agent.reportUsage({ tokens, costUsd }). The answer holds the new budget. |
| The refusal at a limit | 429 budget_exceeded, LumoAuthBudgetExceededError |
| Tell your services | The webhook agent.budget_exhausted |
States
Runs on sample data, with the neutral theme of shadcn/ui.
- No budget set.
- Near a limit. A row turns to a warning at 80 percent, or at
warnAt. - At a limit. The meter says that the agent is stopped until a period starts again.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
budgetrequired | AgentBudget | The budget of the agent, as agent.budget() returns it. | |
agentName | string | The name of the agent, for the title. | |
warnAt | number | 0.8 | The share of a limit at which a row turns to a warning. |
className | string | Class name for the card. |
AgentBudget
| Option | Type | Description |
|---|---|---|
max_api_calls_per_hour, max_api_calls_per_day | number | The limits on calls. Usage: api_calls_this_hour, api_calls_today. |
max_tokens_per_hour, max_tokens_per_day | number | The limits on tokens. Usage: tokens_used_this_hour, tokens_used_today. |
max_cost_per_day_usd, max_cost_per_month_usd | number | The limits on cost. Usage: cost_today_usd, cost_this_month_usd. |
exhausted | boolean | True when the server stopped the agent at a limit. |