liveBook · Manning

Exponential backoff

As explained in Build an AI Agent (From Scratch)

A backoff strategy progressively delays repeated operations after transient failures or service throttling.

Exponential backoff progressively delays retries after transient failures or service throttling, giving overloaded networks and APIs time to recover. section 2.2.6 section 3.3.4

Use backoff as part of a broader asynchronous reliability pattern:

  • Set LiteLLM's num_retries parameter to retry failed requests automatically, such as num_retries=3. section 2.2.6
  • Combine retries with asyncio.Semaphore so concurrency limits prevent large batches from overwhelming the API. section 2.2.6
  • Collect results with return_exceptions=True so one request failure does not cancel successful concurrent calls. section 2.2.6
  • Add backoff, timeout handling, input validation, and meaningful errors when turning a simple custom tool into a production-ready integration. section 3.3.4

Retrying a batch of LLM calls

Create 100 asynchronous tasks, allow only 10 API calls at once with asyncio.Semaphore(10), and pass num_retries=3 to acompletion so temporary failures receive automatic exponential backoff. section 2.2.6

Listing 2.9 Rate-limited concurrent requests
import asyncio
from litellm import acompletion

# Limit to 10 concurrent requests
semaphore = asyncio.Semaphore(10)

async def call_llm(prompt: str) -> str:
    """LLM call with rate limiting and automatic retry."""
    async with semaphore:
        response = await acompletion(
            model="gpt-5-mini",
            messages=[{"role": "user", "content": prompt}],
            num_retries=3  # Automatic retry with exponential backoff
        )
        return response.choices[0].message.content

# Even with 100 concurrent tasks, only 10 API calls run at a time
prompts = [f"What is {i} + {i}?" for i in range(100)]
tasks = [call_llm(p) for p in prompts]
results = await asyncio.gather(*tasks, return_exceptions=True)

Inspect the complete Python pattern for combining a semaphore, LiteLLM retries, exponential backoff, and exception-tolerant result collection. section 2.2.6

How Build an AI Agent (From Scratch) covers it

How deep it goes
Configures automatic retries and pairs them with concurrency limits, but does not detail the delay calculation internally.
Examples
Uses LiteLLM acompletion with num_retries=3 alongside a ten-call semaphore for batches of 100 prompts.
What it assumes
Assumes familiarity with asynchronous functions, await, coroutines, and concurrent request execution.
What you can do after
Operate/configure asynchronous LLM batches with automatic retries and bounded concurrency.
How it connects
Connects retries with asynchronous calls, rate limiting, semaphores, transient failures, and custom-tool error handling.

Also explained in

Compare how 8 books explain exponential backoff →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.