Documentation
¶
Overview ¶
Package modelretry decides whether a failed model call is made again, and when. The agent loops and the single model calls (context compaction, memory, image generation, titles) share it, so a provider failure is retried by the same rule wherever the call is made.
Index ¶
Constants ¶
const MaxRetryAfter = time.Minute
MaxRetryAfter is the longest wait a provider's Retry-After is honored for. A longer one falls back to the backoff: the run does not stall for minutes on one call.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
func Do[T any]( ctx context.Context, logger *slog.Logger, operation string, cfg Config, retryable Classifier, call func(context.Context) (T, error), ) (T, error)
Do makes call, and makes it again after each failure that cfg.Next accepts. Every retried failure is recorded on logger as a WARN event for operation; the failure Do returns is left to the caller to record. A cancelled ctx ends the wait and returns its error.
Types ¶
type Classifier ¶
Classifier reports whether a failed model call is worth making again, and the class of the failure when it is.
type Config ¶
type Config struct {
MaxAttempts int // total retry attempts
FastAttempts int // first N attempts with no delay
BaseDelay time.Duration // backoff base for non-fast attempts
MaxDelay time.Duration // backoff cap
}
Config controls how often and how soon a failed model call is retried. A zero MaxAttempts means DefaultConfig.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default retry strategy: 5 attempts total. Only the first retry fires immediately — it absorbs network blips and instant upstream rejections. Later attempts back off 1s→8s with jitter, so a sustained overload window (typically tens of seconds) is ridden out without hammering the upstream with a burst of immediate retries.
type Reason ¶
type Reason string
Reason is the class of a failure that is retried. Its values are stable and reach clients on the retry event; the provider's own text never does.
const ( // ReasonRateLimited: the provider answered rate_limited. ReasonRateLimited Reason = "rate_limited" // ReasonServerError: the provider answered server_error (failed or // overloaded). ReasonServerError Reason = "server_error" // ReasonStreamIncomplete: the response was cut off before it completed. ReasonStreamIncomplete Reason = "stream_incomplete" // ReasonNetwork: the request or the response failed in transport. ReasonNetwork Reason = "network" )
func RateLimited ¶
RateLimited is the classifier of a model call whose result may cost or persist something even when the call fails. Only a rate-limit answer proves the provider did no work; a server error, a cut-off response or a transport failure may follow a result the provider already produced.