Skip to main content

A wrapper for Gemini API to handle multiple API keys and rotate them on quota exhaustion.

Project description

f5So56g.md.png

Never let a 429 error stop your AI agents again.

GoodbyeQuota is a robust Python wrapper for the Google Gemini API (google-generativeai) that manages a pool of API keys. When one key hits its rate limit or quota (ResourceExhausted), this library automatically switches to the next available key and retries the request seamlessly.

Your code doesn't crash. Your users don't wait. It just works.


✨ Features

  • 🔄 Smart Key Rotation: Automatically cycles through a list of API keys when quotas are hit.
  • ⏱️ Intelligent Cooldowns: Temporarily sidelines exhausted keys so they can recover.
  • 💬 Chat Persistence: Seamlessly handles key switching even inside active chat sessions.
  • 🔌 Drop-in Replacement: Designed to look and feel exactly like the standard google-generativeai library.
  • 🛡️ Fault Tolerance: Handles transient service errors (500/503) with automatic retries.

📦 Installation

Clone the repository and install locally:

git clone https://github.com/cmpdchtr/GoodbyeQuota.git
cd GoodbyeQuota
pip install .

🚀 Usage

1. Basic Generation

Just pass a list of keys instead of a single one.

from goodbye_quota import GoodbyeQuota

# 1. Define your pool of keys
api_keys = [
    "AIzaSy...Key1",
    "AIzaSy...Key2",
    "AIzaSy...Key3",
    # Add as many as you want!
]

# 2. Initialize the client
client = GoodbyeQuota(api_keys)

# 3. Create a model (Just like standard Gemini)
model = client.create_model("gemini-pro")

# 4. Generate content without fear
try:
    response = model.generate_content("Explain quantum computing in 5 words.")
    print(f"🤖 AI: {response.text}")
except Exception as e:
    print(f"❌ Failed: {e}")

2. Chat Sessions

GoodbyeQuota maintains the chat history even if the underlying API key changes mid-conversation.

# Start a chat
chat = model.start_chat(history=[])

while True:
    user_input = input("You: ")
    if user_input.lower() in ['exit', 'quit']:
        break
    
    # If Key #1 dies here, Key #2 takes over instantly
    response = chat.send_message(user_input)
    print(f"Gemini: {response.text}")

3. Configuration Strategies

You can choose how keys are selected:

  • round_robin (Default): Cycles through keys in order (1 -> 2 -> 3 -> 1). Best for fair usage.
  • random: Picks a random valid key for each request.
client = GoodbyeQuota(api_keys, strategy="random")

🧠 How It Works

  1. Initialization: You provide a list of valid Gemini API keys.
  2. Execution: When you call .generate_content() or .send_message(), the library configures the global Gemini environment with the current active key.
  3. Error Handling:
    • If a 429 ResourceExhausted error occurs:
      • The current key is marked as "exhausted" and put in a penalty box (cooldown).
      • The library instantly switches to the next available key.
      • The request is retried automatically.
    • If all keys are exhausted, it will raise an exception (or wait, depending on future config).

⚠️ Disclaimer

This library is intended for legitimate use cases where you have multiple valid API keys (e.g., different projects, organization tiers) and want to ensure high availability. Please respect Google's Terms of Service and API usage policies.

📄 License

MIT License. Feel free to use and modify!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

goodbye_quota-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

goodbye_quota-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file goodbye_quota-0.1.0.tar.gz.

File metadata

  • Download URL: goodbye_quota-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for goodbye_quota-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a62b18ffd37230f984a4d03e498f0c7be7e0c9ff94fb5a2382cb621a12bf694
MD5 bc3802e78645acffb9e26ddb43314bc4
BLAKE2b-256 c386e6c53c818655706798c467a33637b004fec6e9048999c8a3172c87afdc08

See more details on using hashes here.

File details

Details for the file goodbye_quota-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: goodbye_quota-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for goodbye_quota-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d08126ce7a675750ebd520dccfd80f6f65af1c9742e4ddbfe3807c3fde6a79b
MD5 aacb44a1a78eb75f5921a484ab4649bd
BLAKE2b-256 0c16687933bdfb27e3551da028b90495d06547fce8c201cc4d2843e6290ec1bb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page