The LLC budget system now supports two budget modes to accommodate different user scenarios:
- DOLLARS mode (default): Track spend in USD, enforce dollar limits
- TOKENS mode: Track spend in token counts, enforce token limits
This allows users on subscription plans (Claude Max, ChatGPT Plus) or free-tier models to set monthly token allowances instead of dollar budgets.
- Both modes track both metrics:
- Dollar spend (always calculated for cost analytics)
- Token consumption (for usage analytics)
- Only the active mode is enforced for hard-stop budget limits
- DOLLARS mode: Hard stop at
budget_limit(USD) - TOKENS mode: Hard stop at
token_limit(token count) - Alert thresholds work in both modes (default: 80%)
{
"agent_id": "agent-123",
"budget_mode": "tokens",
// Dollar fields (always present)
"budget_limit": "100.00",
"budget_spent": "42.50",
// Token fields (GH#8997)
"token_limit": 1000000,
"tokens_spent": 450000,
"alert_threshold": 0.8,
"remaining": "550000", // In active mode (tokens here)
"is_over_limit": false,
"alert_triggered": false
}PATCH /api/llc/budget/{agent_id}/limit
{
"budget_mode": "tokens",
"token_limit": 1000000,
"alert_threshold": 0.8
}Switch back to dollars mode:
{
"budget_mode": "dollars",
"budget_limit": "100.00"
}budget_mode(String): 'dollars' or 'tokens' (default: 'dollars')token_limit(BigInteger, nullable): Monthly token allowance for TOKENS modetokens_spent(BigInteger): Token consumption counter (tracked in both modes)
Migration 20260604_036_budget_token_mode.py adds these columns with safe defaults:
- Existing rows default to
budget_mode='dollars' tokens_spentinitialized to 0token_limitis NULL for DOLLARS mode agents
# User pays fixed monthly fee, wants to track token usage
await budget_service.update_limit(
agent_id="agent-123",
budget_mode="tokens",
token_limit=5_000_000, # 5M tokens/month
alert_threshold=0.9
)# Free tier allows X tokens/month before switching to paid
await budget_service.update_limit(
agent_id="agent-456",
budget_mode="tokens",
token_limit=100_000, # 100k tokens/month
alert_threshold=0.8
)# Traditional dollar-based budgets for API usage
await budget_service.update_limit(
agent_id="agent-789",
budget_mode="dollars",
budget_limit=Decimal("50.00"),
alert_threshold=0.8
)ingest_cost_event():
- Calculates dollar cost from MODEL_PRICING_PER_1M_TOKENS
- Atomically updates BOTH
budget_spentANDtokens_spent - Enforces limits based on
budget_mode
check_budget():
- Returns remaining budget in the active mode
- TOKENS mode: remaining tokens
- DOLLARS mode: remaining dollars
AgentBudgetState now includes:
budget_modetokens_spenttoken_limit
Computed properties (remaining, is_over_limit, alert_triggered) are mode-aware.
- Show token consumption alongside dollar spend
- Display mode selector: "Track by Dollars" / "Track by Tokens"
- Mode-appropriate charts ($ vs token counts)
- Budget mode toggle in agent configuration
- Conditional input: dollar amount OR token count
- Clear labeling: "Monthly Token Allowance" vs "Monthly Dollar Limit"
-
Create a token-mode budget:
curl -X PATCH http://localhost:8001/api/llc/budget/test-agent/limit \ -H "Content-Type: application/json" \ -d '{ "budget_mode": "tokens", "token_limit": 100000, "alert_threshold": 0.8 }'
-
Ingest some token usage:
curl -X POST http://localhost:8001/api/llc/budget/test-agent/ingest \ -H "Content-Type: application/json" \ -d '{ "tokens_in": 1000, "tokens_out": 500, "model": "claude-sonnet-4-6" }'
-
Check budget status:
curl http://localhost:8001/api/llc/budget/test-agent
Should show
tokens_spent: 1500,remaining: 98500 -
Test hard stop: Ingest tokens until
tokens_spent > token_limit→ Should get 402 BudgetExhausted error
- Test mode switching (dollars → tokens → dollars)
- Test enforcement in both modes
- Test alert thresholds in both modes
- Test cache invalidation on mode change
- GH#8215: Original per-agent budget implementation (dollar-based)
- Paperclip #1756: 8👍 Token-based budget mode request
- Paperclip #339: 13👍 Token tracking for subscription users
- GH#6630: AgentBudgetTracker SharedRuntimeBag integration
- GH#8551: CostDashboard integration
Existing deployments:
- Run migration
20260604_036— safe, adds columns with defaults - All existing agents remain in DOLLARS mode (no behavior change)
- Operators can switch agents to TOKENS mode via API as needed
- Frontend updates can be deployed independently (feature flag)