LLMClientRetry

org.llm4s.llmconnect.LLMClientRetry
@Stable

Stateless helper functions for retrying LLM completion and streaming calls.

Retries the errors RetryPolicy.isRetryable retries (e.g. rate limit, timeout, a 5xx), by the same rule. Fails immediately on any other error, including a client-error response.

'''Retry delay precedence''' (honors upstream backpressure):

  • If the error provides a provider retry-delay hint (e.g. retryDelay on org.llm4s.error.RateLimitError, org.llm4s.error.ServiceError) and it is present and positive, that value is used so we do not retry before the server is ready.
  • Otherwise we fall back to local exponential backoff (baseDelay * 2^attempt) to avoid tight retry loops.
  • The chosen delay is always capped at 30 seconds so waits remain bounded.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def completeWithRetry(client: LLMClient, conversation: Conversation, options: CompletionOptions, maxAttempts: Int, baseDelay: FiniteDuration): Result[Completion]

Calls client.complete with retries on recoverable errors. Binary-compatible overload that delegates to the full version, sleeping the calling thread.

Calls client.complete with retries on recoverable errors. Binary-compatible overload that delegates to the full version, sleeping the calling thread.

Attributes

def completeWithRetry(client: LLMClient, conversation: Conversation, options: CompletionOptions, maxAttempts: Int, baseDelay: FiniteDuration, sleepFn: FiniteDuration => Unit): Result[Completion]

Calls client.complete with retries on recoverable errors.

Calls client.complete with retries on recoverable errors.

Value parameters

baseDelay

base delay for backoff when provider retry-delay hints are absent (default: 1 second); must be positive

client

LLM client

conversation

conversation to complete

maxAttempts

maximum attempts including the first (default: 3); must be positive

options

completion options (default: CompletionOptions())

sleepFn

function used to pause between retries (default: sleeps the calling thread); override for testing

Attributes

Returns

Right(Completion) on success, Left(error) when retries exhausted, non-recoverable error, invalid input, or interrupted

def streamCompleteWithRetry(client: LLMClient, conversation: Conversation, options: CompletionOptions, maxAttempts: Int, baseDelay: FiniteDuration)(onChunk: StreamedChunk => Unit): Result[Completion]

Calls client.streamComplete with retries only when failure occurs before any chunk is emitted. Binary-compatible overload that delegates to the full version, sleeping the calling thread.

Calls client.streamComplete with retries only when failure occurs before any chunk is emitted. Binary-compatible overload that delegates to the full version, sleeping the calling thread.

Attributes

def streamCompleteWithRetry(client: LLMClient, conversation: Conversation, options: CompletionOptions, maxAttempts: Int, baseDelay: FiniteDuration, sleepFn: FiniteDuration => Unit)(onChunk: StreamedChunk => Unit): Result[Completion]

Calls client.streamComplete with retries only when failure occurs before any chunk is emitted. Once streaming has started (at least one chunk delivered), any error is returned immediately without retry.

Calls client.streamComplete with retries only when failure occurs before any chunk is emitted. Once streaming has started (at least one chunk delivered), any error is returned immediately without retry.

Value parameters

baseDelay

base delay for backoff when provider retry-delay hints are absent (default: 1 second); must be positive

client

LLM client

conversation

conversation to complete

maxAttempts

maximum attempts including the first (default: 3); must be positive

onChunk

callback for each streamed chunk

options

completion options (default: CompletionOptions())

sleepFn

function used to pause between retries (default: sleeps the calling thread); override for testing

Attributes

Returns

Right(Completion) on success, Left(error) when retries exhausted, non-recoverable error, invalid input, or interrupted