Overview
TheRetryConfig 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:
JSONDecodeErrorwhen the LLM returns invalid JSONValidationErrorwhen 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’sretry_config is used automatically when response_type is provided:
Retry Flow
When a retry occurs:- Error Detection: The agent catches
JSONDecodeErrororValidationError - Feedback Message: An error message is added to the thread explaining what went wrong
- Backoff Delay: The agent waits
backoff_base_seconds * attempt_numberseconds - Retry Attempt: A new LLM call is made with the feedback in context
- Repeat or Fail: Steps 1-4 repeat until success or
max_retriesis 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
- Agent - The main agent class
- StructuredOutputError - Exception raised when retries are exhausted
- Structured Output Guide - Complete guide to structured output