Monthly credit metering and workflow export/import

Dshamir adds per-user monthly credit enforcement on both chat streaming routes and portable workflow files - two product features that signal the fork is targeting metered multi-tenant operation.

workflowmulti-tenant

Credit enforcement is a new credits.ts module. Before each LLM call, checkCredits(userId) reads messageCreditsUsed and creditsResetDate from the user profile. If the reset date has passed, it resets the counter and advances the date by 30 days. If the user is at or above MONTHLY_CREDIT_LIMIT (env var, defaults to 999999 if unset), the route returns 429 with a message. After a successful stream completes, incrementCredits(userId) bumps the counter via updateMany. Both chat routes (/chat and /projects/:id/chat) got the check wired in.

Workflow export and import are two new endpoints. GET /workflows/:id/export serializes a workflow record to JSON and sends it as a .mikeworkflow.json download. POST /workflows/import reads the uploaded file and creates a new workflow for the current user. A one-line follow-up commit fixed the audit action type from "import" (not a valid enum value) to "create".

These are product-tier decisions, not infrastructure primitives. The credit module assumes UserProfile.messageCreditsUsed exists in the schema - so it only applies if you adopt the Prisma migration. The logic itself is straightforward and easy to adapt to a different storage model if needed.

So what Worth a look if you want LLM usage metering. The `credits.ts` module is about 40 lines and easy to drop in or adapt. Skip if you are not running a multi-tenant or usage-limited deployment. Workflow portability is useful for teams managing multiple self-hosted instances.

View this fork on GitHub →

Spotted something wrong? Or know the PR text has fresher detail than the writeup above?

Commits in this thread

2 commits from Dshamir/AI-Legal, oldest first. Source extracted verbatim from the harvested git log.

SHA Subject Author Date
e889c1dd feat: credit enforcement on chat, workflow export/import Dshamir 2026-05-23 ↗ GitHub
commit body
- 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 #157).
- Add GET /workflows/:id/export (.mikeworkflow.json download) and
  POST /workflows/import endpoints for portable workflow transfer
  between environments (PR #59).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1a09a245 fix: use valid audit action type for workflow import Dshamir 2026-05-23 ↗ GitHub
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Capture this thread into my fork

Download a single Markdown prompt that tells Claude how to port every commit above into your working tree — adapting paths and structure to match your repo. Run it via claude -p < capture-thread-525.md from inside the repo you want the changes in.

⬇ Download capture-thread-525.md