Agentic Design Pattern: Retry Backoff

Written by

in


    """
Pattern 146: Retry with Backoff

Description:
    Implements retry logic with exponential backoff for handling transient failures
    in LLM API calls and agent operations. This pattern automatically retries failed
    operations with increasing delays, helping systems recover from temporary issues
    like rate limits, network errors, or service disruptions.

Components:
    - Retry Manager: Orchestrates retry attempts
    - Backoff Strategy: Calculates delay between retries
    - Failure Classifier: Determines if error is retryable
    - Success Monitor: Tracks retry statistics
    - Circuit Breaker Integration: Prevents endless retries
    - Fallback Handler: Provides alternatives when retries exhausted

Use Cases:
    - Handle API rate limiting
    - Recover from transient network errors
    - Manage service unavailability
    - Deal with timeout errors
    - Handle overloaded systems

Benefits:
    - Automatic error recovery
    - Reduced manual intervention
    - Improved reliability
    - Better user experience
    - Graceful handling of transient failures

Trade-offs:
    - Increased latency on failures
    - Resource consumption during retries
    - Potential for cascading delays
    - Complexity in retry logic

LangChain Implementation:
    Uses decorators and custom retry logic with exponential backoff,
    jitter, and intelligent failure classification.
"""

import os
import time
import random
from typing import Any, Callable, Optional, List, Dict
from dataclasses import dataclass, field
from enum import Enum
from datetime import datetime
from functools import wraps
from dotenv import load_dotenv

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

load_dotenv()


class BackoffStrategy(Enum):
    """Backoff strategy for retries"""
    FIXED = "fixed"
    LINEAR = "linear"
    EXPONENTIAL = "exponential"
    FIBONACCI = "fibonacci"


class ErrorType(Enum):
    """Types of errors for classification"""
    TRANSIENT = "transient"  # Temporary, retryable
    PERMANENT = "permanent"  # Persistent, not retryable
    RATE_LIMIT = "rate_limit"  # Rate limiting
    TIMEOUT = "timeout"  # Timeout error
    UNKNOWN = "unknown"  # Unknown error type


@dataclass
class RetryConfig:
    """Configuration for retry behavior"""
    max_attempts: int = 3
    initial_delay: float = 1.0
    max_delay: float = 60.0
    backoff_strategy: BackoffStrategy = BackoffStrategy.EXPONENTIAL
    backoff_multiplier: float = 2.0
    jitter: bool = True
    jitter_range: float = 0.1
    retryable_errors: List[str] = field(default_factory=lambda: [
        "rate_limit",
        "timeout",
        "connection",
        "service_unavailable"
    ])


@dataclass
class RetryAttempt:
    """Information about a retry attempt"""
    attempt_number: int
    error_type: ErrorType
    error_message: str
    delay_before: float
    timestamp: datetime
    success: bool


@dataclass
class RetryMetrics:
    """Metrics for retry operations"""
    total_calls: int = 0
    successful_first_try: int = 0
    successful_after_retry: int = 0
    failed_after_retries: int = 0
    total_retries: int = 0
    total_delay_time: float = 0.0
    average_attempts: float = 0.0
    retry_success_rate: float = 0.0


