Skip to main content

🚀 Concurrent OpenAI Manager

A lightweight, preemptive rate limiter and concurrency manager for OpenAI's API

codecov PyPI version License: MIT Python 3.11+

✨ Features

  • 🎯 Preemptive Token Estimation: Attempts to predict token usage before making API calls
  • 🔄 Smart Rate Limiting: Manages requests and tokens per minute to avoid API limits
  • ⚡ Concurrent Request Handling: Efficient parallel processing with semaphore control
  • 💰 Built-in Cost Tracking: Real-time cost estimation for better budget management
  • 🎚️ Fine-tuned Control: Adjustable parameters for optimal performance

📦 Installation

pip install concurrent-openai

🚀 Quick Start

  1. Set up your environment:
echo "OPENAI_API_KEY=your_api_key" >> .env
# OR
export OPENAI_API_KEY=your_api_key

Note: You can also pass the api_key to the ConcurrentOpenAI client.

  1. Start making requests:
from concurrent_openai import ConcurrentOpenAI


client = ConcurrentOpenAI(
    api_key="your-api-key",  # not required if OPENAI_API_KEY env var is set
    max_concurrent_requests=5,
    requests_per_minute=200,
    tokens_per_minute=40000
)

response = client.create(
    messages=[{"role": "user", "content": "Hello!"}],
    model="gpt-4o",
    temperature=0.7
)

print(response.content)

Or pass your own instance of AsyncOpenAI

from openai import AsyncOpenAI
from concurrent_openai import ConcurrentOpenAI


openai_client = AsyncOpenAI(api_key="your-api-key")

client = ConcurrentOpenAI(
    client=openai_client,
    max_concurrent_requests=5,
    requests_per_minute=200,
    tokens_per_minute=40000
)

🎯 Why Concurrent OpenAI Manager?

  • Preemptive Rate Limiting: Unlike other libraries that react to rate limits, here the idea is to predict the token usage before making requests
  • Resource Optimization: Smart throttling prevents request surges and optimizes API usage
  • Cost Control: Built-in cost estimation helps manage API expenses effectively
  • Lightweight: Minimal dependencies, focused functionality

🔧 Advanced Usage

Azure OpenAI Integration

The library supports seamless integration with Azure OpenAI services:

from openai import AsyncAzureOpenAI
from concurrent_openai import ConcurrentOpenAI


azure_client = AsyncAzureOpenAI(
    azure_endpoint="your-azure-endpoint",
    api_key="your-azure-api-key",
    api_version="2024-02-01"
)

client = ConcurrentOpenAI(
    client=azure_client,
    max_concurrent_requests=5,
    requests_per_minute=60,
    tokens_per_minute=10000
)

response = await client.create(
    messages=[{"role": "user", "content": "Hello!"}],
    model="gpt-35-turbo", # Use your deployed model name
    temperature=0.7
)

Batch Processing

from concurrent_openai import ConcurrentOpenAI

messages_list = [
    [{"role": "user", "content": f"Process item {i}"}]
    for i in range(10)
]

client = ConcurrentOpenAI(api_key="your-api-key")
responses = client.create_many(
    messages_list=messages_list,
    model="gpt-40",
    temperature=0.7
)

for resp in responses:
    if resp.is_success:
        print(resp.content)

Cost Tracking

client = ConcurrentOpenAI(
    api_key="your-api-key",
    input_token_cost=2.5 / 1_000_000,  # see https://openai.com/api/pricing/ for the latest costs
    output_token_cost=10 / 1_000_000
)

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for concurrent-openai 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for concurrent-openai 1.2.2
File Size Uploaded
concurrent_openai-1.2.2.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for concurrent-openai 1.2.2
File Interpreter ABI Platform
concurrent_openai-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.7 kB

Release files / concurrent_openai-1.2.2.tar.gz

Download URL concurrent_openai-1.2.2.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
433f5fc1f7126e639739e38953ca438f5c3641f0a0dc46fbe95100bbe5358f1b
BLAKE2b-256 checksum
How to use checksums
47fe7b739bb2af8df92a947943747297c8b69d8c7c3897f27e2a809f19ca3ed2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 24, 2025.

Transparency log

Release files / concurrent_openai-1.2.2-py3-none-any.whl

Download URL concurrent_openai-1.2.2-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a714c54d1d9a5456bb6151c0f57b7d4c2b2068415417b71fbc1e2ce76b3b22cc
BLAKE2b-256 checksum
How to use checksums
b3e5f4e399898cf43f9a15706840af07c27488a31195c1ac0c7f40746ef14f9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 24, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.2.2 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page