diff --git a/crates/claude-connector/src/messages.rs b/crates/claude-connector/src/messages.rs index eed496d..3eee27e 100644 --- a/crates/claude-connector/src/messages.rs +++ b/crates/claude-connector/src/messages.rs @@ -27,7 +27,11 @@ //! //! Failures are typed as the Persona drain reads them: no answer could be had //! (connection, deadline, 5xx, 429, 402, 3xx) is an outage kind, and only a -//! request or answer the provider refused is a strike. +//! request or answer the provider refused is a strike. Two 400 +//! `invalid_request_error` answers are the exception: the provider reports a +//! spent balance and the operator's Console spend limit as a 400 whose message +//! begins with a fixed string (`SPEND_EXHAUSTED`), and only that string tells +//! them from a bad request, so they are matched, as 402 is, to `Authentication`. //! //! The assistant turn is replayed from what the contract carries: thinking //! blocks as parsed, text by its text, tool calls by id, name and input. The @@ -528,6 +532,21 @@ fn kind_of_status(status: u16) -> FailureKind { } } +/// The fixed beginnings of the provider's 400 messages for a spent balance +/// and a reached Console spend limit, lower-cased. +const SPEND_EXHAUSTED: [&str; 2] = [ + "your credit balance is too low", + "you have reached your specified api usage limits", +]; + +fn spend_exhausted(error: &Value) -> bool { + error["type"].as_str() == Some("invalid_request_error") + && error["message"].as_str().is_some_and(|text| { + let text = text.trim_start().to_lowercase(); + SPEND_EXHAUSTED.iter().any(|prefix| text.starts_with(prefix)) + }) +} + /// A failure the provider answered: its own type and message, bounded. fn http_failure(status: u16, body: &[u8]) -> (Vec, TransportOutcome) { let parsed: Value = serde_json::from_slice(body).unwrap_or(Value::Null); @@ -541,7 +560,12 @@ fn http_failure(status: u16, body: &[u8]) -> (Vec, TransportOutc message.push_str(": "); message.push_str(text); } - failed(kind_of_status(status), bounded(message), None) + let kind = if status == 400 && spend_exhausted(error) { + FailureKind::Authentication + } else { + kind_of_status(status) + }; + failed(kind, bounded(message), None) } /// A failure before an answer: the deadline, the size cap, the connection. diff --git a/crates/claude-connector/src/messages/tests.rs b/crates/claude-connector/src/messages/tests.rs index e94f0c9..4a5585b 100644 --- a/crates/claude-connector/src/messages/tests.rs +++ b/crates/claude-connector/src/messages/tests.rs @@ -684,7 +684,7 @@ fn t_c0_http_errors_typed() { ( 400, FailureKind::InvalidRequest, - r#"{"type":"error","error":{"type":"invalid_request_error","message":"Your credit balance is too low to access the Anthropic API."}}"#, + r#"{"type":"error","error":{"type":"invalid_request_error","message":"max_tokens must be positive"}}"#, ), ( 402, @@ -727,6 +727,66 @@ fn t_c0_http_errors_typed() { assert!(failure_of(&outcome).1.contains("credit balance is too low")); } +#[test] +fn a_spent_balance_or_a_reached_spend_limit_is_not_a_strike() { + let body = |kind: &str, message: &str| { + json!({"type": "error", "error": {"type": kind, "message": message}}).to_string() + }; + for (kind, message, expected) in [ + ( + "invalid_request_error", + "Your credit balance is too low", + FailureKind::Authentication, + ), + ( + "invalid_request_error", + "Your credit balance is too low to access the Anthropic API. Please go to Plans & Billing.", + FailureKind::Authentication, + ), + ( + "invalid_request_error", + "You have reached your specified API usage limits", + FailureKind::Authentication, + ), + ( + "invalid_request_error", + "You have reached your specified API usage limits. You will regain access on 2026-12-01 at 00:00 UTC.", + FailureKind::Authentication, + ), + ( + "invalid_request_error", + "YOU HAVE REACHED YOUR SPECIFIED API USAGE LIMITS. Later.", + FailureKind::Authentication, + ), + ( + "invalid_request_error", + "prompt is too long: 300000 tokens", + FailureKind::InvalidRequest, + ), + ( + "invalid_request_error", + "Note: your credit balance is too low is not how this starts", + FailureKind::InvalidRequest, + ), + ( + "api_error", + "Your credit balance is too low", + FailureKind::Unavailable, + ), + ] { + let status = if kind == "api_error" { 500 } else { 400 }; + let (url, _) = mock(vec![(status, body(kind, message))]); + let (_, outcome) = backend(url, Some(KEY)).run(&request()); + assert_eq!(failure_of(&outcome).0, expected, "{message}"); + } + // The same words on another status keep that status's kind. + let (_, outcome) = http_failure( + 404, + body("invalid_request_error", "Your credit balance is too low").as_bytes(), + ); + assert_eq!(failure_of(&outcome).0, FailureKind::Provider); +} + #[test] fn a_dead_endpoint_and_a_slow_one_are_typed() { let dead = TcpListener::bind("127.0.0.1:0").unwrap();