Budget Intelligence: Forecaster, Agent Budgets, and Alerts
2026-03-11
What We Built
Wired the existing BudgetForecaster and AgentBudgetManager classes into live API routes. The /v1/budget/forecast endpoint now returns trend analysis, projected depletion dates, anomaly detection, and confidence scoring — powered by 30-day historical spend data from Redis instead of the previous inline arithmetic.
Added per-agent budget management endpoints (/v1/budget/agents) allowing operators to set conversation, hourly, and daily spend limits per agent with configurable exhaustion actions (downgrade model or stop). Added budget alert configuration and history endpoints for threshold-based webhook notifications.
Why It Matters
Budget intelligence was built but unwired — the forecaster existed as a standalone class but the API returned simple spend/limit math. Now operators get proactive warnings: "at current rate you'll hit your limit in 12 days" with anomaly detection flagging unexpected spend spikes. Per-agent budgets prevent any single agent from consuming disproportionate resources. Alert webhooks enable integration with PagerDuty, Slack, or custom monitoring.
How It Works
- Forecaster wiring:
GET /v1/budget/forecastinitializes a per-tenantBudgetForecaster, seeds it with 30 days of daily spend from Redis counters, then callsforecast()anddetectAnomalies(). Response includesavgDailySpendUsd,depletionDate,daysRemaining,trend(increasing/decreasing/stable),projectedPeriodSpendUsd,confidence, and anomaly array with Z-score severity. - Agent budgets:
AgentBudgetManagerpersisted to ConfigStore underagent-budget:{agentId}keys. SupportsperConversationUsd,perHourUsd,perDayUsdlimits withdowngradeorstopexhaustion actions. Includes leaderboard ranking agents by total spend. - Alerts: ConfigStore-backed alert configuration with customizable thresholds (default 80/90/95%), webhook URL, and enable/disable toggle. History endpoint with NaN-safe pagination.
- Dashboard: Budget Gauge section with radial SVG gauges (green/amber/red based on threshold proximity, CSS pulse animation at warning levels), depletion countdown, forecast stats in
glassStatCard, anomaly cards, and agent budget leaderboard table.
The Numbers
- 5 new API endpoints (+ 6 JWT-authenticated dashboard bridges including write endpoints)
- 7 new SDK methods across TypeScript and Python (sync + async)
- 14 route-level tests (up from 6)
- 30-day rolling forecast window with confidence scoring
- Per-agent budget granularity: conversation, hourly, daily
Competitive Edge
No AI gateway competitor offers integrated budget intelligence with anomaly detection. Portkey and OpenRouter provide spend tracking but not forecasting — they tell you what you spent, not what you're about to spend. BrainstormRouter's BudgetForecaster with linear regression trend analysis and Z-score anomaly detection turns budget management from reactive monitoring into proactive cost control.
Lockstep Checklist
- [x] API Routes:
src/api/routes/budget.tsrewritten,src/api/routes/auth.tsupdated with 4 new bridges. - [x] TS SDK:
packages/sdk-ts/src/resources/budget.tsupdated with 6 new methods and types. - [x] Python SDK:
packages/sdk-py/src/brainstormrouter/resources/budget.pyupdated with sync + async methods. - [x] MCP Schemas:
br_get_budget_forecastdescription updated intool-manifest.tsandagents.json. - [x] Dashboard:
site/dashboard/src/pages/insights.ts— Budget Gauge, forecast panel, agent budgets, anomaly cards. - [x] Tests:
src/api/routes/budget.test.ts— 14 tests covering forecast shape, agent CRUD, alert config, pagination.