Skip to main content

Drop-in async Groq client with automatic API key rotation on rate limits

Project description

groq-rotator

A drop-in async Groq client that automatically rotates API keys when you hit rate limits. Useful when you have multiple free-tier Groq keys and don't want your pipeline to crash.

Install

pip install groq-rotator

Usage

Use it exactly like the normal AsyncGroq client:

import asyncio
from groq_rotator import AsyncRotatingGroq

client = AsyncRotatingGroq(api_keys=["key1", "key2", "key3"])

async def main():
    response = await client.chat.completions.create(
        model="llama3-70b-8192",
        messages=[{"role": "user", "content": "Hello"}]
    )
    print(response.choices[0].message.content)

asyncio.run(main())

That's it. No extra setup. If key1 hits a rate limit, it silently switches to key2 and retries.

Parallel requests (main use case)

import asyncio
from groq_rotator import AsyncRotatingGroq

client = AsyncRotatingGroq(api_keys=["key1", "key2", "key3"])

async def process(prompt: str):
    response = await client.chat.completions.create(
        model="llama3-70b-8192",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

async def main():
    prompts = ["Summarize X", "Classify Y", "Extract Z"]
    results = await asyncio.gather(*[process(p) for p in prompts])
    print(results)

asyncio.run(main())

FastAPI

from fastapi import FastAPI
from groq_rotator import AsyncRotatingGroq

app = FastAPI()
client = AsyncRotatingGroq(api_keys=["key1", "key2", "key3"])

@app.post("/chat")
async def chat(prompt: str):
    response = await client.chat.completions.create(
        model="llama3-70b-8192",
        messages=[{"role": "user", "content": prompt}]
    )
    return {"response": response.choices[0].message.content}

Optional: know when a key rotates

def on_rotate(index: int):
    print(f"Switched to key index {index}")

client = AsyncRotatingGroq(api_keys=["key1", "key2"], on_rotate=on_rotate)

When is this useful?

  • You have multiple free-tier Groq keys and want to maximize throughput
  • You're running batch jobs or multi-agent pipelines with heavy Groq usage
  • You're in a FastAPI or async backend and can't afford a crash on rate limit
  • You want rotation to be invisible — no changes to how you write Groq code

When you don't need this

  • You have a paid Groq key with high rate limits
  • You're making one-off requests in a script
  • You're not using async

Error handling

If all keys are rate limited, it raises groq.RateLimitError so you can handle it yourself:

from groq import RateLimitError

try:
    response = await client.chat.completions.create(...)
except RateLimitError:
    print("All keys exhausted")

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

groq_rotator-0.1.3.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

groq_rotator-0.1.3-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file groq_rotator-0.1.3.tar.gz.

File metadata

  • Download URL: groq_rotator-0.1.3.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for groq_rotator-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2b072c19644b657d60e0febede5cb0a0274e37a781f68e6b4ae0892f066b26b2
MD5 1fca53cb26c8016940bb800ee6585ae1
BLAKE2b-256 005ab07d65f6e48331238a22ca05ac02fa2ca1a6531046adece0e2f971558d17

See more details on using hashes here.

File details

Details for the file groq_rotator-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: groq_rotator-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for groq_rotator-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 52993a71371ea293c3b6473acaa4798da35db19c7367cb03a0293c8480ebd4e3
MD5 95bbd592feba5f93c7fc9a22a2d5f0fb
BLAKE2b-256 112bd91fbf7522fff9d27eeab31bda760f7684ccb64fbc32075b2156bd7259ef

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