Skip to content

[codex] polish API-key USD quota usage output - #4398

Merged
diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.33from
Witroch4:codex/usage-quota-percent-reset
Jun 21, 2026
Merged

diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.33from
Witroch4:codex/usage-quota-percent-reset

Conversation

@Witroch4

Copy link
Copy Markdown
Contributor

Summary

  • show API-key USD quota percentages and reset hints in @@om-usage
  • include quota percentage and Resets in ... in USD quota 400 rejection messages
  • cut weekly API-key USD usage windows at observed provider weekly quota reset drops, not only resetAt - 7 days

Why

API-key USD quota enforcement needs to follow the same effective quota cycle that users see from the Claude Code account. Anthropic can reset the weekly quota before the naive resetAt - 7 days boundary. When that happens, a strict per-key USD cap would keep counting spend from the old provider quota cycle and incorrectly block users after the upstream quota already reset.

OmniRoute already stores provider quota_snapshots, so this PR detects the real weekly reset by watching the provider usage percentage drop, for example 28% -> 0% or back to 1%, and uses that observed snapshot time as the start of the weekly USD window for all API keys on that provider connection.

Behavior

  • With USD quota enabled, @@om-usage now reports daily/weekly quota, spend, percentage, and reset hint for the API key itself.
  • Quota-blocked requests still return HTTP 400 for Claude Code compatibility, now with percentage and reset hint.
  • Weekly USD spend uses the latest observed weekly provider reset/drop from quota_snapshots; if unavailable, it falls back to resetAt - 7 days, then rolling 7 days.

Validation

  • node --import tsx/esm --test tests/unit/api-key-usage-limits.test.ts tests/unit/internal-usage-command.test.ts — 13/13 pass
  • npm run typecheck:core
  • npm run check:file-size
  • npm run lint — 0 errors, existing warnings remain
  • Docker build: docker build --target runner-base -t omniroute:usage-quota-percent-reset-cut-20260620 .
  • Deployed smoke test on 100.64.0.1:20128: /login returned HTTP 200 and container is healthy

@Witroch4
Witroch4 requested a review from diegosouzapw as a code owner June 20, 2026 14:41

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances API key usage limit tracking and reporting by introducing daily reset times, calculating usage percentages, and displaying remaining time until reset for both daily and weekly quotas. It also implements logic to detect observed weekly quota resets by querying database snapshots. The reviewer identified a critical performance bottleneck in getObservedWeeklyWindowStartIso where an unbounded database query is executed on every API request, and suggested optimizing the query with date range and reset day filters.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +209 to +232
function getObservedWeeklyWindowStartIso(
connectionId: string,
targetResetAtIso: string,
nowMs: number
): string | null {
if (!connectionId || !targetResetAtIso) return null;

try {
const rows = getDbInstance()
.prepare(
`
SELECT
remaining_percentage as remainingPercentage,
next_reset_at as nextResetAt,
created_at as createdAt
FROM quota_snapshots
WHERE connection_id = @connectionId
AND LOWER(window_key) LIKE '%weekly%'
AND LOWER(window_key) NOT LIKE '%sonnet%'
AND created_at <= @nowIso
ORDER BY created_at ASC, id ASC
`
)
.all({ connectionId, nowIso: new Date(nowMs).toISOString() }) as QuotaSnapshotRow[];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Performance Bottleneck: Unbounded Database Query on Hot Path

The current query in getObservedWeeklyWindowStartIso fetches all weekly snapshots for the given connection from the database without any date range or reset day constraints. Since this function is called on every single API request (via getApiKeyUsageLimitStatus to check usage limits), and snapshots are retained for up to 90 days, this will load, parse, and iterate over thousands of irrelevant rows in memory on every request.

Solution

  1. Filter by next_reset_at LIKE @targetDayPattern to only fetch snapshots that match the target reset day.
  2. Filter by created_at >= @sinceIso (e.g., 8 days ago) to restrict the scan range to the current weekly cycle.
function getObservedWeeklyWindowStartIso(
  connectionId: string, 
  targetResetAtIso: string, 
  nowMs: number
): string | null {
  if (!connectionId || !targetResetAtIso) return null;

  const targetDay = resetDay(targetResetAtIso);
  if (!targetDay) return null;

  const sinceIso = new Date(nowMs - 8 * 24 * 60 * 60 * 1000).toISOString();

  try {
    const rows = getDbInstance()
      .prepare(
        `
        SELECT
          remaining_percentage as remainingPercentage,
          next_reset_at as nextResetAt,
          created_at as createdAt
        FROM quota_snapshots
        WHERE connection_id = @connectionId
          AND LOWER(window_key) LIKE '%weekly%'
          AND LOWER(window_key) NOT LIKE '%sonnet%'
          AND created_at <= @nowIso
          AND created_at >= @sinceIso
          AND next_reset_at LIKE @targetDayPattern
        ORDER BY created_at ASC, id ASC
      `
      )
      .all({
        connectionId,
        nowIso: new Date(nowMs).toISOString(),
        sinceIso,
        targetDayPattern: `${targetDay}%`,
      }) as QuotaSnapshotRow[];

@diegosouzapw
diegosouzapw changed the base branch from release/v3.8.30 to release/v3.8.32 June 20, 2026 18:31
@diegosouzapw
diegosouzapw changed the base branch from release/v3.8.32 to release/v3.8.33 June 21, 2026 14:03
Updates @@om-usage for USD quota-enabled API keys to report per-key daily/weekly usage percentages and reset hints, includes quota percent/reset details in 400 quota rejection messages, and cuts the weekly USD window at observed provider quota reset drops.

Validations:

- node --import tsx/esm --test tests/unit/api-key-usage-limits.test.ts tests/unit/internal-usage-command.test.ts

- npm run typecheck:core

- npm run check:file-size

- npm run lint
@diegosouzapw
diegosouzapw force-pushed the codex/usage-quota-percent-reset branch from 718fa6e to 3272802 Compare June 21, 2026 15:02
@diegosouzapw
diegosouzapw merged commit d3f1b23 into diegosouzapw:release/v3.8.33 Jun 21, 2026
3 checks passed
@diegosouzapw

Copy link
Copy Markdown
Owner

Obrigado pela contribuição, @Witroch4! 🙌 Reconstruí o PR sobre a release/v3.8.33 (a base estava ~75 commits atrás — padrão squash-base-stale), preservando o commit-feature e os 13 testes. A melhoria de UX de quota (percentual + reset hints) e o corte da janela semanal no reset real entram na próxima release.

diegosouzapw added a commit that referenced this pull request Jun 21, 2026
Cycle drift + #4398 +2. Documented breakdown. Rule #9.
@diegosouzapw diegosouzapw mentioned this pull request Jun 22, 2026
tkgo11 pushed a commit to tkgo11/OmniRoute that referenced this pull request Sep 23, 2026
…utoff (diegosouzapw#4398)

Adds USD usage percentages and reset hints to @@om-usage and 400 quota rejections, and cuts the weekly USD window at the real observed reset via quota_snapshots instead of resetAt-7d. Rebuilt onto release/v3.8.33 (squash-base-stale; delta = 5 files/+353).

Integrated into release/v3.8.33.
tkgo11 pushed a commit to tkgo11/OmniRoute that referenced this pull request Sep 23, 2026
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.

2 participants