Skip to content

feat(quotas): estimate free-model daily usage per egress - #183

Open
unrayed wants to merge 2 commits into
jasonxu114514:mainfrom
unrayed:feat/free-quota-estimate
Open

unrayed wants to merge 2 commits into
jasonxu114514:mainfrom
unrayed:feat/free-quota-estimate

Conversation

@unrayed

@unrayed unrayed commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The gateway serves free models through rotating egresses (proxies, direct, WARP), but gives no visibility into how much of the free daily allowance each egress has consumed. Users only find out when a request is answered with FreeUsageLimitError.

This adds a passive per-egress estimate for the current UTC day. It never probes or generates traffic to measure quota, and it never touches the upstream request path beyond observation.

Design

  • Count accepted client requests: one increment per upstream attempt whose response status is 2xx for a free model. Retries count per real attempt. Availability probes and admin UI traffic are excluded.
  • Egress identity: proxy URL, direct, or warp:<block> — resolved exactly as the request was routed.
  • Learn a per-egress allowance floor only when all of these hold:
    • the error is FreeUsageLimitError,
    • it carries Retry-After of at least 1 minute,
    • the retry time lands within 5 minutes of the next UTC midnight (midnightSkew),
    • that egress has already served at least free_quota.assumed_daily_limit accepted free requests today: a refusal below the prior can be scoped to one conversation rather than the egress, so it teaches nothing.
  • No cross-egress learning or sharing: a floor learned on one egress applies only to that egress. A later 2xx on the same egress clears exhaustion.
  • Snapshots list only today's egresses with Used > 0. Limit = max(limit, used), Remaining = 0 once exhausted, source is learned, assumed, or unknown.
  • Day rollover clears counts only when the stored event day is older than the current UTC day, so an out-of-order event cannot rewind state.

Config and state

  • free_quota.assumed_daily_limit (default 200, 0 = unknown) is the fallback for egresses that have not learned a floor. It is config-file only; admin config saves preserve it.
  • State persists beside the config as <config>.freequota.json; a missing or corrupt file only clears estimates.
  • GET /api/free-quotas returns the snapshot; the WebUI "Free models" tab renders it with used/remaining and a countdown to UTC midnight.

Evidence

Real run on a side instance built from this branch (main gateway untouched):

  • Live beam-free request returned HTTP 200; total_used moved 2 → 3 and a new egress row socks5h://127.0.0.1:10802 appeared.
  • Restarting the side instance preserved total_used=3 and all rows in oc2-side.json.freequota.json.
  • WebUI free tab showed Used 3, Remaining ≈597, three rows, countdown ticking (1h 35m 27s → 1h 35m 24s).
  • gofmt -l . clean, go vet ./... clean, go test -race ./internal/freequota/ passes, Linux and Windows go build ./... pass, node --check on changed JS, git diff --check clean.

Known limits

  • Scopes from earlier days are only pruned on rollover, so the file can keep a few stale egress rows.
  • A 2xx followed by a stalled stream still counts as one accepted request (no completion signal is used).
  • If reloading state fails exactly at rollover, the retry path keeps prior counts rather than inventing new ones.
  • Exact-midnight boundary is covered by midnightSkew logic tests, not a live clock test.
  • The countdown timer pauses while the tab is hidden and resumes on visible.
  • State file writes are not followed by a directory fsync.

@unrayed
unrayed force-pushed the feat/free-quota-estimate branch from 5779864 to dd40016 Compare November 19, 2026 20:28
The upstream exposes no remaining-quota endpoint and no rate-limit
headers, so count what the gateway already sends: accepted free-model
client requests per egress for the current UTC day. Only a free-limit
429 that follows real usage on that egress teaches a floor, and only
when its Retry-After points near the next UTC midnight. Shape refusals,
short throttles, and refusals on a fresh exit teach nothing. The floor
stays with that egress across midnight; free_quota.assumed_daily_limit
is the prior for an egress that has not taught its own, and 0 shows
unknown. Availability probes are not counted.

State persists beside the config in config.json.freequota.json,
survives runtime rebuilds, and is served by GET /api/free-quotas. The
WebUI shows it in the existing free-models tab of Model & quotas.
A midnight-Retry-After refusal can be scoped to one conversation rather
than to the egress: the same exit serves another session moments later.
Learning a limit from such a refusal recorded counts as low as 2-19 and
marked healthy egresses exhausted, so the page under-reported the pool.

Now a refusal proves egress exhaustion only once that egress has already
accepted at least free_quota.assumed_daily_limit requests. Below that it
teaches nothing and the configured prior stays in place.
@unrayed
unrayed force-pushed the feat/free-quota-estimate branch from dd40016 to e915e2b Compare November 20, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant