modelerrors

package
v1.31.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package modelerrors provides error classification utilities for LLM model providers. It determines whether errors are retryable, identifies context window overflow conditions, extracts HTTP status codes from various SDK error types, and computes exponential backoff durations.

Index

Constants

View Source
const (
	// DefaultRetries is the default number of retries per model with exponential
	// backoff for retryable errors (5xx, timeouts). 2 retries means 3 total attempts.
	// This handles transient provider issues without immediately failing over.
	DefaultRetries = 2

	// DefaultCooldown is the default duration to stick with a fallback model
	// after a non-retryable error before retrying the primary.
	DefaultCooldown = 1 * time.Minute
)

Default fallback configuration.

Variables

This section is empty.

Functions

func CalculateBackoff

func CalculateBackoff(attempt int) time.Duration

CalculateBackoff returns the backoff duration for a given attempt (0-indexed). Uses exponential backoff with jitter.

func ExtractHTTPStatusCode

func ExtractHTTPStatusCode(err error) int

ExtractHTTPStatusCode attempts to extract an HTTP status code from the error. Checks in order: 1. Known provider SDK error types (Anthropic, Gemini) 2. Regex parsing of error message (fallback for OpenAI and others) Returns 0 if no status code found.

func FormatError

func FormatError(err error) string

FormatError returns a user-friendly error message for model errors. Context overflow gets a dedicated actionable message; all other errors pass through their original message.

func IsContextOverflowError

func IsContextOverflowError(err error) bool

IsContextOverflowError checks whether the error indicates the conversation context has exceeded the model's context window. It inspects both structured SDK error types and raw error message patterns.

Recognised patterns include:

  • Anthropic 400 "prompt is too long: N tokens > M maximum"
  • Anthropic 400 "max_tokens must be greater than thinking.budget_tokens" (emitted when the prompt is so large that max_tokens can't accommodate the thinking budget — a proxy for context overflow)
  • OpenAI 400 "maximum context length" / "context_length_exceeded"
  • Anthropic 500 that is actually a context overflow (heuristic: the error message is opaque but the conversation was already near the limit)

This function intentionally does NOT match generic 500 errors; callers that want to treat an opaque 500 as overflow must check separately with additional context (e.g., session token counts).

func IsRetryableModelError

func IsRetryableModelError(err error) bool

IsRetryableModelError determines if an error should trigger a retry of the SAME model.

Retryable errors (retry same model with backoff): - Network timeouts - Temporary network errors - HTTP 5xx errors (server errors) - HTTP 529 (Anthropic overloaded) - HTTP 408 (request timeout)

Non-retryable errors (skip to next model in chain immediately): - Context cancellation - HTTP 429 (rate limit) - provider is explicitly rate limiting us - HTTP 4xx errors (client errors) - Authentication errors - Invalid request errors

The key distinction is: 429 means "you're calling too fast, slow down" which suggests we should try a different model, not keep hammering the same one.

func IsRetryableStatusCode

func IsRetryableStatusCode(statusCode int) bool

IsRetryableStatusCode determines if an HTTP status code is retryable. Retryable means we should retry the SAME model with exponential backoff.

Retryable status codes: - 5xx (server errors): 500, 502, 503, 504 - 529 (Anthropic overloaded) - 408 (request timeout)

Non-retryable status codes (skip to next model immediately): - 429 (rate limit) - provider is explicitly telling us to back off - 4xx client errors (400, 401, 403, 404) - won't get better with retry

func SleepWithContext

func SleepWithContext(ctx context.Context, d time.Duration) bool

SleepWithContext sleeps for the specified duration, returning early if context is cancelled. Returns true if the sleep completed, false if it was interrupted by context cancellation.

Types

type ContextOverflowError

type ContextOverflowError struct {
	Underlying error
}

ContextOverflowError wraps an underlying error to indicate that the failure was caused by the conversation context exceeding the model's context window. This is used to trigger auto-compaction in the runtime loop instead of surfacing raw HTTP errors to the user.

func (*ContextOverflowError) Error

func (e *ContextOverflowError) Error() string

func (*ContextOverflowError) Unwrap

func (e *ContextOverflowError) Unwrap() error

Jump to

Keyboard shortcuts

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