Parallelize the /v1/models and /api/oauth/usage calls using asyncio.gather in _do_fetch_provider_usage

This commit is contained in:
null 2026-05-24 19:18:25 -05:00
parent d406beec56
commit 6bd6390b47
1 changed files with 21 additions and 5 deletions

View File

@ -1022,23 +1022,39 @@ async def _do_fetch_provider_usage(
checked_at=utcnow(), reachable=False,
error="No API key configured.",
)
elif api_key:
result = await _fetch_anthropic(api_key, base_url)
else:
result = ProviderUsageLive(
provider=provider, account_key=account_key,
checked_at=utcnow(), reachable=True,
)
# Overlay subscription windows from OAuth token (auto or explicit)
if effective_session_key and result.reachable is not False:
elif api_key and effective_session_key:
# Fire both calls concurrently — halves latency and eliminates the
# sequential back-to-back requests to api.anthropic.com that caused
# the subscription endpoint to 429 on every initial load.
subscription_attempted = True
sub_windows = await _fetch_anthropic_subscription(effective_session_key)
result, sub_windows = await asyncio.gather(
_fetch_anthropic(api_key, base_url),
_fetch_anthropic_subscription(effective_session_key),
)
if result.reachable is not False:
if sub_windows:
result.subscription_windows = sub_windows
result.reachable = True
else:
result.error = result.error or "No subscription data returned."
elif api_key:
result = await _fetch_anthropic(api_key, base_url)
else:
# OAuth token only — no API key configured
subscription_attempted = True
result = ProviderUsageLive(
provider=provider, account_key=account_key,
checked_at=utcnow(), reachable=True,
)
sub_windows = await _fetch_anthropic_subscription(effective_session_key)
if sub_windows:
result.subscription_windows = sub_windows
else:
result.error = "No subscription data returned."
elif provider in ("openai", "codex"):
if not api_key and not session_key:
result = ProviderUsageLive(