From 6bd6390b47af0e32d4f54cdfc1342883238a2d02 Mon Sep 17 00:00:00 2001 From: null Date: Sun, 24 May 2026 19:18:25 -0500 Subject: [PATCH] Parallelize the /v1/models and /api/oauth/usage calls using asyncio.gather in _do_fetch_provider_usage --- backend/app/services/provider_usage.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/backend/app/services/provider_usage.py b/backend/app/services/provider_usage.py index 972167b..32d9a51 100644 --- a/backend/app/services/provider_usage.py +++ b/backend/app/services/provider_usage.py @@ -1022,22 +1022,38 @@ async def _do_fetch_provider_usage( checked_at=utcnow(), reachable=False, error="No API key configured.", ) + + 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 + 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, ) - # Overlay subscription windows from OAuth token (auto or explicit) - if effective_session_key and result.reachable is not False: - subscription_attempted = True sub_windows = await _fetch_anthropic_subscription(effective_session_key) if sub_windows: result.subscription_windows = sub_windows - result.reachable = True else: - result.error = result.error or "No subscription data returned." + result.error = "No subscription data returned." elif provider in ("openai", "codex"): if not api_key and not session_key: