Skip to contents

Wraps call_llm so that transient failures are retried while permanent ones fail fast. Retried conditions are rate limits (HTTP 429), server errors (HTTP 5xx and 408), and network-level interruptions (timeouts, connection resets, DNS failures). Errors that retrying cannot fix, such as an invalid parameter (400), a missing key (401/403), or a prompt that exceeds the context window, are raised immediately.

Usage

call_llm_robust(
  config,
  messages,
  tries = 5,
  wait_seconds = 2,
  backoff_factor = 3,
  verbose = FALSE,
  memoize = FALSE
)

Arguments

config

An llm_config object from llm_config.

messages

A list of message objects (or character vector for embeddings).

tries

Integer. Total number of attempts (the first call plus retries) before giving up. Default is 5.

wait_seconds

Numeric. Initial wait time (seconds) before the first retry. Default is 2.

backoff_factor

Numeric. Multiplier for wait time after each failure. Default is 3.

verbose

Logical. If TRUE, prints the full API response.

memoize

Logical. If TRUE, calls are cached to avoid repeated identical requests. Default is FALSE.

Value

The successful result from call_llm, or an error if all retries fail.

Details

When the provider supplies a Retry-After header with a 429, the wait honors it; otherwise waits grow exponentially with a little jitter so that parallel workers do not retry in lockstep.

See also

call_llm for the underlying, non-robust API call. cache_llm_call for a memoised version that avoids repeated requests. llm_config to create the configuration object. chat_session for stateful, interactive conversations.

Examples

if (FALSE) { # \dontrun{
robust_resp <- call_llm_robust(
config = llm_config("groq", "openai/gpt-oss-20b"),
messages = list(list(role = "user", content = "Hello, LLM!")),
tries = 5,
wait_seconds = 2,
memoize = FALSE
)
print(robust_resp)
as.character(robust_resp)
} # }