modelretry

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

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

View Source
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.

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

Sleep waits for d, or returns ctx's error when it ends first.

Types

type Classifier

type Classifier func(error) (Reason, bool)

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.

func (Config) Next

func (c Config) Next(retries int, err error, retryable Classifier) (Retry, bool)

Next decides whether a call that failed with err, after retries earlier retries, is made again. err is classified by retryable; a nil retryable means Retryable.

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

func RateLimited(err error) (Reason, bool)

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.

func Retryable

func Retryable(err error) (Reason, bool)

Retryable is the classifier of a model call that has no side effect beyond its answer. The order of the checks is part of the answer.

type Retry

type Retry struct {
	// Attempt is the one-based number of this retry, out of MaxAttempts.
	Attempt     int
	MaxAttempts int
	// Delay is the wait before the call is made again.
	Delay  time.Duration
	Reason Reason
}

Retry is the decision to make a failed call again.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL