Class: RubyLLM::Agents::DSL::Reliability::OnFailureBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/agents/dsl/reliability.rb

Overview

Builder class for on_failure block with simplified syntax

Uses more intuitive method names:

  • ‘retry times:` instead of `retries max:`

  • ‘fallback to:` instead of `fallback_models`

  • ‘circuit_breaker after:` instead of `circuit_breaker errors:`

  • ‘timeout` instead of `total_timeout`

Reliability DSL collapse

Reliability DSL collapse

Constructor Details

#initializeOnFailureBuilder

Returns a new instance of OnFailureBuilder.



372
373
374
375
376
377
378
379
380
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 372

def initialize
  @retries_config = nil
  @fallback_models_list = []
  @total_timeout_value = nil
  @circuit_breaker_config = nil
  @retryable_patterns_list = nil
  @fallback_providers_list = []
  @non_fallback_errors_list = nil
end

Instance Attribute Details

#circuit_breaker_configObject (readonly)

Returns the value of attribute circuit_breaker_config.



368
369
370
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 368

def circuit_breaker_config
  @circuit_breaker_config
end

#fallback_models_listObject (readonly)

Returns the value of attribute fallback_models_list.



368
369
370
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 368

def fallback_models_list
  @fallback_models_list
end

#fallback_providers_listObject (readonly)

Returns the value of attribute fallback_providers_list.



368
369
370
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 368

def fallback_providers_list
  @fallback_providers_list
end

#non_fallback_errors_listObject (readonly)

Returns the value of attribute non_fallback_errors_list.



368
369
370
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 368

def non_fallback_errors_list
  @non_fallback_errors_list
end

#retries_configObject (readonly)

Returns the value of attribute retries_config.



368
369
370
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 368

def retries_config
  @retries_config
end

#retryable_patterns_listObject (readonly)

Returns the value of attribute retryable_patterns_list.



368
369
370
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 368

def retryable_patterns_list
  @retryable_patterns_list
end

#total_timeout_valueObject (readonly)

Returns the value of attribute total_timeout_value.



368
369
370
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 368

def total_timeout_value
  @total_timeout_value
end

Instance Method Details

#circuit_breaker(after: nil, errors: nil, within: 60, cooldown: 300) ⇒ Object

Configure circuit breaker

Examples:

circuit_breaker after: 5, cooldown: 5.minutes
circuit_breaker errors: 10, within: 60, cooldown: 300

Parameters:

  • after (Integer) (defaults to: nil)

    Number of errors to trigger open state

  • errors (Integer) (defaults to: nil)

    Alias for after (compatibility)

  • within (Integer) (defaults to: 60)

    Rolling window in seconds

  • cooldown (Integer, ActiveSupport::Duration) (defaults to: 300)

    Cooldown period



456
457
458
459
460
461
462
463
464
465
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 456

def circuit_breaker(after: nil, errors: nil, within: 60, cooldown: 300)
  error_threshold = after || errors || 10
  cooldown_seconds = cooldown.respond_to?(:to_i) ? cooldown.to_i : cooldown

  @circuit_breaker_config = {
    errors: error_threshold,
    within: within,
    cooldown: cooldown_seconds
  }
end

#fallback(to:) ⇒ Object

Configure fallback models

Examples:

fallback to: "gpt-4o-mini"
fallback to: ["gpt-4o-mini", "gpt-3.5-turbo"]

Parameters:

  • to (String, Array<String>)

    Model(s) to fall back to



411
412
413
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 411

def fallback(to:)
  @fallback_models_list = Array(to)
end

#fallback_models(*models) ⇒ Object

Also support fallback_models for compatibility



416
417
418
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 416

def fallback_models(*models)
  @fallback_models_list = models.flatten
end

#fallback_provider(provider, **options) ⇒ Object

Configure a fallback provider (for audio agents)

Parameters:

  • provider (Symbol)

    The provider to fall back to

  • options (Hash)

    Provider-specific options



425
426
427
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 425

def fallback_provider(provider, **options)
  @fallback_providers_list << {provider: provider, **options}
end

#non_fallback_errors(*error_classes) ⇒ Object

Configure errors that should never trigger fallback



473
474
475
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 473

def non_fallback_errors(*error_classes)
  @non_fallback_errors_list = error_classes.flatten
end

#retries(times: 0, backoff: :exponential, base: 0.4, max_delay: 3.0, on: []) ⇒ Object

Configure retry behavior

Examples:

retries times: 3, backoff: :exponential

Parameters:

  • times (Integer) (defaults to: 0)

    Number of retry attempts

  • backoff (Symbol) (defaults to: :exponential)

    Backoff strategy (:constant or :exponential)

  • base (Float) (defaults to: 0.4)

    Base delay in seconds

  • max_delay (Float) (defaults to: 3.0)

    Maximum delay between retries

  • on (Array<Class>) (defaults to: [])

    Error classes to retry on



393
394
395
396
397
398
399
400
401
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 393

def retries(times: 0, backoff: :exponential, base: 0.4, max_delay: 3.0, on: [])
  @retries_config = {
    max: times,
    backoff: backoff,
    base: base,
    max_delay: max_delay,
    on: on
  }
end

#retryable_patterns(*patterns) ⇒ Object

Configure additional retryable patterns



468
469
470
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 468

def retryable_patterns(*patterns)
  @retryable_patterns_list = patterns.flatten
end

#timeout(duration) ⇒ Object Also known as: total_timeout

Configure timeout for all retry/fallback attempts

Examples:

timeout 30
timeout 30.seconds

Parameters:

  • duration (Integer, ActiveSupport::Duration)

    Timeout duration



437
438
439
440
# File 'lib/ruby_llm/agents/dsl/reliability.rb', line 437

def timeout(duration)
  # Handle ActiveSupport::Duration
  @total_timeout_value = duration.respond_to?(:to_i) ? duration.to_i : duration
end