pub struct RateLimitConfig {
pub respect_retry_after: bool,
pub default_delay: Duration,
pub max_delay: Duration,
pub requests_per_minute: u32,
pub fallback_after_retries: u32,
pub max_retries: u32,
}Expand description
Policy for handling 429 / 503 rate-limit responses from LLM providers.
Governs backoff and retry behaviour when the server signals that the client
should slow down: a 429 Too Many Requests, a 503 Service Unavailable,
or a 529 Overloaded. All three can carry a Retry-After hint that this
config can honour.
Distinct from StreamRetryConfig, which covers generic
stream-initialization transport failures (the connection itself failed to
open).
§Example
use loopctl::stream::handler::RateLimitConfig;
use std::time::Duration;
let cfg = RateLimitConfig {
default_delay: Duration::from_secs(2),
fallback_after_retries: 2,
..Default::default()
};
assert!(cfg.validate().is_ok());Fields§
§respect_retry_after: boolWhether to honour a 429/503 Retry-After value when the server
provides one.
When true (the default), backoff returns the
server-advised delay (capped at max_delay). When
false, the server’s hint is ignored and
default_delay is always used.
default_delay: DurationBackoff used when the server gives no Retry-After.
Also used unconditionally when
respect_retry_after is false. Defaults
to 5s — long enough to let a transient burst clear, short enough that a
missing header doesn’t stall the agent.
max_delay: DurationUpper bound on any single rate-limit backoff.
Caps both the server’s Retry-After (when honoured) and
default_delay so a misbehaving provider cannot
stall the agent indefinitely.
requests_per_minute: u32Advisory per-minute request ceiling for proactive throttling (0 = unset).
This field is not read at runtime by the reactive rate-limit handler
— it does not throttle on its own. It is the value a caller feeds to
RateLimiter::new when
attaching a proactive limiter via
StreamHandler::with_rate_limiter.
Zero (the default) means no proactive throttling is configured; reactive handling
of server-returned 429/503 responses is unaffected and governed by the
other fields below.
fallback_after_retries: u32Number of rate-limit retries before switching to a fallback model.
After this many retries on the same model, the next rate limit triggers a fallback to a different model (if one is configured).
max_retries: u32Hard cap on rate-limit retries for a single turn.
Once this many retries have been exhausted, the turn fails outright.
Distinct from fallback_after_retries,
which controls the escalation threshold to a fallback model, not the
hard stop after which the turn gives up entirely.
Implementations§
Source§impl RateLimitConfig
impl RateLimitConfig
Sourcepub fn validate(&self) -> Result<(), String>
pub fn validate(&self) -> Result<(), String>
Validate the policy.
default_delay and max_delay must be non-zero, max_delay must be at
least default_delay, and max_retries must be at least 1.
§Errors
Returns a human-readable description of the first violated constraint.
use loopctl::stream::handler::RateLimitConfig;
use std::time::Duration;
assert!(RateLimitConfig::default().validate().is_ok());
let bad = RateLimitConfig { max_retries: 0, ..Default::default() };
assert!(bad.validate().is_err());Sourcepub fn backoff(&self, server_hint: Option<Duration>) -> Duration
pub fn backoff(&self, server_hint: Option<Duration>) -> Duration
Effective backoff for a detected rate limit given an optional server hint.
- If
server_hintisSome(d)andrespect_retry_afteristrue, returnsmin(d, max_delay). - Otherwise returns
min(default_delay, max_delay).
use loopctl::stream::handler::RateLimitConfig;
use std::time::Duration;
let cfg = RateLimitConfig::default();
assert_eq!(cfg.backoff(Some(Duration::from_secs(12))), Duration::from_secs(12));
assert_eq!(cfg.backoff(None), cfg.default_delay);Trait Implementations§
Source§impl Clone for RateLimitConfig
impl Clone for RateLimitConfig
Source§fn clone(&self) -> RateLimitConfig
fn clone(&self) -> RateLimitConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more