Skip to main content
LLM failures use provider-aware error classification with structured recovery routing, enabling intelligent retry policies, context compression, credential rotation, and model fallback strategies.
The user sends a prompt; structured errors route retries, compression, or model fallback.
LLM errors now also carry typed AgentErrorKind classifications for more precise error handling. See LLM Error Classification for the complete taxonomy and failover decision system.
When an Agent is configured with retry=..., retryable LLMErrors are automatically retried using jittered exponential backoff. See Agent Retry for the full API.

Quick Start

1

Simple Agent with Structured Errors

2

Advanced Error Classification


Error Categories

The new classifier recognizes seven distinct error categories with specific recovery actions:
Budget exhaustion (BudgetExceededError) now routes to permanent and surfaces immediately without retries. It was previously reachable via rate_limit, which burned retry attempts on a billing state that will never recover on its own.

How Errors Are Classified

The classifier picks a category using three signals, in order of precedence — stable signals first, message text last.
  1. HTTP status code — read from error.status_code, error.http_status, error.status, an integer error.code, or error.response.status_code.
  2. Exception class name — matched case-insensitively across the class hierarchy (MRO), so provider SDK subclasses are caught without importing those SDKs.
  3. Message regex — used only when steps 1 and 2 don’t match.
This makes classification reliable across providers whose error messages don’t include the expected keyword — a 429 rate limit whose message never says “rate” is still classified as rate_limit.

Status code mapping

Exception class mapping

Provider SDK exception classes are matched by class name across the MRO, so subclasses in provider SDKs are handled without importing those SDKs.

Ambiguous status codes (HTTP 400)

Some providers overload HTTP 400 — OpenAI returns context_length_exceeded as a 400, and some auth failures also come back as 400. The classifier refines a broad 400 using message signals before falling back to invalid_request.
Canonical examples live in the SDK tests: tests/unit/llm/test_error_classifier.py::TestTypeAndCodeClassification and TestStructuredRetryAfter.

LLMErrorClassification Structure

The new structured classification provides detailed recovery routing information:

Provider-Aware Backoff

Different providers have different rate limiting patterns, so the classifier uses provider-specific base delays: Backoff times include ±50% jitter to prevent thundering herd problems when multiple agents hit rate limits simultaneously.

Retry-After header

The classifier reads a provider’s Retry-After signal before falling back to message parsing, in this order:
  1. error.response.headers["retry-after"] (or Retry-After — case-insensitive)
  2. error.headers["retry-after"]
  3. error.retry_after attribute
  4. Regex over the message text
Numeric values are capped at 300 s. Non-numeric values (e.g. an HTTP-date Retry-After) fall through to the regex step, so existing behaviour is preserved.

Recovery Actions


Jittered Backoff

The retry system uses exponential backoff with ±50% jitter to avoid thundering herd problems:

Advanced Classification Usage

For users who want direct access to the classifier:

How It Works

Async Support

Structured error classification works with both sync and async agents:

Typed Error Classification (AgentErrorKind)

LLM errors are automatically classified into typed AgentErrorKind categories for precise handling. For the complete system, see LLM Error Classification.

Quick Reference

  • Retryable: rate_limit, overloaded, idle_timeout, auth
  • Non-retryable: auth_permanent, model_not_found, format_error, context_overflow, billing
  • Limited retry: unknown, empty_response

Legacy Support

The simple two-bucket classification (retryable/non-retryable) remains available for backward compatibility, but typed categories provide much more control.

LLMError Structure

The LLMError class provides structured error information:

Error Context

The on_error handler receives enhanced context information:

Retry Depth Limits

The recursive recovery loop (after context compression and after transient-error backoff) is bounded by the agent’s configured retry policy:
  • With retry= set: the loop honours RetryBackoffConfig.max_retries (e.g. retry=True → 3, max_retries=0 → recursion disabled).
  • Without retry= set: the loop falls back to 2, preserving prior behaviour.
  • Context compression: triggered on the context_overflow category.
This bound is read from Agent._max_retry_depth(), which returns RetryBackoffConfig.max_retries when a retry config is present and 2 otherwise. See Agent Retry for how to configure it.

Unimplemented Recovery Actions

Some recovery actions are planned but not yet implemented in the core system:
Credential Rotation: When should_rotate_credential=True, the user message includes “Credential rotation is not yet implemented”. Users must handle credential management manually.Model Fallback: When should_fallback_model=True, the user message includes “Model fallback is not yet implemented”. Users can implement custom fallback logic in their on_error handlers.

Migration from Binary Classification

If you were previously checking error.is_retryable only, you can now access richer classification: Before (binary classification):
After (structured classification):
Typed Error Categories (New):

Best Practices

Use the structured error_category field instead of pattern matching on error messages:
Track error patterns using the new categorical data:
Build on the structured classification for advanced recovery:
Customize handling based on the LLM provider:

LLM Error Classification

Typed error categories and failover decisions

Task Retry Policy

Configure task-level retry behavior and policies

Hooks

Agent lifecycle hooks and events

Model Failover

Cross-provider failover with FailoverManager