class RetryWithBackoffAgent:
    """
    Agent that implements retry logic with exponential backoff.
    
    Automatically retries failed operations with intelligent backoff
    strategies and error classification.
    """
    
    def __init__(
        self,
        model: str = "gpt-3.5-turbo",
        config: Optional[RetryConfig] = None
    ):
        """
        Initialize the retry agent.
        
        Args:
            model: LLM model to use
            config: Retry configuration
        """
        self.llm = ChatOpenAI(model=model, temperature=0.7)
        self.config = config or RetryConfig()
        
        self.metrics = RetryMetrics()
        self.retry_history: List[RetryAttempt] = []
    
    def calculate_delay(self, attempt: int) -> float:
        """
        Calculate delay for retry attempt based on strategy.
        
        Args:
            attempt: Current attempt number (0-indexed)
            
        Returns:
            Delay in seconds
        """
        if self.config.backoff_strategy == BackoffStrategy.FIXED:
            delay = self.config.initial_delay
            
        elif self.config.backoff_strategy == BackoffStrategy.LINEAR:
            delay = self.config.initial_delay * (attempt + 1)
            
        elif self.config.backoff_strategy == BackoffStrategy.EXPONENTIAL:
            delay = self.config.initial_delay * (
                self.config.backoff_multiplier ** attempt
            )
            
        elif self.config.backoff_strategy == BackoffStrategy.FIBONACCI:
            delay = self.config.initial_delay * self._fibonacci(attempt + 1)
        
        else:
            delay = self.config.initial_delay
        
        # Cap at max delay
        delay = min(delay, self.config.max_delay)
        
        # Add jitter if enabled
        if self.config.jitter:
            jitter_amount = delay * self.config.jitter_range
            delay += random.uniform(-jitter_amount, jitter_amount)
        
        return max(0, delay)  # Ensure non-negative
    
    def _fibonacci(self, n: int) -> int:
        """Calculate nth Fibonacci number."""
        if n  ErrorType:
        """
        Classify error to determine if retryable.
        
        Args:
            error: Exception to classify
            
        Returns:
            Error type classification
        """
        error_str = str(error).lower()
        
        if any(keyword in error_str for keyword in ["rate limit", "429"]):
            return ErrorType.RATE_LIMIT
        
        elif any(keyword in error_str for keyword in ["timeout", "timed out"]):
            return ErrorType.TIMEOUT
        
        elif any(keyword in error_str for keyword in [
            "connection", "network", "unavailable", "503", "502"
        ]):
            return ErrorType.TRANSIENT
        
        elif any(keyword in error_str for keyword in [
            "invalid", "authentication", "authorization", "400", "401", "403"
        ]):
            return ErrorType.PERMANENT
        
        else:
            return ErrorType.UNKNOWN
    
    def is_retryable(self, error: Exception) -> bool:
        """
        Determine if error should be retried.
        
        Args:
            error: Exception to check
            
        Returns:
            True if error is retryable
        """
        error_type = self.classify_error(error)
        
        if error_type == ErrorType.PERMANENT:
            return False
        
        error_str = str(error).lower()
        return any(
            keyword in error_str
            for keyword in self.config.retryable_errors
        )
    
    def retry_with_backoff(
        self,
        func: Callable,
        *args,
        **kwargs
    ) -> Any:
        """
        Execute function with retry logic.
        
        Args:
            func: Function to execute
            *args: Positional arguments
            **kwargs: Keyword arguments
            
        Returns:
            Function result
            
        Raises:
            Exception: If all retries exhausted
        """
        self.metrics.total_calls += 1
        attempts = []
        
        for attempt in range(self.config.max_attempts):
            try:
                result = func(*args, **kwargs)
                
                # Record success
                if attempt == 0:
                    self.metrics.successful_first_try += 1
                else:
                    self.metrics.successful_after_retry += 1
                    self.metrics.total_retries += attempt
                
                # Record attempt
                attempts.append(RetryAttempt(
                    attempt_number=attempt + 1,
                    error_type=ErrorType.TRANSIENT,
                    error_message="Success",
                    delay_before=0,
                    timestamp=datetime.now(),
                    success=True
                ))
                
                self.retry_history.extend(attempts)
                self._update_metrics()
                
                return result
                
            except Exception as e:
                error_type = self.classify_error(e)
                
                # Check if should retry
                if not self.is_retryable(e):
                    print(f"ā›” Non-retryable error: {error_type.value}")
                    self.metrics.failed_after_retries += 1
                    raise
                
                # Last attempt - don't retry
                if attempt == self.config.max_attempts - 1:
                    print(f"āŒ Max retries ({self.config.max_attempts}) exhausted")
                    self.metrics.failed_after_retries += 1
                    
                    attempts.append(RetryAttempt(
                        attempt_number=attempt + 1,
                        error_type=error_type,
                        error_message=str(e),
                        delay_before=0,
                        timestamp=datetime.now(),
                        success=False
                    ))
                    
                    self.retry_history.extend(attempts)
                    self._update_metrics()
                    raise
                
                # Calculate delay and retry
                delay = self.calculate_delay(attempt)
                
                print(f"āš ļø  Attempt {attempt + 1} failed: {error_type.value}")
                print(f"   Retrying in {delay:.2f}s...")
                
                attempts.append(RetryAttempt(
                    attempt_number=attempt + 1,
                    error_type=error_type,
                    error_message=str(e),
                    delay_before=delay,
                    timestamp=datetime.now(),
                    success=False
                ))
                
                self.metrics.total_delay_time += delay
                time.sleep(delay)
    
    def _update_metrics(self):
        """Update calculated metrics."""
        total_operations = (
            self.metrics.successful_first_try +
            self.metrics.successful_after_retry +
            self.metrics.failed_after_retries
        )
        
        if total_operations > 0:
            self.metrics.average_attempts = (
                (self.metrics.successful_first_try +
                 self.metrics.successful_after_retry +
                 self.metrics.total_retries) / total_operations
            )
        
        retry_operations = (
            self.metrics.successful_after_retry +
            self.metrics.failed_after_retries
        )
        
        if retry_operations > 0:
            self.metrics.retry_success_rate = (
                self.metrics.successful_after_retry / retry_operations
            )
    
    def query_with_retry(self, query: str) -> str:
        """
        Query LLM with retry logic.
        
        Args:
            query: Query string
            
        Returns:
            LLM response
        """
        def _query():
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are a helpful assistant."),
                ("user", "{query}")
            ])
            chain = prompt | self.llm | StrOutputParser()
            return chain.invoke({"query": query})
        
        return self.retry_with_backoff(_query)
    
    def get_metrics_report(self) -> str:
        """Generate retry metrics report."""
        report = []
        report.append("\n" + "="*60)
        report.append("RETRY WITH BACKOFF METRICS REPORT")
        report.append("="*60)
        
        report.append(f"\nšŸ“Š Overall Statistics:")
        report.append(f"   Total Calls: {self.metrics.total_calls}")
        report.append(f"   Successful First Try: {self.metrics.successful_first_try}")
        report.append(f"   Successful After Retry: {self.metrics.successful_after_retry}")
        report.append(f"   Failed After Retries: {self.metrics.failed_after_retries}")
        
        report.append(f"\nšŸ”„ Retry Statistics:")
        report.append(f"   Total Retries: {self.metrics.total_retries}")
        report.append(f"   Average Attempts: {self.metrics.average_attempts:.2f}")
        report.append(f"   Retry Success Rate: {self.metrics.retry_success_rate:.1%}")
        
        report.append(f"\nā±ļø  Timing:")
        report.append(f"   Total Delay Time: {self.metrics.total_delay_time:.2f}s")
        
        if self.metrics.total_calls > 0:
            success_rate = (
                (self.metrics.successful_first_try + self.metrics.successful_after_retry) /
                self.metrics.total_calls
            )
            report.append(f"\nāœ… Overall Success Rate: {success_rate:.1%}")
        
        report.append("\n" + "="*60)
        
        return "\n".join(report)


