Cost Control
Cost control in LLM Proxy relies on model pricing, key and account budgets, and usage tracking.
1. Prices
Cost is calculated from the pricing fields on each model record, or from a provider mapping when overridden there. See Models & Pricing for field names, units, and tiers.
To populate prices for existing models:
- Open Models → Sync from models.dev and review the pricing diff preview (nothing is written yet).
- Confirm to write mapping-level pricing. Unchecked rows keep their manual values.
- Fill in any models not covered by the catalog manually.
cost_usd: null is not $0
A null cost means the cost is unknown, either because no rate is configured or because the request had nothing billable. Budget checks only count spend when a rate exists to compute it, so configure prices for every model you serve.
Pricing can also vary by context tiers (above an input token threshold), time windows (peak/off-peak, evaluated in the window's own timezone), and service tiers (flex or priority, selected by the request's service_tier). They apply in that order, each overriding only the rates it defines. On a provider mapping, leaving an overlay null inherits the model-level setting, while an explicit empty list or map disables it for that upstream. Reasoning tokens are billed at reasoning_cost_per_1m when set, and at the output rate otherwise.
openrouter and nanogpt report their own cost per request. Their reported figures replace the computed cost, including any upstream peak/off-peak or service-tier discounts.
2. Budgets
Budgets are checked on every billable request carrying an API key. Read endpoints (GET and HEAD under /v1/*, such as model lists, stored responses, files, and batch listings) still authenticate and apply rate limits, but are never blocked by budget so clients can still inspect available models when a key is exhausted.
| Budget | Scope | Set in | Reset |
|---|---|---|---|
| Key budget | One API key | API Keys | Owner via Reset (POST /api/api-keys/{name}/budget/reset) |
| Account budget | All of a user's keys combined | Users & Roles | Admin only (POST /api/team/members/{user_id}/budget/reset) |
- Periods: daily (00:00 UTC), weekly (ISO Monday), monthly (configurable reset day, default the 1st), or lifetime (since the last reset).
- Over-budget responses return 429
budget_exceeded(oruser_budget_exceededfor account budgets). If spend cannot be read, the check fails closed and rejects the request. - Enforcement always uses the UTC calendar window. The range selector in the usage panel is for inspection only.
Typical production setup:
| Key | Budget | Purpose |
|---|---|---|
prod-backend | Monthly | Caps service spend, aligned with your billing cycle |
dev-playground | Daily (small) | Catches runaway loops early |
one-off-batch | Lifetime | Enforces a fixed project cap |
Pair budgets with rate_limit_rpm so a traffic burst cannot exhaust a budget in seconds.
3. Visibility
| Surface | Shows |
|---|---|
| Usage dashboard | Spend, tokens, requests, success rate, cache savings, trends, per-provider and per-model breakdowns |
| Logs → Proxy Logs | Per-request cost, tokens, cache tokens, retries, and fallbacks |
GET /api/logs/usage-stats | Aggregated usage as JSON, filtered by date range |
GET /api/api-keys/spend/summary | Current spend per key |
GET /api/api-keys/{name}/usage | One key's usage over a date range |
GET /api/me/budget | Your account budget status |
Cache savings require cached_read_cost_per_1m on the model; without it, the metric is skipped and a warning is logged. The figure is net: cache-write premiums are subtracted from read savings, so a turn that writes a prefix once and reads it once reports a net cost. Set cached_write_long_cost_per_1m when an upstream charges more for long-lived cache writes (such as OpenAI's 12-hour writes), or those tokens bill at the standard cache-write rate.
4. Reduce spend structurally
- Use smart routing (
fast,auto,best) to send simpler requests to cheaper models. Log metadata recordsrouting_savingsper request, andweak/strongfeedback tunes routing over time. See Virtual Models & Routing. - Set provider selection to
cost_optimizedto order equal-priority providers by price, orbalancedto weigh price against observed latency. - Configure cache read and write rates so cache hits are billed accurately and savings are tracked.
- Use model allowlists to restrict keys to the cheaper models they need.
Example: audit yesterday's spend
curl -s "http://localhost:8080/api/logs/usage-stats?start_date=2026-09-12&end_date=2026-09-12" \
-H "Authorization: Bearer $ADMIN_JWT" | jq '{
cost: .summary.total_cost,
requests: .summary.total_requests,
success_rate: .summary.success_rate,
cache_savings: .summary.cache_savings_usd,
top_models: (.by_model | sort_by(-.cost) | .[:5])
}'Related
- Users & Roles: account-level caps
- API Keys: key-level caps and limits
- Monitoring & Alerting: track spend and health over time