Files · xAI Grok Cost Control for SMB Customer Support Agents
77 (1 binary, 577.8 kB total)attempt 1
README.md·2059 B·markdown
markdown
# xAI Grok Cost Control for SMB Customer Support Agents
Prevent runaway AI spending by enforcing per-tenant daily budgets and fallback routing for xAI Grok-powered support agents.
## How it works
A layered budget guard protects each tenant: a **soft cap** (80% of daily limit) triggers a warning, and a **hard cap** (100%) triggers automatic fallback from xAI Grok to a cheaper OpenAI model. Budgets reset daily, and all spend is recorded via Helicone telemetry.
## Architecture
Express backend + Next.js dashboard, using REAA packages:
- `@reaatech/agent-budget-engine` — per-tenant budget enforcement
- `@reaatech/llm-router-core` — primary model routing
- `@reaatech/llm-router-fallback` — fallback routing when budget is exhausted
- `@reaatech/llm-cost-telemetry` — cost tracking, config loaders, utilities
- `helicone` — telemetry export (deprecated but in use)
## Prerequisites
- Node.js >= 22, pnpm 10
- xAI API key
- OpenAI API key (for fallback model)
- Helicone API key
## Quick start
```bash
pnpm install
pnpm dev # Terminal 1: Next.js dev server (port 3000)
pnpm server # Terminal 2: Express API server (port 3001)
```
Open http://localhost:3000 and navigate to /dashboard.
## Environment variables
| Variable | Default | Description |
|----------|---------|-------------|
| `XAI_API_KEY` | — | xAI Grok API key |
| `OPENAI_API_KEY` | — | OpenAI API key (fallback) |
| `HELICONE_API_KEY` | — | Helicone telemetry key |
| `HELICONE_BASE_URL` | `https://api.hconeai.com` | Helicone endpoint |
| `DEFAULT_DAILY_BUDGET` | `5.00` | Default daily budget per tenant |
| `TENANT_BUDGETS` | `{}` | Per-tenant overrides as JSON |
| `PORT` | `3001` | Express server port |
| `NEXT_PUBLIC_EXPRESS_URL` | `http://localhost:3001` | Express URL for Next.js |
## API reference
- **POST /api/chat** — Send a chat message (routed through budget check + fallback)
- **GET /api/spend** — Current spend summary (served by Express at NEXT_PUBLIC_EXPRESS_URL)
## Testing
```bash
pnpm test
```
## License
MIT