Class RetryPolicy

java.lang.Object
com.codename1.ai.RetryPolicy

public final class RetryPolicy extends Object

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
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.

  • 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:
      maxAttempts - total attempts including the initial call
      initialDelayMs - delay before the first retry
      maxDelayMs - upper bound for computed delays
      multiplier - growth factor applied after each attempt
      jitter - 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:
      t - operation failure
      attemptsSoFar - attempts already made, including the failed one
      Returns:
      true for a transient failure within the attempt limit
    • computeDelayMs

      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:
      t - failure that triggered the retry
      attemptIndex - zero-based retry index
      Returns:
      delay in milliseconds
    • getMaxAttempts

      public int getMaxAttempts()
      Returns:
      total allowed attempts including the initial call