Skip to content

feat(route-proxy): complimentary-token metering wall for openai-payg-free (#13839) - #14025

Merged
dezren39 merged 1 commit into
mainfrom
lane/metering-wall-13839
Nov 24, 2026
Merged

dezren39 merged 1 commit into
mainfrom
lane/metering-wall-13839

Conversation

@dezren39

@dezren39 dezren39 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Implements the metering wall from #13839 (the issue body carries the full problem statement; this is its smallest version, landed).

What was missing: openai-payg-free draws on OpenAI's daily complimentary buckets (large group 1M tokens/day, small 10M/day, reset 00:00 UTC). Exhaustion is silent — no error, traffic just bills — and every wall the estate had was failure-driven, so none could ever fire for this alias. Drew's scope clarification (2026-11-23): count from response usage in the route proxy, fail closed at the bucket, no balance-polling, paid tails untouched.

What this does:

  • Counting — per group per UTC day, from the route_attempt ledger: tokens_input + tokens_output over the alias's attempts started that day. Those are the response's prompt/completion totals; reasoning tokens (a completion subset) and cached tokens (a prompt subset) are already inside and count exactly once. The counter is the ledger, so it survives proxy/server restarts by construction and also counts session traffic on the alias (same provider id, same org bucket).
  • Wall — at bucket − margin, the group's pairs are excluded in the existing seating gate (applyKnownExhaustedGate) until the next 00:00 UTC; routes fall through to the openai-payg paid tail. One deliberate difference from the failure-driven walls: the gate's walk-anyway bypass never resurrects a metered pair — if nothing else survives, the route answers no-available-members rather than billing the free alias (fail closed).
  • Config — a tokenMetering declaration on the openai-payg-free block, next to the alias: large group, 1,000,000 dailyTokens, 20,000 marginTokens, all three catalog models mapped. The margin exists because a request crossing the bucket bills in full; the wall stops new requests at the cap and does not try to predict request sizes.
  • Checker — inspectProviderMeteringRules in the route-rules guard: declaration shape, marginTokens < dailyTokens, every catalog model mapped to exactly one group, no mapped model outside the catalog (fail closed, mirroring the proxy's warn-and-never-engage for malformed declarations).

Tests (each names its oracle in its title/comment):

  • Proxy: counting accuracy against the fixture leaf's constant usage payload (5+2 per call; three calls = 21 = cap 28−7); wall engagement exactly at bucket − margin with reasoning counted once (seeded boundary rows); group separation; reset at 00:00 UTC on the pinned clock; fail-closed 502 with no fall-through leg and zero leaf hits; counter persistence across a proxy restart (fresh process, same ledger).
  • Checker: well-formed declaration passes the full checker; missing groups, non-positive bucket, margin ≥ bucket, double-mapped model, unmapped catalog model, and outside-catalog model each fail naming the offender.

Local runs: proxy suite 85/85, route-rules suite 52/52, the remaining config-guard suites 91/91, and all three guards pass on the real .opencode/opencode.jsonc. CI runs on main per repo policy; the nightly opencode-config-guard covers these fixtures.

Closes #13839

{
  "direct_parent": null,
  "links": [],
  "lineage": {
    "design": null,
    "roadmap_item": null,
    "priority": null
  },
  "pm_meta": 1,
  "provenance": {
    "queue_id": null,
    "scheduled_start": null,
    "picked_up_at": null,
    "work_started_at": null,
    "created_at": "2026-11-24T02:37:52Z",
    "session_id": null,
    "lane": null,
    "host": "htch-runtime",
    "worktree": "/home/hatch/workspace/lanes/lane-a/code-wt-13839",
    "model": null,
    "lane_start_issues": [],
    "workspace_ref": {
      "kind": "worktree",
      "id": "/home/hatch/workspace/lanes/lane-a/code-wt-13839",
      "repo": "developing-today/code",
      "branch": "lane/metering-wall-13839",
      "sha": "aa147d6ac350ba104d9a7d9d012f555c3bea7ffe"
    },
    "auth_subject": "archer",
    "stamped_by": "fleet 0.1.0 (native)"
  }
}

Run Proof on this PR
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…free (#13839)

The free alias draws on OpenAI's daily complimentary buckets
(large group 1M tokens/day, reset 00:00 UTC) whose exhaustion is
silent — traffic just starts billing — so no failure-driven wall
could ever fire for it. Now the proxy counts the day's usage per
group from the route_attempt ledger (tokens_input + tokens_output,
the response's total; reasoning/cache tokens are subsets of those
and count exactly once) and walls the alias's pairs at bucket
minus margin until the 00:00 UTC reset, inside the existing
seating gate. The counter is the ledger, so it survives restarts
by construction. Unlike the failure-driven walls, the gate's
walk-anyway bypass never resurrects a metered pair: fail closed,
or the wall would bill the balance it exists to protect.

Config: a tokenMetering declaration on the openai-payg-free block
(large group, 1M/day, 20K margin, all three catalog models).
Checker: fail-closed validation of the declaration — shape,
margin < bucket, every catalog model mapped to exactly one group,
no mapped model outside the catalog.

Tests (each names its oracle): counting accuracy against the
fixture leaf's constant usage payload, wall engagement exactly at
bucket minus margin with reasoning counted once, group
separation, reset at 00:00 UTC on a pinned clock, fail-closed
with no fall-through leg, and counter persistence across a proxy
restart. Proxy suite 85/85, route-rules suite 52/52, all
opencode-config-guard checks pass on the real config.

Closes #13839
@coderabbitai

coderabbitai Bot commented Nov 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 48 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 084e1a91-0e88-4d0a-8829-a835f88e37e9
Commits

Reviewing files that changed from the base of the PR and between 8b767a0 and aa147d6.

Files selected for processing: 5

  • .opencode/opencode.jsonc
  • bin/check-opencode-route-rules.mjs
  • bin/check-opencode-route-rules.test.mjs
  • bin/opencode-route-proxy.mjs
  • bin/opencode-route-proxy.test.mjs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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.

Route proxy: complimentary-token metering wall for openai-payg-free (per-group daily counting, wall at bucket, reset 00:00 UTC)

1 participant