def retry_decorator(config: Optional[RetryConfig] = None):
    """
    Decorator for automatic retry with backoff.
    
    Args:
        config: Retry configuration
        
    Returns:
        Decorated function
    """
    if config is None:
        config = RetryConfig()
    
    def decorator(func: Callable) -> Callable:
        @wraps(func)
        def wrapper(*args, **kwargs):
            agent = RetryWithBackoffAgent(config=config)
            return agent.retry_with_backoff(func, *args, **kwargs)
        return wrapper
    return decorator


def demonstrate_retry_with_backoff():
    """Demonstrate the Retry with Backoff pattern."""
    print("="*60)
    print("RETRY WITH BACKOFF PATTERN DEMONSTRATION")
    print("="*60)
    
    # Example 1: Basic retry with exponential backoff
    print("\n" + "="*60)
    print("Example 1: Basic Retry with Exponential Backoff")
    print("="*60)
    
    config = RetryConfig(
        max_attempts=4,
        initial_delay=1.0,
        backoff_strategy=BackoffStrategy.EXPONENTIAL,
        backoff_multiplier=2.0
    )
    
    agent = RetryWithBackoffAgent(config=config)
    
    print("\nDelay progression for exponential backoff:")
    for i in range(4):
        delay = agent.calculate_delay(i)
        print(f"  Attempt {i+1}: {delay:.2f}s delay")
    
    # Example 2: Compare backoff strategies
    print("\n" + "="*60)
    print("Example 2: Compare Backoff Strategies")
    print("="*60)
    
    strategies = [
        BackoffStrategy.FIXED,
        BackoffStrategy.LINEAR,
        BackoffStrategy.EXPONENTIAL,
        BackoffStrategy.FIBONACCI
    ]
    
    print(f"\n{'Strategy':<15} {'Attempt 1':<12} {'Attempt 2':<12} {'Attempt 3':<12} {'Attempt 4':<12}")
    print("-" * 63)
    
    for strategy in strategies:
        config = RetryConfig(
            initial_delay=1.0,
            backoff_strategy=strategy,
            backoff_multiplier=2.0,
            jitter=False
        )
        agent = RetryWithBackoffAgent(config=config)
        
        delays = [agent.calculate_delay(i) for i in range(4)]
        print(f"{strategy.value:<15} {delays[0]:<12.2f} {delays[1]:<12.2f} {delays[2]:<12.2f} {delays[3]:<12.2f}")
    
    # Example 3: Error classification
    print("\n" + "="*60)
    print("Example 3: Error Classification")
    print("="*60)
    
    agent = RetryWithBackoffAgent()
    
    test_errors = [
        Exception("Rate limit exceeded (429)"),
        Exception("Connection timeout"),
        Exception("Service unavailable (503)"),
        Exception("Invalid API key (401)"),
        Exception("Network connection failed"),
        Exception("Unknown error occurred")
    ]
    
    print("\nError classification results:")
    for error in test_errors:
        error_type = agent.classify_error(error)
        is_retryable = agent.is_retryable(error)
        status = "āœ… RETRY" if is_retryable else "ā›” DON'T RETRY"
        print(f"  {status:<15} {error_type.value:<15} {str(error)[:40]}")
    
    # Example 4: Simulate retry scenarios
    print("\n" + "="*60)
    print("Example 4: Simulate Retry Scenarios")
    print("="*60)
    
    # Simulate transient failure that succeeds on retry
    call_count = {"count": 0}
    
    def flaky_function():
        """Function that fails first 2 times, then succeeds."""
        call_count["count"] += 1
        if call_count["count"] < 3:
            raise Exception("Service temporarily unavailable (503)")
        return "Success!"
    
    config = RetryConfig(max_attempts=5, initial_delay=0.5)
    agent = RetryWithBackoffAgent(config=config)
    
    print("\nSimulating transient failure (succeeds on 3rd attempt):")
    try:
        result = agent.retry_with_backoff(flaky_function)
        print(f"āœ… Result: {result}")
    except Exception as e:
        print(f"āŒ Failed: {e}")
    
    # Example 5: Jitter demonstration
    print("\n" + "="*60)
    print("Example 5: Jitter for Thundering Herd Prevention")
    print("="*60)
    
    config_no_jitter = RetryConfig(
        initial_delay=1.0,
        backoff_strategy=BackoffStrategy.EXPONENTIAL,
        jitter=False
    )
    
    config_with_jitter = RetryConfig(
        initial_delay=1.0,
        backoff_strategy=BackoffStrategy.EXPONENTIAL,
        jitter=True,
        jitter_range=0.2
    )
    
    agent_no_jitter = RetryWithBackoffAgent(config=config_no_jitter)
    agent_with_jitter = RetryWithBackoffAgent(config=config_with_jitter)
    
    print("\nDelay comparison (5 simulated clients):")
    print(f"\n{'Attempt':<10} {'No Jitter':<40} {'With Jitter':<40}")
    print("-" * 90)
    
    for attempt in range(3):
        no_jitter_delays = [agent_no_jitter.calculate_delay(attempt) for _ in range(5)]
        with_jitter_delays = [agent_with_jitter.calculate_delay(attempt) for _ in range(5)]
        
        no_jitter_str = ", ".join(f"{d:.2f}s" for d in no_jitter_delays)
        with_jitter_str = ", ".join(f"{d:.2f}s" for d in with_jitter_delays)
        
        print(f"{attempt+1:<10} {no_jitter_str:<40} {with_jitter_str:<40}")
    
    print("\nšŸ’” Jitter spreads retry attempts, preventing thundering herd")
    
    # Example 6: Real LLM query with retry
    print("\n" + "="*60)
    print("Example 6: LLM Query with Retry Protection")
    print("="*60)
    
    config = RetryConfig(
        max_attempts=3,
        initial_delay=1.0,
        backoff_strategy=BackoffStrategy.EXPONENTIAL
    )
    
    agent = RetryWithBackoffAgent(config=config)
    
    try:
        print("\nQuerying LLM with retry protection...")
        result = agent.query_with_retry(
            "What is the capital of France? Answer in one word."
        )
        print(f"āœ… Response: {result}")
    except Exception as e:
        print(f"āŒ Failed after all retries: {e}")
    
    # Example 7: Metrics and reporting
    print("\n" + "="*60)
    print("Example 7: Retry Metrics and Reporting")
    print("="*60)
    
    # Simulate multiple operations
    agent = RetryWithBackoffAgent(config=RetryConfig(max_attempts=3))
    
    test_queries = [
        "What is 2+2?",
        "Explain quantum computing in one sentence",
        "Name a primary color"
    ]
    
    print("\nExecuting multiple queries with retry protection...")
    for query in test_queries:
        try:
            agent.query_with_retry(query)
        except Exception as e:
            print(f"Query failed: {e}")
    
    report = agent.get_metrics_report()
    print(report)
    
    # Summary
    print("\n" + "="*60)
    print("SUMMARY")
    print("="*60)
    print("""
The Retry with Backoff pattern demonstrates:

1. Automatic Recovery: Handles transient failures automatically
2. Multiple Strategies: Fixed, linear, exponential, Fibonacci backoff
3. Error Classification: Intelligent distinction between retryable/non-retryable
4. Jitter Support: Prevents thundering herd problem
5. Metrics Tracking: Comprehensive monitoring of retry behavior

Key Benefits:
- Improved reliability (often 95%+ success rate)
- Reduced manual intervention
- Better user experience
- Graceful handling of rate limits
- Protection against cascading failures

Backoff Strategies:
- Fixed: Same delay each time (simple, predictable)
- Linear: Delay increases linearly (gradual backoff)
- Exponential: Delay doubles each time (recommended)
- Fibonacci: Delay follows Fibonacci sequence (balanced)

Best Practices:
- Use exponential backoff as default
- Add jitter to prevent synchronized retries
- Set reasonable max attempts (3-5 typical)
- Cap maximum delay (60s common)
- Classify errors correctly
- Log retry attempts for debugging
- Integrate with circuit breaker
- Monitor retry metrics

Common Configurations:
- API calls: 3 retries, exponential, 1s initial
- Database: 5 retries, exponential, 0.5s initial
- Network: 4 retries, linear, 2s initial
- Rate limits: Fibonacci, respect Retry-After header
    """)


