fix: enforce monthly message-credit limit before chat LLM calls#157
Open
Shawnaldinho wants to merge 1 commit into
Open
fix: enforce monthly message-credit limit before chat LLM calls#157Shawnaldinho wants to merge 1 commit into
Shawnaldinho wants to merge 1 commit into
Conversation
user_profiles.message_credits_used is surfaced on /user/profile as
`creditsRemaining`, but on main today (a) no code increments it after
an LLM call, so it's always 0, and (b) no code checks it before an
LLM call, so the value is informational only. The "credits remaining"
shown in the UI is therefore a no-op gauge.
Wire the field up:
- New backend/src/lib/credits.ts with:
* monthlyCreditLimit() — reads MONTHLY_MESSAGE_CREDIT_LIMIT from the
env, defaulting to 999999 (the constant previously hard-coded in
routes/user.ts). Behaviour-neutral unless an operator opts in.
* getCreditState(userId, db) — { used, limit, remaining } for the
pre-call check; read-only, doesn't fetch the full profile.
* incrementMessageCredits(userId, db, n=1) — bumps the counter; one
call per user-initiated message, not per tool turn, so the gauge
reflects user-visible message volume.
- POST /chat and POST /projects/:projectId/chat now:
* Reject with 402 + { creditsUsed, creditsLimit } if remaining <= 0,
before flushing response headers (so the client sees a clean error
instead of a half-streamed response).
* Increment after a successful runLLMStream + assistant-message
insert. Failures don't count against the user.
- routes/user.ts now imports monthlyCreditLimit() instead of holding
its own copy of the constant, so the env-driven limit is the single
source of truth.
Tabular and workflow LLM call sites are left for a follow-up — the
two streaming chat routes are the most user-visible entry points and
adding the rest is a wider, more invasive change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
`user_profiles.message_credits_used` is exposed on `/user/profile` as `creditsRemaining`, but on `main` today (a) no code increments it after an LLM call, so it's always 0, and (b) no code checks it before an LLM call. The "credits remaining" the UI shows is therefore a no-op gauge.
Changes
Why
This closes the specific "credit counter tracked but not enforced" gap from https://insights.flank.ai/where-mikeoss-falls-short.html (gap 6). Two design choices worth flagging:
Tabular and workflow LLM call sites are not touched here — the two streaming chat routes are the most user-visible entry points and wiring the rest is a wider change worth its own PR.
Testing