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

groqauto-0.1.0.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

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

groqauto-0.1.0-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for groqauto-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5f0c96c90125134f9b2487dd53c2db3c6fc41a9c9f46b1f6bb41265fb49dba61
MD5 3739bf62f494b07136bc5e181b5c69fe
BLAKE2b-256 e300d715e06bde87238fc2803c70caadb3c2c9df3bcfaf52fbe30749e01dc6f2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for groqauto-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2bda3592352ca9d312ace8b36b05ecfc62751f8847be5cf1c1e511cdf26fde0
MD5 a11f408227a0a4e1a4755a3342827b4f
BLAKE2b-256 a1d36f75be2a9156867568368b92776eb93aec2b75712f1381afce6006e6d0e4

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