Package chatretry provides retry logic for transient LLM provider
errors. It classifies errors as retryable or permanent and uses
exponential backoff with provider retry hints when available.
const (
// InitialDelay is the backoff duration for the first retry// attempt.
InitialDelay = 1 * time.Second// MaxDelay is the upper bound for the exponential backoff// duration. Matches the cap used in coder/mux.
MaxDelay = 60 * time.Second// MaxAttempts is the upper bound on retry attempts before// giving up. With a 60s max backoff this allows roughly// 25 minutes of retries, which is reasonable for transient// LLM provider issues.
MaxAttempts = 25
)
Delay returns the backoff duration for the given 0-indexed attempt.
Uses exponential backoff: min(InitialDelay * 2^attempt, MaxDelay).
Matches the backoff curve used in coder/mux.
Retry calls fn repeatedly until it succeeds, returns a
non-retryable error, ctx is canceled, or MaxAttempts is reached.
Retries use exponential backoff capped at MaxDelay, unless the
normalized error includes a longer provider Retry-After hint.
The onRetry callback (if non-nil) is called before each retry
attempt, giving the caller a chance to reset state, log, or
publish status events.
OnRetryFn is called before each retry attempt with the attempt
number (1-indexed), the raw error that triggered the retry, the
normalized error payload, and the delay before the next attempt.
RetryFn is the function to retry. It receives a context and returns
an error. The context may be a child of the original with adjusted
deadlines for individual attempts.