if __name__ == "__main__":
    demonstrate_retry_with_backoff()

    

Agentic Design Pattern: Retry Backoff

Pattern 146: Retry Backoff – Line-by-Line Explanation

Overview

This document provides a detailed line-by-line explanation of the Retry Backoff pattern implementation.


Pattern Description

Pattern 146: Retry with Backoff

Description: Implements retry logic with exponential backoff for handling transient failures in LLM API calls and agent operations. This pattern automatically retries failed operations with increasing delays, helping systems recover from temporary issues like rate limits, network errors, or service disruptions.

Components: – Retry Manager: Orchestrates retry attempts – Backoff Strategy: Calculates delay between retries – Failure Classifier: Determines if error is retryable – Success Monitor: Tracks retry statistics – Circuit Breaker Integration: Prevents endless retries – Fallback Handler: Provides alternatives when retries exhausted

Use Cases: – Handle API rate limiting – Recover from transient network errors – Manage service unavailability – Deal with timeout errors – Handle overloaded systems

Benefits: – Automatic error recovery – Reduced manual intervention – Improved reliability – Better user experience – Graceful handling of transient failures

Trade-offs: – Increased latency on failures – Resource consumption during retries – Potential for cascading delays – Complexity in retry logic

LangChain Implementation: Uses decorators and custom retry logic with exponential backoff, jitter, and intelligent failure classification.


