Provider usage endpoint degrades gracefully instead of 500

/dashboard/api/provider/{id}/usage returned HTTP 500 whenever a usage fetch
failed — most commonly when a provider's OAuth credentials are expired/invalid
(e.g. the codex_* providers). The Providers dashboard polls every provider's
usage on a timer, so this spammed 500s (visible in nginx/access logs) for any
stale-credential provider.

A monitoring poll failing is an expected soft condition, not a server error.
Return 200 with usage:None and an 'unavailable' flag so the UI can render an
offline state; genuine data still returns normally.
Co-Authored-By: 's avatarClaude Opus 4.8 (1M context) <noreply@anthropic.com>
parent 11df0261
......@@ -2284,7 +2284,13 @@ async def api_provider_usage(request: Request, provider_id: str):
logger.warning(f"api_provider_usage error for {provider_id}: {e}")
if cached:
return JSONResponse({"success": True, "supported": True, "usage": cached['usage_data'], "stale": True})
return JSONResponse({"success": False, "error": str(e)}, status_code=500)
# A usage-fetch failure — invalid/expired credentials, provider offline,
# or an upstream error — is an expected soft condition for a monitoring
# poll, not a server error. Returning 500 spammed the dashboard (which
# polls every provider's usage on a timer) with 500s for any provider
# whose credentials are stale. Degrade gracefully: 200 with usage:None
# and an 'unavailable' flag so the UI can render an offline state.
return JSONResponse({"success": True, "supported": True, "usage": None, "unavailable": True, "error": str(e)})
@router.post("/dashboard/api/rotation")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment