call_llm_robust: Robustly Call LLM API (Simple Retry)

View source: R/LLM_robust_utils.R

call_llm_robustR Documentation

Robustly Call LLM API (Simple Retry)

Description

Wraps call_llm to handle rate-limit errors (HTTP 429 or related "Too Many Requests" messages). It retries the call a specified number of times, now using exponential backoff. You can also choose to cache responses if you do not need fresh results each time.

Usage

call_llm_robust(
  config,
  messages,
  tries = 3,
  wait_seconds = 10,
  backoff_factor = 2,
  verbose = FALSE,
  json = 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. Number of retries before giving up. Default is 3.

wait_seconds

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

backoff_factor

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

verbose

Logical. If TRUE, prints the full API response.

json

Logical. If TRUE, returns the raw JSON as an attribute.

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.

Examples

## Not run: 
  # Basic usage:
  robust_resp <- call_llm_robust(
    config = my_llm_config,
    messages = list(list(role = "user", content = "Hello, LLM!")),
    tries = 3,
    wait_seconds = 10,
    memoize = FALSE
  )
  cat("Response:", robust_resp, "\n")

## End(Not run)

LLMR documentation built on April 4, 2025, 1:11 a.m.