Path: redb-route/redb.Route.Llm/Providers/LlmExceptions.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Providers/LlmExceptions.cs ---- using System; using System.Net.Http; namespace redb.Route.Llm.Providers; /// /// Turns a failed HTTP response into the right provider exception. One place, because the /// mapping is the same for every OpenAI-shaped API and getting it wrong is invisible: a caller /// that cannot tell 429 from 401 either retries a dead key forever or gives up on a queue that /// would have cleared in a second. /// public static class LlmHttpErrors { /// /// Builds the exception for a non-2xx response: 429 → /// (honouring Retry-After in both its delta and date forms), 5xx and the soft /// "overloaded" 529 → , anything else → /// carrying the status. /// /// Provider identifier, for the exception payload. /// The failed response; only status and headers are read. /// Ready-made message — providers word their own summaries. /// Response body already read by the caller, for diagnostics. public static Exception FromResponse( string providerId, HttpResponseMessage response, string message, string? rawBody) { ArgumentNullException.ThrowIfNull(response); var status = (int)response.StatusCode; if (status == 429) { TimeSpan? retryAfter = null; if (response.Headers.RetryAfter is { } header) { if (header.Delta is { } delta) retryAfter = delta; else if (header.Date is { } when) retryAfter = when - DateTimeOffset.UtcNow; } return new LlmRateLimitException(providerId, message, retryAfter, rawBody); } // 529 is Anthropic's soft "overloaded"; it is not a standard code, and treating it as // transient is right for anyone who ever returns it. if (status == 529 || status is >= 500 and <= 599) return new LlmTransientException(providerId, message, status, rawBody); return new HttpRequestException(message, inner: null, statusCode: response.StatusCode); } } /// /// Provider signalled rate limiting (HTTP 429). Distinct from /// because the caller should respect /// rather than retry immediately. /// public sealed class LlmRateLimitException : Exception { /// Provider identifier ("anthropic", "openai", ...). public string ProviderId { get; } /// Suggested wait time before retry, if the provider sent one. public TimeSpan? RetryAfter { get; } /// Raw response body for diagnostics. public string? RawBody { get; } /// Creates a rate-limit exception. public LlmRateLimitException(string providerId, string message, TimeSpan? retryAfter = null, string? rawBody = null, Exception? inner = null) : base(message, inner) { ProviderId = providerId; RetryAfter = retryAfter; RawBody = rawBody; } } /// /// Transient provider failure that is safe to retry (HTTP 5xx, Anthropic's /// overloaded 529, network resets). The caller is expected /// to apply its own back-off / circuit-breaker policy. A call that ran out of /// its time limit is , not this. /// public sealed class LlmTransientException : Exception { /// Provider identifier. public string ProviderId { get; } /// HTTP status code, when known. public int? StatusCode { get; } /// Raw response body for diagnostics. public string? RawBody { get; } /// Creates a transient-failure exception. public LlmTransientException(string providerId, string message, int? statusCode = null, string? rawBody = null, Exception? inner = null) : base(message, inner) { ProviderId = providerId; StatusCode = statusCode; RawBody = rawBody; } } /// Which limit of a model call ran out. public enum LlmTimeoutKind { /// : the whole call, streamed or not. Call, /// : the provider sent nothing for too long. StreamIdle } /// /// A limit of the call to the model ran out (). /// covers the whole call, waiting for the response and reading it, streamed or not, whatever client sends it; /// covers a stream's silence. A cancellation by the caller is /// not this: it stays an . Retrying the same call with the same limit usually /// runs out again; a long generation needs a larger . /// public sealed class LlmTimeoutException : TimeoutException { /// Provider identifier. public string ProviderId { get; } /// Name of the connection factory whose limit ran out, when it has one. public string? FactoryName { get; } /// The limit that ran out. public TimeSpan Limit { get; } /// Which limit ran out. public LlmTimeoutKind Kind { get; } /// Creates a timeout exception. public LlmTimeoutException( string providerId, string? factoryName, TimeSpan limit, LlmTimeoutKind kind, string message, Exception? inner = null) : base(message, inner) { ProviderId = providerId; FactoryName = factoryName; Limit = limit; Kind = kind; } }