Hmbown/CodeWhale · error · anyhow::Error

Failed to call DeepSeek Chat API: HTTP

Error message

Failed to call DeepSeek Chat API: HTTP {status}: {error_text}

What it means

The blocking DeepSeek chat-completions call returned a non-2xx status. The body is read with a byte bound and sanitized through `sanitize_http_error_body` with the provider display name before being embedded in the error. The provider response status is also recorded for metrics (`record_provider_response`) before the bail.

Solutions

  1. Read the sanitized body; DeepSeek's own error message names the concrete problem
  2. 402: top up the DeepSeek account or switch key
  3. 401: update the API key in config/secret storage
  4. 404: fix the model id (deepseek-chat vs deepseek-reasoner naming)
  5. 429: reduce concurrency and retry with backoff

Example fix

// before
"model": "deepseek-chat-v3" // unknown id -> HTTP 404

// after
"model": "deepseek-chat"
Defensive patterns

Strategy: retry

Validate before calling

anyhow::ensure!(valid_deepseek_model(&request.model), "unknown model {}", request.model);
anyhow::ensure!(!api_key.is_empty(), "DeepSeek API key missing");

Type guard

fn is_retryable_status_in(err: &anyhow::Error) -> bool {
    let m = err.to_string();
    m.contains("429") || m.contains("HTTP 5")
}

Try / catch

match client.create_message_chat(&prepared).await {
    Ok(resp) => Ok(resp),
    Err(err) if is_retryable_status_in(&err) => backoff_and_retry(err).await,
    Err(err) if err.to_string().contains("402") => Err(anyhow!("DeepSeek balance exhausted: {err}")),
    Err(err) => Err(err),
}

Prevention

When it happens

Trigger: Non-success from `send_json_with_retry` on the chat endpoint: 401 bad key, 402 insufficient balance, 404 unknown model, 400 invalid parameters, 429 rate limited.

Common situations: Out-of-credit DeepSeek accounts (402 is DeepSeek-specific), wrong model names, aggressive parallelism tripping 429s, keys rotated but not updated in config.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@0c42157ee5 (2026-08-20). Data as JSON: /api/errors/8bb59d7222c4f0bb. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/client/chat.rs:1141

            None
        };

        // The endpoint was resolved by the shared seam alongside the body, so
        // a route-shape decision (e.g. DeepSeek's strict-tools `/beta` path)
        // cannot be made twice with two different answers.
        let url = prepared.endpoint.url.as_str();
        let response = self.send_json_with_retry(url, body).await?;

        let status = response.status();
        crate::client::record_provider_response(self.api_provider, status.as_u16());
        if !status.is_success() {
            let raw_error_text = bounded_error_text(response, ERROR_BODY_MAX_BYTES).await;
            let error_text = sanitize_http_error_body(
                Some(self.api_provider.display_name()),
                status.as_u16(),
                &raw_error_text,
            );
            anyhow::bail!("Failed to call DeepSeek Chat API: HTTP {status}: {error_text}");
        }

        let response_text = response
            .text()
            .await
            .context("Failed to read Chat API response body")?;
        let value: Value =
            serde_json::from_str(&response_text).context("Failed to parse Chat API JSON")?;
        let parsed = parse_chat_message_for_route(&value, self.api_provider, &self.base_url)?;
        if let Some(key) = response_cache_key {
            crate::llm_response_cache::response_cache().put(key, parsed.clone());
        }
        Ok(parsed)
    }
}

impl DeepSeekClient {
    async fn open_chat_stream_response(

View on GitHub (pinned to 0c42157ee5)