{"record":{"id":"8bb59d7222c4f0bb","repo":"Hmbown/CodeWhale","slug":"failed-to-call-deepseek-chat-api-http-status","errorCode":null,"errorMessage":"Failed to call DeepSeek Chat API: HTTP {status}: {error_text}","messagePattern":"Failed to call DeepSeek Chat API: HTTP (.+?): (.+?)","errorType":"http","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/client/chat.rs","lineNumber":1141,"sourceCode":"            None\n        };\n\n        // The endpoint was resolved by the shared seam alongside the body, so\n        // a route-shape decision (e.g. DeepSeek's strict-tools `/beta` path)\n        // cannot be made twice with two different answers.\n        let url = prepared.endpoint.url.as_str();\n        let response = self.send_json_with_retry(url, body).await?;\n\n        let status = response.status();\n        crate::client::record_provider_response(self.api_provider, status.as_u16());\n        if !status.is_success() {\n            let raw_error_text = bounded_error_text(response, ERROR_BODY_MAX_BYTES).await;\n            let error_text = sanitize_http_error_body(\n                Some(self.api_provider.display_name()),\n                status.as_u16(),\n                &raw_error_text,\n            );\n            anyhow::bail!(\"Failed to call DeepSeek Chat API: HTTP {status}: {error_text}\");\n        }\n\n        let response_text = response\n            .text()\n            .await\n            .context(\"Failed to read Chat API response body\")?;\n        let value: Value =\n            serde_json::from_str(&response_text).context(\"Failed to parse Chat API JSON\")?;\n        let parsed = parse_chat_message_for_route(&value, self.api_provider, &self.base_url)?;\n        if let Some(key) = response_cache_key {\n            crate::llm_response_cache::response_cache().put(key, parsed.clone());\n        }\n        Ok(parsed)\n    }\n}\n\nimpl DeepSeekClient {\n    async fn open_chat_stream_response(","sourceCodeStart":1123,"sourceCodeEnd":1159,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/tui/src/client/chat.rs#L1123-L1159","documentation":"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.","triggerScenarios":"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.","commonSituations":"Out-of-credit DeepSeek accounts (402 is DeepSeek-specific), wrong model names, aggressive parallelism tripping 429s, keys rotated but not updated in config.","solutions":["Read the sanitized body; DeepSeek's own error message names the concrete problem","402: top up the DeepSeek account or switch key","401: update the API key in config/secret storage","404: fix the model id (deepseek-chat vs deepseek-reasoner naming)","429: reduce concurrency and retry with backoff"],"exampleFix":"// before\n\"model\": \"deepseek-chat-v3\" // unknown id -> HTTP 404\n\n// after\n\"model\": \"deepseek-chat\"","handlingStrategy":"retry","validationCode":"anyhow::ensure!(valid_deepseek_model(&request.model), \"unknown model {}\", request.model);\nanyhow::ensure!(!api_key.is_empty(), \"DeepSeek API key missing\");","typeGuard":"fn is_retryable_status_in(err: &anyhow::Error) -> bool {\n    let m = err.to_string();\n    m.contains(\"429\") || m.contains(\"HTTP 5\")\n}","tryCatchPattern":"match client.create_message_chat(&prepared).await {\n    Ok(resp) => Ok(resp),\n    Err(err) if is_retryable_status_in(&err) => backoff_and_retry(err).await,\n    Err(err) if err.to_string().contains(\"402\") => Err(anyhow!(\"DeepSeek balance exhausted: {err}\")),\n    Err(err) => Err(err),\n}","preventionTips":["Monitor for 402 specifically — DeepSeek charges per token and balances deplete silently","Cache model ids from the models endpoint instead of hardcoding","Read the sanitized provider message; it names the exact invalid field"],"tags":["deepseek","chat-completions","http-status","api-error","rust"],"backgroundTag":"deepseek-api-error","analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}