Repository navigation
[codex] polish API-key USD quota usage output - #4398
diegosouzapw merged 1 commit into
Conversation
There was a problem hiding this comment.
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.
| 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[]; |
There was a problem hiding this comment.
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
- Filter by
next_reset_at LIKE @targetDayPatternto only fetch snapshots that match the target reset day. - 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[];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
718fa6e to
3272802
Compare
|
Obrigado pela contribuição, @Witroch4! 🙌 Reconstruí o PR sobre a |
…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.
…#4529) Cycle drift + diegosouzapw#4398 +2. Documented breakdown. Rule diegosouzapw#9.
Summary
@@om-usageResets in ...in USD quota 400 rejection messagesresetAt - 7 daysWhy
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 daysboundary. 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 example28% -> 0%or back to1%, and uses that observed snapshot time as the start of the weekly USD window for all API keys on that provider connection.Behavior
@@om-usagenow reports daily/weekly quota, spend, percentage, and reset hint for the API key itself.quota_snapshots; if unavailable, it falls back toresetAt - 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 passnpm run typecheck:corenpm run check:file-sizenpm run lint— 0 errors, existing warnings remaindocker build --target runner-base -t omniroute:usage-quota-percent-reset-cut-20260620 .100.64.0.1:20128:/loginreturned HTTP 200 and container is healthy