chenhg5/cc-connect · error

request usage endpoint: %w

Error message

request usage endpoint: %w

What it means

fetchUsage performs an HTTP GET to the Codex/ChatGPT usage endpoint using the OAuth bearer token and account id. This error wraps any transport-level failure from client.Do — DNS failure, connection refused/reset, TLS errors, timeouts — before an HTTP response is available. It is thrown whenever the usage endpoint cannot be reached at all.

Source

Thrown at agent/codex/usage.go:104

	return codexOAuthTokens{
		AccessToken: payload.Tokens.AccessToken,
		AccountID:   payload.Tokens.AccountID,
	}, nil
}

func (a *Agent) fetchUsage(ctx context.Context, client *http.Client, tokens codexOAuthTokens) (*core.UsageReport, error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, codexUsageURL, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Authorization", "Bearer "+tokens.AccessToken)
	req.Header.Set("ChatGPT-Account-Id", tokens.AccountID)
	req.Header.Set("User-Agent", "codex-cli")

	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("request usage endpoint: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
		return nil, fmt.Errorf("usage endpoint returned status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
	}

	var payload codexUsageResponse
	if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
		return nil, fmt.Errorf("decode usage response: %w", err)
	}

	return mapCodexUsage(payload), nil
}

func mapCodexUsage(payload codexUsageResponse) *core.UsageReport {
	report := &core.UsageReport{

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Verify basic connectivity: `curl -v https://chatgpt.com/backend-api/...` from the same host/user
  2. Check proxy env vars (HTTPS_PROXY/HTTP_PROXY/NO_PROXY) are set for the process running cc-connect
  3. Confirm DNS resolves inside containers/VPNs; test `getent hosts chatgpt.com`
  4. Retry after network recovery; if behind a firewall, allowlist the usage endpoint host

Example fix

// before: no network pre-check, opaque failure
usage, err := agent.GetUsage(ctx)
// after: fail fast with a clear message
if err := probeHTTPS("https://chatgpt.com"); err != nil {
    log.Fatalf("network unreachable for codex usage endpoint: %v (check proxy/VPN)", err)
}
usage, err := agent.GetUsage(ctx)
Defensive patterns

Strategy: retry

Validate before calling

conn, err := net.DialTimeout("tcp", "chatgpt.com:443", 3*time.Second)
if err != nil { return fmt.Errorf("usage endpoint unreachable: %w", err) }
conn.Close()

Try / catch

report, err := agent.GetUsage(ctx)
if err != nil && strings.Contains(err.Error(), "request usage endpoint") {
    // transient transport failure: retry with backoff
    return retryWithBackoff(3, 2*time.Second, func() error { _, err = agent.GetUsage(ctx); return err })
}

Prevention

When it happens

Trigger: client.Do(req) returns err inside fetchUsage, called from GetUsage with valid tokens read from auth.json: network unreachable, proxy misconfigured, DNS failure, TLS interception, or request context timeout.

Common situations: Offline or firewalled environments blocking chatgpt.com; corporate proxies requiring configuration (HTTPS_PROXY) not set for the daemon; VPN required; IPv6/DNS issues in containers; long-running bridge whose connections break after network changes.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/b90240dae20cfc4e. Report an issue: GitHub.