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.1.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.1-py3-none-any.whl (2.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: groq_rotator-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 44b1ae256cf65d58ecc1ff1d4e100517fdb656a8621812024d8f2252a419f0f9
MD5 33f33fd8115d77d53d621dd4c1b19b2c
BLAKE2b-256 f0665c4d13318eadb1c991e0f5ab7cee3a1dc32848ca305af22e81aa717308d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: groq_rotator-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 830a6baa782123afbc38d179ba68c584a91a8300bf79514af4a9c905adc9e650
MD5 968aa80e8c1aeddca51f7e8347bd1fd8
BLAKE2b-256 401d20d3c8cb3fd929cd7a205628c963dad2ce6ea977e068b693e4d3f10f26e4

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