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

  1. Retry with a fresh device code; transient truncation is the most common cause
  2. Disable or tune keep-alive/idle timeouts on the httpClient if resets recur
  3. 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

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


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