Repository navigation
fix(billing): chat refuses a request over the user's or company's budget before the LLM call (#16845) - #20589
Conversation
…t is exhausted (#16845) Budget policy only acted after spend on an agent, so a budget set for a user or company never stopped a chat request. A pre-call gate now reads the LLM principal, compares the user and tenant policies with that scope's spend and refuses with a 402 / error message carrying an i18n key. Chat spend is attributed to the principal's user and tenant so there is something to compare. Gate sits in LLMService chat/chat_optimized/stream, the workflow's ollama call and both compat gateways; each refusal is audited. An unreadable store allows the request and logs, as budget evaluation does. chat_optimized is split (prompt assembly, vLLM model lookup, post-call bookkeeping) to stay under the length limit.
Gate compare before its SSE stream; skip hourly policies until an hourly counter exists; mark budget refusals with an error_code so the REST send path and both transcript streams show the message instead of a generic retry or failure; key transcript spend and gate on the canonical principal id; run resumed graph turns under the caller's principal.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 40 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. CodeRabbit Verify
|
✅ SSOT Configuration Compliance: Passing🎉 No new hardcoded values of either class — Known backlog in |
|
Code review on PR #20589 (head 13b9a15): changes needed. Read only; nothing was run. The gate reaches the workflow iteration,
Owner decisions, not in the ruling: whether a dormant org-wide $500 monthly cap should go live on existing installs (the counter starts at $0 at deploy, so no immediate stop, and users get no warning before refusal); and fail open versus closed on a store outage, since the per-key Low: |
… policy once (#16845) The default tenant policy is seeded once per tenant (marker stored with the policies) so a disabled or deleted default stays gone. The gate's store reads are time-bounded, spend is cached per scope for a few seconds, failures are logged once per interval, and the outage decision lives in one named flag. The refusal sentence no longer embeds English scope/period words: they are i18n keys of their own. Workflow gate documents why it records no spend.
|
Code review on PR #20589 (head e90b597): approve, with the store-outage behaviour awaiting the owner, currently allow, in one flag. Read only; I didn't run the tests. The check is bounded by a timeout with cached spend, and seeding is once per tenant. By the author's reading, the seeded policy names tenant |
Auto-sync: merging
|
| side | commit | subject | issue refs |
|---|---|---|---|
| main | fd42d03b5 |
Merge #20543 @ d1d1a11 into the merge train | #20543 |
| main | f8e3fb9fa |
test(i18n): record the English-only key the pattern analysis error adds (#17651) | #17651 |
| main | 42d3afc4a |
test(i18n): record the English-only key the analytics load failure adds (#17651) | #17651 |
| this branch | 13b9a15f3 |
chore(guards): the untranslated baseline counts the one new English-only budget key (#16845) | #16845 |
…et-stop # Conflicts: # repo_tests/slm_node_id_one_home_19322_test.py
… attachment verdict and the caller's budget scope (#16845)
|
Code review on PR #20589 (head 435f2b0): approve. Covers the merge of main on the resume path, read only. Not checked: the rest of the ~100 merged files beyond the resume path. |
…ew module, and the error params spread drops its empty fallback (#20589)
…watchdog test uses the shared repo root, and the reach floors follow the merged tree (mrveiss#20589)
Refs #16845
Thinking Path
Normal chat never reached the budget check, because that check needs an agent, and it pauses after the spend rather than refusing. The maintainer ruled the budget is checked before the chat LLM call, against the user's or company's budget, with a clear refusal. Per-tenant spend didn't exist, and chat records carried no user, so the first step was recording them. Reading the paths showed several set no LLM principal at all, which would have made the check a no-op there.
What Changed
services/chat_budget_gate.py:enforce_chat_budgetlooks up user and tenant policies against that scope's spend.pauseandalert_then_pausepolicies refuse with a 402 carrying an i18n key; every refusal writes one audit event.LLMService.chat,chat_optimizedandstream, the workflow's LLM iteration, both compat gateways, and the compare endpoint.record_response_usageandrecord_token_usagedefault to the principal's user and add tenant spend underllm_cost:by_tenant.chat_budget_bounds.py: timeout, a 5s spend cache so a refusal can lag by that much, and outage logging, all env-backed.chat_optimizedsplit to meet the function limit;services/llm_optimized_prompt.pyis new. Ceilings lowered:llm_service.py,manager.py.A user will notice:
Chosen, and open to reversal:
trigger_budget_evaluationdoes. The per-keycheck_budgetfails closed instead.STORE_OUTAGE_ALLOWS_REQUESTinchat_budget_bounds.pyflips it; the workflow would then show a generic error. Awaiting the maintainer.Not covered, on purpose or not yet:
sk-keys stay under their per-key budget._interpret_*calls to self-hosted Ollama have no check. The workflow iteration is gated but records no spend: it posts credential-less Ollama requests, so it can only be metered through a proxy the code can't detect.Lands after #20379 and #20401, which also change
chat_workflow/manager.py.Verification
From
autobot-backend:pytest services/chat_budget_gate_entries_test.py services/chat_budget_gate_test.py services/llm_usage_recording_stream_test.py services/llm_usage_recording_test.py services/llm_spend_call_sites_test.py api/openai_compat_test.py tests/integration/test_budget_hard_stop.py api/chat_generate_ai_response_test.py: plusservices/chat_budget_gate_bounds_test.py: 103 passed.repo_tests/i18n_untranslated_ratchet_test.py: 13 passed. Frontend specs forstreamErrorTextand locale parity pass. Removing each fix made its tests fail, apart from thetranscriber/routes/ai.pycatch and the anthropic gateway, which weren't mutated.A code review found the compare bypass, the hourly over-refusal, the swallowed REST error and the transcript handling; all are fixed here.
Not verified: no live host run, so criterion 4 stays unticked and the issue stays open.
chat_optimizedhas a source check only. The full REST send route and a refusal inside a full graph turn weren't run end to end. Other locales lack the new key, by the English-first rule.Criterion 3: the per-conversation limiter stays unused, per the maintainer decision on the issue.
Model Used
Claude Sonnet 5.5 for implementation and review, Claude Opus 5.5 for direction.
🤖 Generated with Claude Code