router-for-me/CLIProxyAPI · error

xai device token request failed with status %d: %s

Error message

xai device token request failed with status %d: %s

What it means

The token exchange finished without an error payload, but the HTTP status was not 200. Because the error field was empty, this is an unexpected server state — e.g. 5xx with an empty body, or a redirect that landed on a non-JSON page — and the status plus raw body are surfaced.

Source

Thrown at internal/auth/xai/xai.go:320

			return nil, nil, interval, true
		case "slow_down":
			nextInterval := interval + defaultPollInterval
			return nil, nil, nextInterval, true
		case "expired_token":
			return nil, fmt.Errorf("xai device code expired"), interval, false
		case "access_denied":
			return nil, fmt.Errorf("xai device authorization denied"), interval, false
		default:
			desc := strings.TrimSpace(payload.ErrorDescription)
			if desc != "" {
				return nil, fmt.Errorf("xai device token error: %s: %s", payload.Error, desc), interval, false
			}
			return nil, fmt.Errorf("xai device token error: %s", payload.Error), interval, false
		}
	}

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("xai device token request failed with status %d: %s", resp.StatusCode, strings.TrimSpace(string(body))), interval, false
	}
	if strings.TrimSpace(payload.AccessToken) == "" {
		return nil, fmt.Errorf("xai device token response missing access_token"), interval, false
	}

	email, subject := parseJWTIdentity(payload.IDToken)
	return buildTokenData(payload.AccessToken, payload.RefreshToken, payload.IDToken, payload.TokenType, payload.ExpiresIn, email, subject), nil, interval, false
}

// RefreshTokens refreshes an xAI access token.
func (a *XAIAuth) RefreshTokens(ctx context.Context, refreshToken, tokenEndpoint string) (*TokenData, error) {
	if strings.TrimSpace(refreshToken) == "" {
		return nil, fmt.Errorf("xai token refresh: refresh token is required")
	}
	if ctx == nil {
		ctx = context.Background()
	}
	refreshToken = strings.TrimSpace(refreshToken)

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Check the status code in the message: 5xx suggests retrying later, 4xx suggests inspecting the body
  2. Verify xAI status/health pages for auth service incidents
  3. Restart the flow with a fresh device code if the code might have been consumed
Defensive patterns

Strategy: retry

Try / catch

if err != nil && strings.Contains(err.Error(), "failed with status") {
    status := extractStatusCode(err)
    if status >= 500 {
        time.Sleep(backoff)
        return restartDeviceFlow(ctx)
    }
    return err // 4xx: inspect the surfaced body
}

Prevention

When it happens

Trigger: Token endpoint returns 500/502/503 with an empty or non-standard body after the device code is granted.

Common situations: xAI auth service outage or deploy blip; gateway timeout pages; retrying an already-consumed device code in a way that yields a bare non-200.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/5e1b77a17c6aa5ea. Report an issue: GitHub.