public final class RetryPolicy
- Object
- RetryPolicy
Decides whether and how long to wait before retrying a failed
LlmClient call. Default policy retries LlmRateLimitException
(honouring Retry-After) and LlmModelOverloadedException with
exponential backoff + jitter; other failures are surfaced
immediately.
Wire a policy onto a request like this:
AsyncResource<ChatResponse> r = RetryPolicy.exponentialBackoff()
.runChat(client, request);
The synchronous block runs on the calling thread. On the EDT it
uses Display.invokeAndBlock automatically so the UI stays
responsive between attempts.
Methods
public static RetryPolicy exponentialBackoff() | 4 attempts, starting at 500 ms, doubling, capped at 30 s, with jitter. |
public static RetryPolicy none() | No retries – failures are returned to the caller as-is. |
public static RetryPolicy custom(int maxAttempts, long initialDelayMs, long maxDelayMs, double multiplier, boolean jitter) | Creates a bounded exponential retry policy. |
public boolean shouldRetry(Throwable t, int attemptsSoFar) | Inspects a thrown exception and decides whether this policy permits another attempt. |
public long computeDelayMs(Throwable t, int attemptIndex) | Returns the delay to wait before the next attempt, honouring Retry-After from rate-limit exceptions when present. |
public int getMaxAttempts() |
Inherited methods
Method details
exponentialBackoff
public static RetryPolicy exponentialBackoff()4 attempts, starting at 500 ms, doubling, capped at 30 s, with
jitter. Good default for chat workloads.
Returns
recommended transient-failure policy
none
public static RetryPolicy none()No retries – failures are returned to the caller as-is.
Returns
single-attempt policy
custom
public static RetryPolicy custom(int maxAttempts, long initialDelayMs, long maxDelayMs, double multiplier, boolean jitter)Creates a bounded exponential retry policy.
Parameters
maxAttemptsint- total attempts including the initial call
initialDelayMslong- delay before the first retry
maxDelayMslong- upper bound for computed delays
multiplierdouble- growth factor applied after each attempt
jitterboolean- whether to randomize each delay from zero to its bound
Returns
normalized retry policy
shouldRetry
public boolean shouldRetry(Throwable t, int attemptsSoFar)Inspects a thrown exception and decides whether this policy permits
another attempt.
Parameters
tThrowable- operation failure
attemptsSoFarint- attempts already made, including the failed one
Returns
true for a transient failure within the attempt limitcomputeDelayMs
public long computeDelayMs(Throwable t, int attemptIndex)Returns the delay to wait before the next attempt, honouring
Retry-After from rate-limit exceptions when present.Parameters
tThrowable- failure that triggered the retry
attemptIndexint- zero-based retry index
Returns
delay in milliseconds
getMaxAttempts
public int getMaxAttempts()Returns
total allowed attempts including the initial call