Add per-user daily token quota with persistent usage tracking

This commit is contained in:
2026-08-21 12:25:57 -07:00
parent 2c3dc66878
commit f1200b7d79
6 changed files with 199 additions and 45 deletions

View File

@@ -26,14 +26,15 @@ If either command fails, install Docker before you continue.
3. Set a test value for each line:
```
ANTHROPIC_API_KEY=<a personal or trial key, for testing only>
APP_USERNAME=tester
APP_PASSWORD=test-password-123
APP_USERS=tester:test-password-123,tester2:test-password-456
RATE_LIMIT_MAX=5
RATE_LIMIT_WINDOW_MS=60000
TOKEN_LIMIT_PER_USER=2000
TOKEN_USAGE_FILE=./data/token-usage.json
```
4. Save the file.
Use a personal or trial API key here, not the company-billed key. Keep the company key for the real deployment. Set `APP_USERNAME` and `APP_PASSWORD` so you can confirm the login gate works. `RATE_LIMIT_MAX=5` with a 60 second window makes the rate limit easy to trigger on purpose, for testing.
Use a personal or trial API key here, not the company-billed key. Keep the company key for the real deployment. `APP_USERS` sets up two logins so you can confirm each person gets a separate quota. `RATE_LIMIT_MAX=5` with a 60 second window makes the rate limit easy to trigger on purpose. `TOKEN_LIMIT_PER_USER=2000` is deliberately low, so you can hit the daily quota in a couple of clicks instead of a couple hundred.
`.env` is not tracked by Git. Docker Compose reads it automatically because `docker-compose.yml` lists it under `env_file`.
@@ -59,14 +60,14 @@ Use a personal or trial API key here, not the company-billed key. Keep the compa
```
docker compose logs
```
4. Confirm the log reports whether it found `ANTHROPIC_API_KEY` and whether a login is required.
4. Confirm the log reports whether it found `ANTHROPIC_API_KEY`, how many named users are configured, and the per-user token quota.
## Step 5: Test in a browser
1. Open a browser.
2. Go to `http://localhost:5173`.
3. Confirm the browser asks for a username and password.
4. Enter the `APP_USERNAME` and `APP_PASSWORD` values from your `.env` file.
4. Enter one of the `APP_USERS` pairs from your `.env` file, for example `tester` / `test-password-123`.
5. Confirm the toolkit's landing page loads, with links to both tools.
6. Open each tool link and confirm it loads.
@@ -91,13 +92,26 @@ Use a personal or trial API key here, not the company-billed key. Keep the compa
2. Confirm the next click returns a rate-limit message instead of a normal draft or a silent failure.
3. Wait for the time window to pass, then confirm the button works again.
## Step 9: Stop the container
## Step 9: Test the per-user token quota
1. Log in as `tester` and click the AI draft button once or twice, until the response reports a quota error instead of a draft. With `TOKEN_LIMIT_PER_USER=2000`, this takes one or two clicks.
2. Confirm the error names `tester`, the tokens used, the limit, and a countdown to the reset.
3. Open a new private or incognito browser window and log in as `tester2` instead.
4. Confirm `tester2` can still click the AI draft button. Each named user has a separate quota.
5. Run this command to view the usage file directly:
```
cat data/token-usage.json
```
6. Confirm it lists a separate entry for each user who made a call, with today's date and a token count.
7. Run `docker compose restart`, then confirm `tester` is still blocked. The quota survives a restart because `data` is a mounted volume.
## Step 10: Stop the container
1. Run this command:
```
docker compose down
```
2. This stops and removes the container. It does not delete your `.env` file or the project folder.
2. This stops and removes the container. It does not delete your `.env` file, your `data` folder, or the project folder.
## Rebuild after a code change
@@ -114,7 +128,10 @@ Use a personal or trial API key here, not the company-billed key. Keep the compa
Stop whatever else is using that port, or change the port mapping in `docker-compose.yml` from `"5173:5173"` to, for example, `"5180:5173"`. Then open `http://localhost:5180` instead.
**The browser does not ask for a login.**
Check that both `APP_USERNAME` and `APP_PASSWORD` are set in `.env`, with no typos in the variable names. Restart the container after any `.env` change: `docker compose up -d --build`.
Check that `APP_USERS` is set in `.env` (or the legacy `APP_USERNAME`/`APP_PASSWORD` pair), with no typos in the variable names. Restart the container after any `.env` change: `docker compose up -d --build`.
**Everyone seems to share one quota, or a user's quota did not reset the next day.**
Check that each person has their own entry in `APP_USERS`, not one shared `APP_USERNAME`/`APP_PASSWORD`. Quota resets happen on UTC calendar days, which may be a few hours off from your local midnight.
**The AI draft button reports a missing key.**
Check that `ANTHROPIC_API_KEY` is set in `.env` and is a real key. Restart the container after the change.