Skip to main content

High Concurrency Batch Requests

When you need to process large volumes of requests — for batch evaluation, dataset generation, or production workloads — you can take advantage of Python’s asyncio with aiohttp to run thousands of concurrent calls against KiosAPI.

Installation

httpx is less efficient than aiohttp for high-concurrency workloads. For best performance, prefer aiohttp with a single shared ClientSession.

Full example

Key design points

  • Single shared ClientSession — create one aiohttp.ClientSession and reuse it for all requests. This enables HTTP connection pooling and dramatically reduces overhead.
  • Semaphore for concurrency control — caps the number of in-flight requests to MAX_CONCURRENCY, preventing resource exhaustion.
  • while True loop — continuously pulls the next batch of prompts until your source is exhausted. Replace generate_prompts() with your own logic (file reader, DB cursor, message queue, etc.).

Limits and best practices

Do not exceed 5,000 concurrent requests in a single script. Beyond this point, Python’s single-threaded event loop and OS socket limits become bottlenecks, leading to timeouts and degraded throughput.
If you need more than 5,000 concurrent requests, run multiple scripts in parallel instead of increasing the concurrency limit of a single process. For example, run 3 scripts each with MAX_CONCURRENCY = 2000 to achieve ~6,000 concurrent requests across separate processes.

Error handling

When running at high concurrency, individual request failures are expected. Always use return_exceptions=True with asyncio.gather and implement retries with exponential backoff for transient errors (429, 500, 502, 503).

Next steps

  • Quickstart — get started with a single request.
  • OpenAI SDK — SDK configuration details.
  • Rate Limits — see the dashboard for current limits — understand platform-level limits.