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
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(