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.
Dshamir
added a commit
to Dshamir/AI-Legal
that referenced
this pull request
May 24, 2026
- Wire up monthly credit checking and incrementing for both chat streaming routes. Credits checked before LLM call (429 if exceeded), incremented after successful response. Auto-resets when past reset date. Limit via MONTHLY_CREDIT_LIMIT env var (PR willchen96#157). - Add GET /workflows/:id/export (.mikeworkflow.json download) and POST /workflows/import endpoints for portable workflow transfer between environments (PR willchen96#59). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Dshamir
added a commit
to Dshamir/AI-Legal
that referenced
this pull request
May 24, 2026
…tegration - CHANGELOG: add security hardening and feature entries for PRs willchen96#158, willchen96#81, willchen96#76, willchen96#79, willchen96#145, willchen96#112, willchen96#111, willchen96#110, willchen96#155, willchen96#157, willchen96#59 - ROADMAP: mark 12 new items as completed - CLAUDE.md: add sanitize.ts, streamTimeout.ts, credits.ts to lib index, update test count to 40 - README: update API endpoints table (chat pagination, workflow export), security row (HKDF, RLS, prompt defense), encryption row Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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