Async/Await & Concurrent API Calls
~20 min read
AI applications make many API calls that can run concurrently. asyncio and async/await are essential for building fast LLM pipelines that do not block on network I/O.
Python asyncio enables concurrent I/O without threading complexity. async def defines a coroutine — a function that can pause (await) while waiting for I/O without blocking the event loop. For AI: call 5 LLM APIs simultaneously instead of sequentially with asyncio.gather(). Streaming LLM responses use async for to iterate over chunks as they arrive.
💻 Code example
import asyncio
import anthropic
client = anthropic.AsyncAnthropic()
async def analyze_document(doc: str) -> str:
response = await client.messages.create(
model="claude-opus-4-5", max_tokens=512,
messages=[{"role": "user", "content": f"Summarize: {doc}"}]
)
return response.content[0].text
# Process 5 documents concurrently instead of sequentially
async def batch_analyze(docs: list[str]) -> list[str]:
tasks = [analyze_document(doc) for doc in docs]
return await asyncio.gather(*tasks) # 5x faster than sequential
results = asyncio.run(batch_analyze(my_documents))
💬 Deep Dive with AI
Key points
- •async def creates a coroutine — must be awaited to run
- •await pauses execution until I/O completes (non-blocking)
- •asyncio.gather() runs multiple coroutines concurrently
- •async for iterates over streaming responses as chunks arrive