router-for-me/CLIProxyAPI · error
kimi: failed to read token response: %w
Error message
kimi: failed to read token response: %w
What it means
The token endpoint returned headers successfully but io.ReadAll of its body failed mid-stream during a polling tick. Same class as a body read failure on the device-code request, but occurring on POST /api/oauth/token; it aborts the polling loop (shouldContinue=false).
Source
Thrown at internal/auth/kimi/kimi.go:295
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
for k, v := range c.commonHeaders() {
req.Header.Set(k, v)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("kimi: token request failed: %w", err), false
}
defer func() {
if errClose := resp.Body.Close(); errClose != nil {
log.Errorf("kimi token exchange: close body error: %v", errClose)
}
}()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("kimi: failed to read token response: %w", err), false
}
// Parse response - Kimi returns 200 for both success and pending states
var oauthResp struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn float64 `json:"expires_in"`
Scope string `json:"scope"`
}
if err = json.Unmarshal(bodyBytes, &oauthResp); err != nil {
return nil, fmt.Errorf("kimi: failed to parse token response: %w", err), false
}
if oauthResp.Error != "" {View on GitHub (pinned to 78f0c4079e)
Solutions
- Re-run the login flow — a fresh device code and new connections usually clear it
- Check for proxy interference on auth.kimi.com and bypass it
- If reproducible every time, capture with curl -i to see whether the body is consistently truncated (server-side issue)
Defensive patterns
Strategy: retry
Try / catch
if err != nil && strings.Contains(err.Error(), "failed to read token response") {
// restart login; single read failure aborts the poll loop
} Prevention
- Avoid networks with aggressive stream idle timeouts during login
- Restart the flow on any mid-body read failure
When it happens
Trigger: Connection reset between response headers and body on the token endpoint, proxy or load balancer cutting the stream, truncated chunked encoding from auth.kimi.com.
Common situations: Flaky networks during the multi-minute authorization wait, mobile tethers, corporate proxies with short idle/stream timeouts.
Related errors
- kimi: failed to read device code response: %w
- kimi: token request failed: %w
- kimi: device code request failed: %w
- kimi: device code request failed with status %d: %s
- kimi: failed to create token request: %w
AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15).
Data as JSON: /api/errors/cacd141fc8eb850d.
Report an issue: GitHub.