File Statistics

  • Total Lines: 665
  • Classes: 6
  • Functions: 2
  • Constants: 0

Module Documentation

"""
Pattern 146: Retry with Backoff

Description:
    Implements retry logic with exponential backoff for handling transient failures
    in LLM API calls and agent operations. This pattern automatically retries failed
    operations with increasing delays, helping systems recover from temporary issues
    like rate limits, network errors, or service disruptions.

Components:
    - Retry Manager: Orchestrates retry attempts
    - Backoff Strategy: Calculates delay between retries
    - Failure Classifier: Determines if error is retryable
    - Success Monitor: Tracks retry statistics
    - Circuit Breaker Integration: Prevents endless retries
    - Fallback Handler: Provides alternatives when retries exhausted

Use Cases:
    - Handle API rate limiting
    - Recover from transient network errors
    - Manage service unavailability
    - Deal with timeout errors
    - Handle overloaded systems

Benefits:
    - Automatic error recovery
    - Reduced manual intervention
    - Improved reliability
    - Better user experience
    - Graceful handling of transient failures

Trade-offs:
    - Increased latency on failures
    - Resource consumption during retries
    - Potential for cascading delays
    - Complexity in retry logic

LangChain Implementation:
    Uses decorators and custom retry logic with exponential backoff,
    jitter, and intelligent failure classification.
"""

