Groq logo

Groq API Integration & Code Recipes

OpenAI SDK

Copy-paste OpenAI-compatible code snippets for Python, Node.js, and cURL

OpenAI-Compatible SDK Recipes

Developer API SDK & Code Recipes for Groq

Tested OpenAI-standard drop-in code snippets for Python, TypeScript, and cURL.

OpenAI API Compatible
# 1. Install official OpenAI client:
# pip install openai

from openai import OpenAI
import os

client = OpenAI(
    base_url="https://api.groq.com/openai/v1",
    api_key=os.environ.get("GROQ_API_KEY", "your_api_key_here")
)

response = client.chat.completions.create(
    model="llama-3.3-70b-versatile",
    messages=[
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": "Explain KV cache compression in 3 concise bullet points."}
    ],
    temperature=0.6,
)

print(response.choices[0].message.content)
💡 Tip: Set GROQ_API_KEY in your environment variables. Zero client SDK changes required.Open Groq Console

1. Python OpenAI Drop-In Replacement

Python 3.10+

You can use your existing OpenAI code without refactoring. Simply replace the base URL and pass your Groq API key:

from openai import OpenAI client = OpenAI(base_url="https://api.groq.com/openai/v1", api_key=KEY) res = client.chat.completions.create(model="llama-3.3-70b", messages=[...])

2. Low-Latency Streaming (SSE)

<150ms TTFT

For real-time voice agents or chatbot UIs, stream tokens chunk-by-chunk using Server-Sent Events (SSE) to eliminate perceptible wait times.

stream = client.chat.completions.create(..., stream=True) for chunk in stream: print(chunk.choices[0].delta.content or "", end="", flush=True)

3. High-Concurrency Async Batching

AsyncOpenAI

Use AsyncOpenAI with asyncio.gather to process hundreds of parallel requests simultaneously without blocking the main event loop.

from openai import AsyncOpenAI client = AsyncOpenAI(base_url=URL, api_key=KEY) results = await asyncio.gather(*(send_request(p) for p in prompts))

4. Exponential Backoff & Retries

Tenacity / Retries

To handle sudden traffic spikes gracefully, configure client-side retries with exponential backoff on HTTP 429 status codes.

# Configure automatic client retry in OpenAI SDK: client = OpenAI(max_retries=3, timeout=30.0)