Skip to main content

Overview

The RetryConfig class configures automatic retry behavior for agent operations, particularly useful when using structured output that may fail validation.

Class Definition

Properties

int
default:"3"
Maximum number of retry attempts for a failed operation. Set to 0 to disable retries entirely.
  • Minimum value: 0
  • Each retry includes the error message as feedback to the LLM
bool
default:"True"
When True, the agent will automatically retry if structured output validation fails.This handles:
  • JSONDecodeError when the LLM returns invalid JSON
  • ValidationError when JSON doesn’t match the Pydantic schema
bool
default:"False"
When True, the agent will retry if a tool execution fails with an exception.Use with caution—some tool errors are not recoverable by retry (e.g., authentication failures).
float
default:"1.0"
Base delay in seconds for exponential backoff between retries.The actual delay is: backoff_base_seconds * retry_attempt
  • Retry 1: 1.0s delay
  • Retry 2: 2.0s delay
  • Retry 3: 3.0s delay

Creating RetryConfig

Usage with Agent

At Agent Creation

At Runtime

The agent’s retry_config is used automatically when response_type is provided:

Retry Flow

When a retry occurs:
  1. Error Detection: The agent catches JSONDecodeError or ValidationError
  2. Feedback Message: An error message is added to the thread explaining what went wrong
  3. Backoff Delay: The agent waits backoff_base_seconds * attempt_number seconds
  4. Retry Attempt: A new LLM call is made with the feedback in context
  5. Repeat or Fail: Steps 1-4 repeat until success or max_retries is exhausted

Error Handling

When all retries are exhausted, StructuredOutputError is raised:

Immutability

RetryConfig instances are immutable (frozen). Create a new instance to change values:

Best Practices

Choosing max_retries

When to Enable tool_error Retry

Production Configuration

See Also