Skip to main content

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")

Release files for groq-rotator 0.1.3

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

Source distribution (sdist)

Source distribution for groq-rotator 0.1.3
File Size Uploaded
groq_rotator-0.1.3.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for groq-rotator 0.1.3
File Interpreter ABI Platform
groq_rotator-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 5.9 kB

Release files / groq_rotator-0.1.3.tar.gz

Download URL groq_rotator-0.1.3.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2b072c19644b657d60e0febede5cb0a0274e37a781f68e6b4ae0892f066b26b2
BLAKE2b-256 checksum
How to use checksums
005ab07d65f6e48331238a22ca05ac02fa2ca1a6531046adece0e2f971558d17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / groq_rotator-0.1.3-py3-none-any.whl

Download URL groq_rotator-0.1.3-py3-none-any.whl
Size 3.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52993a71371ea293c3b6473acaa4798da35db19c7367cb03a0293c8480ebd4e3
BLAKE2b-256 checksum
How to use checksums
112bd91fbf7522fff9d27eeab31bda760f7684ccb64fbc32075b2156bd7259ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

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