From f0bd9b49508d1c4907b800481da143c7031ab39f Mon Sep 17 00:00:00 2001 From: null Date: Sun, 24 May 2026 17:23:41 -0500 Subject: [PATCH] feat: implement retry logic for rate-limited requests to the Anthropic subscription endpoint --- backend/app/services/provider_usage.py | 36 +++++++++++++++----------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/backend/app/services/provider_usage.py b/backend/app/services/provider_usage.py index 76f3e0e..664fe2b 100644 --- a/backend/app/services/provider_usage.py +++ b/backend/app/services/provider_usage.py @@ -755,21 +755,27 @@ async def _fetch_anthropic_subscription(session_key: str) -> list[SubscriptionWi starts with ``sk-ant-``). Returns an empty list if the key is invalid or the endpoint is unreachable. """ - async with httpx.AsyncClient(timeout=REQUEST_TIMEOUT) as client: - try: - resp = await client.get( - _ANTHROPIC_SUBSCRIPTION_URL, - headers={ - "Authorization": f"Bearer {session_key}", - "User-Agent": "pipeline", - "Accept": "application/json", - "anthropic-version": "2023-06-01", - "anthropic-beta": "oauth-2025-04-20", - }, - ) - except Exception as exc: - logger.warning("provider_usage.subscription.anthropic.fetch_failed error=%s", exc) - return [] + headers = { + "Authorization": f"Bearer {session_key}", + "User-Agent": "pipeline", + "Accept": "application/json", + "anthropic-version": "2023-06-01", + "anthropic-beta": "oauth-2025-04-20", + } + # Retry once on 429 — the subscription endpoint can be rate-limited when + # called right after another api.anthropic.com request (e.g. /v1/models). + for attempt in range(2): + async with httpx.AsyncClient(timeout=REQUEST_TIMEOUT) as client: + try: + resp = await client.get(_ANTHROPIC_SUBSCRIPTION_URL, headers=headers) + except Exception as exc: + logger.warning("provider_usage.subscription.anthropic.fetch_failed error=%s", exc) + return [] + if resp.status_code == 429 and attempt == 0: + import asyncio as _asyncio + await _asyncio.sleep(1.5) + continue + break if not resp.status_code == 200: logger.debug(