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.2.tar.gz (2.2 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.2-py3-none-any.whl (2.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: groq_rotator-0.1.2.tar.gz
  • Upload date:
  • Size: 2.2 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.2.tar.gz
Algorithm Hash digest
SHA256 36c88b62e0c91e5efa163052a2c60d1aad1a5ed5171d2ff82b62622c97cf96f9
MD5 a9aae9f908885356142da6d4e51b7688
BLAKE2b-256 ca2089ba9e8f4037c2ec45e659d00c326a2242772056ad21cc0bb976c4de484c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: groq_rotator-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 2.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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0189a4104025ad564ce5c676b951524cdfcff1300284a6e48eaa5b5b73fc1064
MD5 5d9b9cfb12143ee530935edfbf4b2d02
BLAKE2b-256 52221cdcdd6cc5f1a07b9e1b13610a2cfeff75820624e881397bf395ad413b6a

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