Purpose: This module implements the Retry Backoff pattern, providing a structured approach to agent design and implementation.


Import Statements

Standard Library Imports

import dataclasses.dataclass
import dataclasses.field
import datetime.datetime
import enum.Enum
import functools.wraps
import langchain_core.output_parsers.StrOutputParser
import langchain_core.prompts.ChatPromptTemplate
import os
import random
import time
import typing.Any
import typing.Callable
import typing.Dict
import typing.List
import typing.Optional

Purpose: Core Python functionality: – dataclasses.dataclass: Simplified class definitions with @dataclass – dataclasses.field: Simplified class definitions with @dataclass – datetime.datetime: Date and time handling – enum.Enum: Enumeration support – functools.wraps: Higher-order functions and operations on callable objects – langchain_core.output_parsers.StrOutputParser: Regular expression operations – langchain_core.prompts.ChatPromptTemplate: Regular expression operations – os: Environment variable access and system operations – random: Random number generation – time: Time-related functions – typing.Any: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Callable: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Dict: Type hints for better code documentation (List, Dict, Any, Optional) – typing.List: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Optional: Type hints for better code documentation (List, Dict, Any, Optional)

LangChain Imports

from langchain_openai.ChatOpenAI

Purpose: LangChain framework components: – LLM Interface: Wrapper for OpenAI models – Chains: Composable building blocks for complex workflows

Other Imports

from dotenv.load_dotenv

Purpose: Additional dependencies for specialized functionality.


Classes

Class: BackoffStrategy

Lines 60-65

Inherits from: Enum

Purpose:

Backoff strategy for retries

