Class: Ask::Agent::Middleware::RetryOnFailure
- Defined in:
- lib/ask/agent/middleware/retry_on_failure.rb
Overview
Retries LLM provider calls on transient failures.
Retries on:
- RateLimitError with exponential backoff + jitter
- ServerError (5xx) with exponential backoff
Does NOT retry on:
- Unauthorized — credentials are wrong, retrying won't help
- InvalidCredential — same
- ModelNotFound — model doesn't exist
- ConfigurationError — user config issue
Constant Summary collapse
- DEFAULT_MAX_RETRIES =
3
Instance Method Summary collapse
- #around_request(provider, request) ⇒ Object
-
#initialize(max_retries: DEFAULT_MAX_RETRIES) ⇒ RetryOnFailure
constructor
A new instance of RetryOnFailure.
Constructor Details
#initialize(max_retries: DEFAULT_MAX_RETRIES) ⇒ RetryOnFailure
Returns a new instance of RetryOnFailure.
23 24 25 |
# File 'lib/ask/agent/middleware/retry_on_failure.rb', line 23 def initialize(max_retries: DEFAULT_MAX_RETRIES) @max_retries = max_retries end |
Instance Method Details
#around_request(provider, request) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ask/agent/middleware/retry_on_failure.rb', line 27 def around_request(provider, request) last_error = nil @max_retries.times do |attempt| begin return yield rescue Ask::RateLimitError => e raise if attempt >= @max_retries - 1 delay = e.retry_after || compute_backoff(attempt) sleep(delay) last_error = e rescue Ask::ServerError, Ask::ServiceUnavailable => e raise if attempt >= @max_retries - 1 sleep(compute_backoff(attempt)) last_error = e rescue Ask::Unauthorized, Ask::InvalidCredential, Ask::ModelNotFound, Ask::ConfigurationError raise end end raise last_error if last_error end |