# Class RetryPolicy

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

public final class RetryPolicy extends java.lang.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 Summary

  Modifier and Type
  Method
  Description
  `long`
  `computeDelayMs(Throwable t, int attemptIndex)`
  Returns the delay to wait before the next attempt, honouring `Retry-After` from rate-limit exceptions when present.
  `static RetryPolicy`
  `custom(int maxAttempts, long initialDelayMs, long maxDelayMs, double multiplier, boolean jitter)`
  `static RetryPolicy`
  `exponentialBackoff()`
  4 attempts, starting at 500 ms, doubling, capped at 30 s, with jitter.
  `int`
  `getMaxAttempts()`
  `static RetryPolicy`
  `none()`
  No retries -- failures are returned to the caller as-is.
  `boolean`
  `shouldRetry(Throwable t, int attemptsSoFar)`
  Inspect a thrown exception and decide whether to retry.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

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

  - ### none

    public static RetryPolicy none()
    No retries -- failures are returned to the caller as-is.

  - ### custom

    public static RetryPolicy custom(int maxAttempts, long initialDelayMs, long maxDelayMs, double multiplier, boolean jitter)

  - ### shouldRetry

    public boolean shouldRetry(Throwable t, int attemptsSoFar)
    Inspect a thrown exception and decide whether to retry. Apps can override to add provider-specific rules (e.g. retry on a custom 5xx code).

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

  - ### getMaxAttempts

    public int getMaxAttempts()