liveBook · Manning

Asynchronous programming

As explained in Build an AI Agent (From Scratch)

Asynchronous operation is execution in which a task proceeds independently of the initiating interaction and reports its result later.

Asynchronous programming lets independent tasks proceed without waiting for one another, then delivers each result as it becomes available. For agent systems, this reduces the elapsed time of parallel LLM requests from the sum of their durations to roughly the longest request. section 2.2.6

Python provides the core execution model:

  • Define coroutines with async def. section 2.2.6
  • Use await to suspend the current coroutine while an asynchronous operation completes, allowing other tasks to run. section 2.2.6
  • Use asyncio.gather to start multiple coroutines concurrently and collect their results. section 2.2.6
  • Call LiteLLM through acompletion when the concurrent operation is an LLM request. section 2.2 section 2.2.6

Parallel LLM questions

Create one coroutine for each prompt, gather the responses, and pair each answer with its question. Three one-second requests can complete in about one second instead of three when run concurrently. section 2.2.6

Production-oriented concurrency adds two safeguards:

  • Wrap the acompletion call inside asyncio.Semaphore to cap active API calls, even when many tasks have been created. section 2.2.6
  • Set num_retries to retry transient failures with exponential backoff. section 2.2.6

Asynchronous workflows can also pause and resume at approval boundaries. Persisting pending tool calls and their approval decisions lets an agent wait for human confirmation before continuing. section 6.3.5

How Build an AI Agent (From Scratch) covers it

How deep it goes
Explains coroutines, await, gathering, concurrency limits, and retries, without descending into event-loop internals.
Examples
Uses concurrent question answering, 100 rate-limited prompts, and pausable tool-confirmation workflows.
What it assumes
Assumes basic Python and LLM API familiarity while teaching async def, await, gather, semaphores, and retries.
What you can do after
Apply it via Python asyncio and LiteLLM to run requests concurrently, limit load, and retry transient failures.
How it connects
Connects coroutines, asyncio, LLM APIs, rate limiting, retries, and human-in-the-loop workflows.

Also explained in

Compare how 28 books explain asynchronous programming →

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.