Skip to main content

RateLimitConfig

Struct RateLimitConfig 

Source
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: bool

Whether 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: Duration

Backoff 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: Duration

Upper 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: u32

Advisory 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: u32

Number 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: u32

Hard 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

Source

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());
Source

pub fn backoff(&self, server_hint: Option<Duration>) -> Duration

Effective backoff for a detected rate limit given an optional server hint.

  • If server_hint is Some(d) and respect_retry_after is true, returns min(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

Source§

fn clone(&self) -> RateLimitConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RateLimitConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RateLimitConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more