feat: implement retry logic for rate-limited requests to the Anthropic subscription endpoint

This commit is contained in:
null 2026-05-24 17:23:41 -05:00
parent 79ab63b206
commit f0bd9b4950
1 changed files with 21 additions and 15 deletions

View File

@ -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 starts with ``sk-ant-``). Returns an empty list if the key is invalid or
the endpoint is unreachable. the endpoint is unreachable.
""" """
async with httpx.AsyncClient(timeout=REQUEST_TIMEOUT) as client: headers = {
try: "Authorization": f"Bearer {session_key}",
resp = await client.get( "User-Agent": "pipeline",
_ANTHROPIC_SUBSCRIPTION_URL, "Accept": "application/json",
headers={ "anthropic-version": "2023-06-01",
"Authorization": f"Bearer {session_key}", "anthropic-beta": "oauth-2025-04-20",
"User-Agent": "pipeline", }
"Accept": "application/json", # Retry once on 429 — the subscription endpoint can be rate-limited when
"anthropic-version": "2023-06-01", # called right after another api.anthropic.com request (e.g. /v1/models).
"anthropic-beta": "oauth-2025-04-20", for attempt in range(2):
}, async with httpx.AsyncClient(timeout=REQUEST_TIMEOUT) as client:
) try:
except Exception as exc: resp = await client.get(_ANTHROPIC_SUBSCRIPTION_URL, headers=headers)
logger.warning("provider_usage.subscription.anthropic.fetch_failed error=%s", exc) except Exception as exc:
return [] 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: if not resp.status_code == 200:
logger.debug( logger.debug(