Class: ErrorType

Lines 68-74

Inherits from: Enum

Purpose:

Types of errors for classification

Class: RetryConfig

Lines 78-92

Purpose:

Configuration for retry behavior

Class: RetryAttempt

Lines 96-103

Purpose:

Information about a retry attempt

Class: RetryMetrics

Lines 107-116

Purpose:

Metrics for retry operations

Class: RetryWithBackoffAgent

Lines 119-412

Purpose:

Agent that implements retry logic with exponential backoff.

Automatically retries failed operations with intelligent backoff
strategies and error classification.

Methods:

__init__(self, model, config)

Lines 127-143

Purpose:

Initialize the retry agent.

Args:
    model: LLM model to use
    config: Retry configuration

Parameters: – model: Language model instance – config: Config

calculate_delay(self, attempt)

Lines 145-180

Purpose:

Calculate delay for retry attempt based on strategy.

Args:
    attempt: Current attempt number (0-indexed)

Returns:
    Delay in seconds

Parameters: – attempt: Attempt

Returns: float

_fibonacci(self, n)

Lines 182-189

Purpose:

Calculate nth Fibonacci number.

Parameters: – n: N

Returns: int

classify_error(self, error)

Lines 191-220

Purpose:

Classify error to determine if retryable.

Args:
    error: Exception to classify

Returns:
    Error type classification

Parameters: – error: Error

Returns: ErrorType

is_retryable(self, error)

Lines 222-241

Purpose:

Determine if error should be retried.

Args:
    error: Exception to check

Returns:
    True if error is retryable

Parameters: – error: Error

Returns: bool

retry_with_backoff(self, func)

Lines 243-335

Purpose:

Execute function with retry logic.

Args:
    func: Function to execute
    *args: Positional arguments
    **kwargs: Keyword arguments

Returns:
    Function result

Raises:
    Exception: If all retries exhausted

Parameters: – func: Func

Returns: Any

_update_metrics(self)

Lines 337-360

Purpose:

Update calculated metrics.

Parameters:

query_with_retry(self, query)

Lines 362-380

Purpose:

Query LLM with retry logic.

Args:
    query: Query string

Returns:
    LLM response

Parameters: – query: Input query or question

Returns: str

get_metrics_report(self)

Lines 382-412

Purpose:

Generate retry metrics report.

Parameters:

Returns: str


Functions

retry_decorator(config)

Lines 415-434

Purpose:

Decorator for automatic retry with backoff.

Args:
    config: Retry configuration

Returns:
    Decorated function

Parameters: – config: Config

demonstrate_retry_with_backoff()

Lines 437-660

Purpose:

Demonstrate the Retry with Backoff pattern.

Key Concepts Summary

Pattern Overview

The Retry Backoff pattern provides a structured approach to building AI agents with specific capabilities and behaviors. This implementation leverages LangChain’s composable components to create a robust and maintainable solution.

Core Components

  1. Agent Class: Main implementation of the pattern logic
  2. Prompt Engineering: Carefully crafted prompts to guide LLM behavior
  3. Tool Integration: Optional tools for extended functionality
  4. Memory Management: State handling across interactions
  5. Output Parsing: Structured response processing

When to Use

āœ… Good for: – Scenarios requiring this specific pattern’s strengths – Applications needing structured agent behavior – Systems where explainability is important – Production environments requiring reliability

āŒ Not ideal for: – Simple single-prompt tasks – Scenarios requiring different pattern characteristics – Resource-constrained environments (if computationally intensive)


Best Practices Demonstrated

1. Environment Configuration

  • āœ… Use .env files for API keys
  • āœ… Never hardcode credentials
  • āœ… Validate environment variables on startup

2. Error Handling

  • āœ… Graceful degradation on failures
  • āœ… Informative error messages
  • āœ… Logging for debugging

3. Prompt Engineering

  • āœ… Clear, specific instructions
  • āœ… Structured output formats
  • āœ… Few-shot examples when beneficial

