router-for-me/CLIProxyAPI · error
xai device token: read response: %w
Error message
xai device token: read response: %w
What it means
The token endpoint responded and the connection succeeded, but reading the response body with io.ReadAll failed. Usually a broken connection mid-body: the server or an intermediary closed the stream before Content-Length bytes arrived, or the context was cancelled during the read.
Source
Thrown at internal/auth/xai/xai.go:283
if err != nil {
return nil, fmt.Errorf("xai device token: create request: %w", err), interval, false
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
resp, err := a.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("xai device token request failed: %w", err), interval, false
}
defer func() {
if errClose := resp.Body.Close(); errClose != nil {
log.Errorf("xai device token: close response body error: %v", errClose)
}
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("xai device token: read response: %w", err), interval, false
}
var payload struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
IDToken string `json:"id_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
if err = json.Unmarshal(body, &payload); err != nil {
return nil, fmt.Errorf("xai device token: parse response: %w", err), interval, false
}
if payload.Error != "" {
switch payload.Error {
case "authorization_pending":View on GitHub (pinned to 78f0c4079e)
Solutions
- Retry with a fresh device code; transient truncation is the most common cause
- Disable or tune keep-alive/idle timeouts on the httpClient if resets recur
- Check whether a proxy between the client and xAI truncates responses
Defensive patterns
Strategy: retry
Try / catch
if err != nil && strings.Contains(err.Error(), "read response") {
log.Warnf("truncated token response, retrying with fresh device code: %v", err)
return restartDeviceFlow(ctx)
} Prevention
- Retry on body-read failures; they are almost always transient
- Tune httpClient Transport idle-connection settings if resets recur behind proxies
When it happens
Trigger: Connection reset while streaming the token response body; context cancellation during body read; proxy truncating the response.
Common situations: Flaky networks, aggressive proxy timeouts, server closing keep-alive connections, long-running poll loops hitting idle-connection reuse problems.
Related errors
- xai device token request failed: %w
- failed to read auth file: %w
- read response: %w
- read Claude OAuth %s response: %w
- failed to read token response: %w
AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15).
Data as JSON: /api/errors/024a32b73b5d9fea.
Report an issue: GitHub.