import asyncio async def fetch(client, prompt, sem): async with sem: # never more than N in flight return await client.complete(prompt) async def main(client, prompts, limit=10): sem = asyncio.Semaphore(limit) tasks = [fetch(client, p, sem) for p in prompts] # return_exceptions keeps one failure from cancelling the rest results = await asyncio.gather(*tasks, return_exceptions=True) ok = [r for r in results if not isinstance(r, Exception)] bad = [r for r in results if isinstance(r, Exception)] print(f"{len(ok)} ok, {len(bad)} failed") return ok asyncio.run(main(client, prompts))