chatretry

package
v2.33.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

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.

Index

Constants

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

Variables

This section is empty.

Functions

func Delay

func Delay(attempt int) time.Duration

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.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable determines whether an error from an LLM provider is transient and worth retrying.

func Retry

func Retry(ctx context.Context, fn RetryFn, onRetry OnRetryFn) error

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.

Types

type ClassifiedError

type ClassifiedError = chaterror.ClassifiedError

type OnRetryFn

type OnRetryFn func(attempt int, err error, classified ClassifiedError, delay time.Duration)

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.

type RetryFn

type RetryFn func(ctx context.Context) error

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.

Jump to

Keyboard shortcuts

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