Class: Brute::Middleware::Retry

Inherits:
Base
  • Object
show all
Defined in:
lib/brute/middleware/retry.rb

Overview

Retries the inner call on transient LLM errors with exponential backoff.

Catches LLM::RateLimitError and LLM::ServerError, sleeps with exponential delay, and re-calls the inner app. Non-retryable errors propagate immediately.

Unlike forgecode’s separate retry.rs, this middleware wraps the LLM call directly — it sees the error and retries without the orchestrator knowing.

Constant Summary collapse

DEFAULT_MAX_ATTEMPTS =
3
DEFAULT_BASE_DELAY =

seconds

2

Instance Method Summary collapse

Constructor Details

#initialize(app, max_attempts: DEFAULT_MAX_ATTEMPTS, base_delay: DEFAULT_BASE_DELAY) ⇒ Retry

Returns a new instance of Retry.



23
24
25
26
27
# File 'lib/brute/middleware/retry.rb', line 23

def initialize(app, max_attempts: DEFAULT_MAX_ATTEMPTS, base_delay: DEFAULT_BASE_DELAY)
  super(app)
  @max_attempts = max_attempts
  @base_delay = base_delay
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/brute/middleware/retry.rb', line 29

def call(env)
  attempts = 0
  begin
    @app.call(env)
  rescue LLM::RateLimitError, LLM::ServerError => e
    attempts += 1
    if attempts >= @max_attempts
      env[:metadata][:last_error] = e.message
      raise
    end

    delay = @base_delay ** attempts
    env[:metadata][:retry_attempt] = attempts
    env[:metadata][:retry_delay] = delay

    sleep(delay)
    retry
  end
end