Class RetryPolicy
java.lang.Object
com.codename1.ai.RetryPolicy
Decides whether and how long to wait before retrying a failed
LlmClient call. Default policy retries
invalid reference
LlmRateLimitException
(honouring Retry-After) and
invalid reference
LlmModelOverloadedException
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.
-
Method Summary
Modifier and TypeMethodDescriptionlongcomputeDelayMs(Throwable t, int attemptIndex) Returns the delay to wait before the next attempt, honouringRetry-Afterfrom rate-limit exceptions when present.static RetryPolicycustom(int maxAttempts, long initialDelayMs, long maxDelayMs, double multiplier, boolean jitter) Creates a bounded exponential retry policy.static RetryPolicy4 attempts, starting at 500 ms, doubling, capped at 30 s, with jitter.intstatic RetryPolicynone()No retries -- failures are returned to the caller as-is.booleanshouldRetry(Throwable t, int attemptsSoFar) Inspects a thrown exception and decides whether this policy permits another attempt.
-
Method Details
-
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
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:
maxAttempts- total attempts including the initial callinitialDelayMs- delay before the first retrymaxDelayMs- upper bound for computed delaysmultiplier- growth factor applied after each attemptjitter- whether to randomize each delay from zero to its bound- Returns:
- normalized retry policy
-
shouldRetry
Inspects a thrown exception and decides whether this policy permits another attempt.- Parameters:
t- operation failureattemptsSoFar- attempts already made, including the failed one- Returns:
truefor a transient failure within the attempt limit
-
computeDelayMs
Returns the delay to wait before the next attempt, honouringRetry-Afterfrom rate-limit exceptions when present.- Parameters:
t- failure that triggered the retryattemptIndex- zero-based retry index- Returns:
- delay in milliseconds
-
getMaxAttempts
public int getMaxAttempts()- Returns:
- total allowed attempts including the initial call
-