Skip to content

feat(quotas): estimate free-model daily quota per egress - #172

Closed
unrayed wants to merge 5 commits into
jasonxu114514:mainfrom
unrayed:feat/quota-tracker
Closed

unrayed wants to merge 5 commits into
jasonxu114514:mainfrom
unrayed:feat/quota-tracker

Conversation

@unrayed

@unrayed unrayed commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

OpenCode exposes no remaining-quota endpoint and no rate-limit headers, so this estimates the free-model daily allowance per egress from traffic the gateway already sends. It is a local estimate, never presented as upstream truth.

internal/freequota/                   # per-egress daily state: learn, persist, classify
internal/gateway/upstream.go          # record free-model attempts (egress, status, 429 body)
internal/gateway/freequota_report.go  # join configured proxies/direct/WARP + retained scopes
internal/admin/                       # GET /api/free-quotas, POST /api/free-quotas/probe
webui/                                # Free quota tab: KPIs, capacity, per-exit table

Learning:

start from an assumed limit (free_quota.assumed_daily_limit, default 200, 0 = unknown)
daily successes raise a floor
a genuine daily-quota 429 (free-limit body, reset near UTC midnight) records a ceiling
limit = max(ceiling, floor) as learned; otherwise assumed or unknown
shared_limit = the highest ceiling any egress has taught, used as the prior for exits that lack their own
confidence high/medium/low/none; 30 days retained, 14 shown

Only attempts that reach the provider count as evidence: transport errors, 403s, and session-risk 429s spend nothing and learn nothing.

Every configured exit that has a limit contributes to the estimate, including exits with no traffic yet. Capacity is the sum of limits, remaining is capacity minus used, so the KPI, the capacity bar, and every row reconcile. Limits stay labeled assumed until a real daily-quota 429 makes one learned; a ceiling learned on one exit is shown as assumed on an exit that has not exhausted itself.

A probe sends one real free-model request and reports the classification plus a refreshed report. Every configured exit is listed; an exit with no traffic counts 0 used and contributes its full limit to the estimate. Exits with no configured limit are excluded and shown separately.

Evidence

  • go build ./..., go vet ./..., go test ./... -count=1, gofmt -l ., git diff --check pass; CI green on all Go jobs and WebUI syntax.
  • Tracker tests cover assumed prior, floor, ceiling, wide bounds, multi-day aggregation, persistence reload, genuine vs session-risk 429, exhaustion clearing, shared ceilings, own-ceiling precedence over shared, and that non-billable attempts create no evidence.
  • Fresh state (no free-model traffic yet): every exit is listed at 0 / 200, capacity is 200 × exits, remaining equals capacity, and all exits read No traffic.
  • After observed traffic: capacity stays the full sum of limits and used subtracts from it. Mixed state verified at 3 learned + 17 assumed, 105 used, capacity 3,700, remaining 3,595; rows and KPIs reconcile.
  • Live probe: POST /api/free-quotas/probe {"relay":{"index":0}} → 200, classification ok, report returned.

Merge Danger

Door: two-way. Rollback is removing the package and its hooks; a missing or corrupt tracker file is never fatal.

Blast Radius: free-model quota display only.

  • Recording observes attempts the gateway already sends; nothing extra is sent unless an operator clicks Probe.
  • Diagnostic probes are pinned to one relay or the WARP lane and do not change proxy health, cooldowns, blocks, or the anonymous breaker.
  • State is stored beside the config as config.json.freequota.json; disabling free_quota stops recording.

Track free-model attempts from traffic the gateway already sends. Each
egress starts from an assumed daily limit, raises a floor from daily
successes, and learns a ceiling from genuine daily-quota 429s. Show a
local, non-authoritative estimate in Models and quotas with per-exit
remaining, history, health, and a paid probe.
A transport error or 403 spent nothing and learned nothing, so it must
not make an exit look observed. Capacity counts evidence-backed exits
only, which keeps a fresh install honest instead of reporting an assumed
200 per exit as if it were measured.
Unseen exits now publish the starting assumption instead of a blank, and
the most recently learned ceiling is used as the prior for exits the
upstream has not exhausted yet. Both stay labeled assumed or low
confidence and never count as measured capacity.
Unseen exits advertised an assumed limit in their row while the KPI and
capacity counted measured exits only, so the page showed three totals.
Capacity and remaining now cover every exit with a limit, and the panel
uses one vocabulary: learned, assumed, used, no traffic.
@unrayed

unrayed commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by the current per-egress implementation on feat/free-quota-estimate. The probe endpoint, capacity/confidence model, and shared limit described here are not part of the final design; closing to avoid confusion.

@unrayed unrayed closed this Nov 16, 2026
@unrayed
unrayed deleted the feat/quota-tracker branch November 16, 2026 22:20
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