4. Code Organization

  • āœ… Separation of concerns (class-based design)
  • āœ… Type hints for clarity
  • āœ… Docstrings for documentation

5. Performance Optimization

  • āœ… Appropriate temperature settings
  • āœ… Token budget management
  • āœ… Caching where applicable

LangChain Components Used

  • ChatOpenAI: OpenAI model interface
  • Chains: Sequential component composition

Why LangChain? – Composable abstractions for LLM applications – Rich ecosystem of integrations – Production-ready patterns – Active community and development


Execution Flow

1. Initialize agent with configuration
   ↓
2. Load environment and dependencies
   ↓
3. Prepare prompt with input data
   ↓
4. Invoke LLM through LangChain
   ↓
5. Parse and validate response
   ↓
6. Return formatted result

Key Decision Points: – Model selection (temperature, model type) – Prompt structure (few-shot vs zero-shot) – Error handling strategy – Output format validation


Performance Considerations

Computational Cost

  • LLM Calls: Primary cost factor (based on tokens)
  • Latency: Depends on model and input size
  • Memory: Minimal for most patterns

Optimization Strategies

  1. Prompt Optimization: Minimize tokens while maintaining clarity
  2. Caching: Cache repeated queries/responses
  3. Model Selection: Use smaller models when appropriate
  4. Batch Processing: Group similar requests
  5. Streaming: For long responses, stream tokens

Cost Estimates (Approximate)

  • GPT-3.5-turbo: ~$0.002 per request
  • GPT-4: ~$0.03 per request
  • GPT-4-turbo: ~$0.01 per request

Actual costs vary based on input/output token counts


Common Pitfalls to Avoid

1. Prompt Design Issues

āŒ Vague or ambiguous instructions
āœ… Clear, specific, testable prompts

2. Error Handling

āŒ Ignoring API errors or rate limits
āœ… Implement retry logic and graceful degradation

3. Token Management

āŒ Exceeding model token limits
āœ… Truncate inputs, chunk processing

4. Security

āŒ Exposing API keys in code
āœ… Use environment variables

5. Testing

āŒ Only testing happy paths
āœ… Test edge cases, failures, malformed inputs

6. Cost Management

āŒ Unmonitored API usage
āœ… Implement budgets, alerts, and monitoring


Further Improvements

Production Enhancements

  1. Monitoring: Add logging, metrics, tracing
  2. Validation: Input/output schema validation
  3. Rate Limiting: Implement request throttling
  4. Caching: Redis or in-memory cache for responses
  5. Async Support: Non-blocking operations for scalability

Feature Extensions

  1. Multi-Model Support: Switch between providers
  2. Fallback Models: Graceful degradation strategy
  3. Custom Tools: Domain-specific function calling
  4. Advanced Memory: Vector stores for long-term memory
  5. Evaluation: Automated testing and benchmarking

Code Quality

  1. Testing: Unit tests, integration tests
  2. Documentation: API docs, usage examples
  3. Type Safety: Stricter type hints
  4. Configuration: Externalized config files

Related Patterns

  • Pattern 145
  • Pattern 147

Pattern Families: – Reasoning Patterns (001-011) – Multi-Agent Patterns (012-019) – Tool Use Patterns (020-025) – Memory Patterns (026-031) – And many more…


Summary

The Retry Backoff pattern provides a powerful approach to building AI agents with specific capabilities. This implementation demonstrates:

āœ… Key Strengths: – Clean, maintainable code structure – LangChain integration for composability – Type hints and documentation – Production-ready patterns

šŸŽÆ Best Use Cases: – Applications requiring this pattern’s characteristics – Systems needing structured agent behavior – Production environments requiring reliability

šŸ“š Learning Resources: – LangChain Documentation: https://python.langchain.com/ – Pattern Documentation: See related patterns – OpenAI API Reference: https://platform.openai.com/docs/


File: 146_retry_backoff.py
Total Lines: 665
Pattern Category: AI Agent Design Pattern #146


Generated from actual implementation code
For educational and reference purposes

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *