By Sagar Shankaran, Founder of CallSphere
Learn how to use AsyncOpenAI with Python's asyncio to make concurrent API calls, implement connection pooling, and build high-throughput AI pipelines.
Key takeaways
Synchronous OpenAI API calls block your Python thread while waiting for the response — typically 1 to 10 seconds per request. If you need to process 100 items, that means 100 sequential waits. With async programming, you can fire off many requests concurrently and process them as they complete, reducing total wall-clock time dramatically.
The OpenAI Python SDK ships with a fully async client that integrates seamlessly with Python's asyncio event loop.
The async client mirrors the synchronous API exactly, but every method is a coroutine:
flowchart LR
REQ(["Request"])
BATCH["Continuous batching<br/>vLLM scheduler"]
PREF{"Prefill or<br/>decode?"}
PRE["Prefill phase<br/>parallel attention"]
DEC["Decode phase<br/>token by token"]
KV[("Paged KV cache")]
SAMP["Sampling<br/>top-p, temp"]
STREAM["Stream tokens<br/>to client"]
REQ --> BATCH --> PREF
PREF -->|First token| PRE --> KV
PREF -->|Next token| DEC
KV --> DEC --> SAMP --> STREAM
SAMP -->|EOS| DONE(["Response complete"])
style BATCH fill:#4f46e5,stroke:#4338ca,color:#fff
style KV fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
style STREAM fill:#0ea5e9,stroke:#0369a1,color:#fff
style DONE fill:#059669,stroke:#047857,color:#fff
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def main():
response = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello, async world!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())
The AsyncOpenAI client uses httpx.AsyncClient under the hood, which provides connection pooling and HTTP/2 support automatically.
Hear it before you finish reading
Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.
The biggest win comes from running multiple requests at the same time:
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def summarize(text: str) -> str:
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Summarize the following text in one sentence."},
{"role": "user", "content": text},
],
)
return response.choices[0].message.content
async def main():
articles = [
"Python 3.13 introduces a new JIT compiler that improves performance...",
"The European Union's AI Act requires transparency for high-risk systems...",
"SpaceX successfully launched its 300th Falcon 9 mission this quarter...",
"OpenAI released GPT-4o with native multimodal capabilities...",
"Rust adoption in enterprise backends grew by 40% in 2025...",
]
# Run all 5 summaries concurrently
summaries = await asyncio.gather(*[summarize(article) for article in articles])
for article, summary in zip(articles, summaries):
print(f"Original: {article[:50]}...")
print(f"Summary: {summary}")
print()
asyncio.run(main())
With synchronous code, this takes 5x the time of a single request. With asyncio.gather, all five requests run concurrently and the total time is roughly equal to the slowest single request.
Firing 1000 concurrent requests will hit rate limits. Use a semaphore to cap concurrency:
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI()
semaphore = asyncio.Semaphore(10) # max 10 concurrent requests
async def process_item(item: str) -> str:
async with semaphore:
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": f"Classify this feedback: {item}"}],
)
return response.choices[0].message.content
async def main():
feedback_items = [f"Feedback item {i}" for i in range(100)]
tasks = [process_item(item) for item in feedback_items]
results = await asyncio.gather(*tasks)
print(f"Processed {len(results)} items")
asyncio.run(main())
The semaphore ensures no more than 10 requests are in-flight at any moment, preventing rate limit errors while still processing items much faster than sequential code.
Combine async with streaming for the best real-time experience:
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def stream_chat(prompt: str):
stream = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
stream=True,
)
async for chunk in stream:
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="", flush=True)
print()
asyncio.run(stream_chat("Explain event loops in Python."))
When tasks have variable completion times, asyncio.as_completed lets you handle results as they arrive:
Still reading? Stop comparing — try CallSphere live.
CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def analyze(text: str, index: int) -> tuple[int, str]:
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Extract the sentiment: positive, negative, or neutral."},
{"role": "user", "content": text},
],
)
return index, response.choices[0].message.content
async def main():
texts = [
"This product is amazing! Best purchase ever.",
"Terrible experience. Will never buy again.",
"It works fine. Nothing special.",
]
tasks = [analyze(text, i) for i, text in enumerate(texts)]
for coro in asyncio.as_completed(tasks):
index, sentiment = await coro
print(f"Item {index}: {sentiment}")
asyncio.run(main())
FastAPI is natively async, making it a natural fit:
from fastapi import FastAPI
from openai import AsyncOpenAI
app = FastAPI()
client = AsyncOpenAI()
@app.post("/analyze")
async def analyze_text(text: str):
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Analyze the sentiment of this text."},
{"role": "user", "content": text},
],
)
return {"sentiment": response.choices[0].message.content}
Create one client and reuse it across all requests. The client manages an internal connection pool. Creating a new client per request wastes connections and adds overhead.
Yes, but keep them separate. Use OpenAI() for synchronous code and AsyncOpenAI() for async code. Do not call synchronous methods from within an async function — it blocks the event loop.
It depends on your rate limits. Check your plan's requests-per-minute (RPM) limit. A good starting point is a semaphore value of RPM divided by 6 (to account for variable request duration). Monitor 429 errors and adjust.
#OpenAI #AsyncPython #AsyncIO #Concurrency #Performance #AgenticAI #LearnAI #AIEngineering

Written by
Sagar Shankaran· Founder, CallSphere
LinkedInSagar Shankaran is the founder of CallSphere, where he builds production AI voice and chat agents deployed across healthcare, hospitality, real estate, and home services. He writes about agentic AI, LLM engineering, and shipping voice agents that handle real calls in production.
See how AI voice agents work for your industry. Live demo available -- no signup required.
OpenAI's Frontier platform makes model-native orchestration the default. What that means for agent builders, voice/chat buyers, and the build-vs-buy decision.
The 2026 desktop AI agent landscape — ServiceNow Project Arc, Anthropic Claude offerings, OpenAI agents, and Google Mariner. A buyer's map.
May 2026's biggest agent-architecture shift: planning, tool selection, and self-correction move inside the model. Framework code shrinks. Here is what changes.
A three-way comparison of Gemini Enterprise, Anthropic managed agents and OpenAI Frontier Platform after Cloud Next 2026 — strengths, gaps, buyer fit.
Anthropic's May 2026 push positions Claude as a vertical platform for financial services. The strategic positioning versus OpenAI and Google.
Anthropic's Mythos is not alone. Compare Mythos against OpenAI's cybersec offerings, Google's Big Sleep lineage, and open-source alternatives in 2026.
© 2026 CallSphere Inc. All rights reserved.
Made within San Francisco
Watch how CallSphere handles real customer calls, schedules appointments, and processes payments — live.
Try Live DemoBook a DemoCalculate Your ROI