← Code Recipes
Language Models

Groq Free code examples in Python and JavaScript

Open models served on custom hardware — the fastest tokens per second available.

C# Python Java JavaScript

WHAT THIS PAGE COVERS

Streaming, where the speed difference is the whole point.

Model openai/gpt-oss-20b
Package groq
Checked

Model ids change. If a sample returns a 404 on the model, that is the first thing to check against the official docs.

Install

Groq has its own SDK, and it is the OpenAI SDK's shape with a different name — same methods, same typed errors. You can also point the OpenAI client at Groq if you would rather not add a dependency.

pip install groq

export GROQ_API_KEY="gsk_..."      # macOS / Linux
setx GROQ_API_KEY "gsk_..."        # Windows, then open a new shell
First call

Note the model id has a namespace. Groq serves open models from several publishers, so ids look like openai/... or meta-llama/... — the prefix is part of the name, not a path.

import os
from groq import Groq

client = Groq(api_key=os.environ["GROQ_API_KEY"])

response = client.chat.completions.create(
    model="openai/gpt-oss-20b",
    messages=[
        {"role": "system", "content": "You are a concise technical assistant."},
        {"role": "user", "content": "Explain a database index in two sentences."},
    ],
)

print(response.choices[0].message.content)
Streaming — the whole point

Groq's pitch is tokens per second, and non-streaming throws that away: the user waits for the full answer either way. Measure it, because the difference is the reason to be on this provider.

import time

start = time.perf_counter()
tokens = 0

stream = client.chat.completions.create(
    model="openai/gpt-oss-20b",
    messages=[{"role": "user", "content": "Explain B-trees in 200 words."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        tokens += 1
        print(delta, end="", flush=True)

elapsed = time.perf_counter() - start
print(f"\n~{tokens / elapsed:.0f} chunks/sec")
Swapping providers without a rewrite

Because the SDK mirrors OpenAI's, the same code runs against either — which is what makes Groq easy to trial and easy to fall back from.

import os
from groq import Groq
from openai import OpenAI

def build_client():
    # One switch. Everything downstream calls .chat.completions.create
    # with the same arguments on both clients.
    if os.environ.get("USE_GROQ"):
        return Groq(api_key=os.environ["GROQ_API_KEY"]), "openai/gpt-oss-20b"
    return OpenAI(), "gpt-5.4-mini"

client, model = build_client()

response = client.chat.completions.create(
    model=model,
    messages=[{"role": "user", "content": "Explain a database index."}],
)
print(response.choices[0].message.content)

# The typed errors line up too: groq.RateLimitError and
# openai.RateLimitError have the same name and the same meaning.
Install

Groq's own SDK mirrors the OpenAI SDK's shape. Server-side only.

npm install groq-sdk

export GROQ_API_KEY="gsk_..."
First call

Model ids carry a publisher prefix — openai/... or meta-llama/... — because Groq serves open models from several sources. The prefix is part of the id.

import Groq from "groq-sdk";

const client = new Groq({ apiKey: process.env.GROQ_API_KEY });

const response = await client.chat.completions.create({
  model: "openai/gpt-oss-20b",
  messages: [
    { role: "system", content: "You are a concise technical assistant." },
    { role: "user", content: "Explain a database index in two sentences." },
  ],
});

console.log(response.choices[0].message.content);
Streaming — the whole point

Non-streaming discards the reason to be here: the user waits for the whole answer regardless of how fast it was generated.

const started = performance.now();
let chunks = 0;

const stream = await client.chat.completions.create({
  model: "openai/gpt-oss-20b",
  messages: [{ role: "user", content: "Explain B-trees in 200 words." }],
  stream: true,
});

for await (const chunk of stream) {
  const delta = chunk.choices[0]?.delta?.content;
  if (delta) {
    chunks++;
    process.stdout.write(delta);
  }
}

const seconds = (performance.now() - started) / 1000;
console.log(`\n~${Math.round(chunks / seconds)} chunks/sec`);
Swapping providers without a rewrite

The two SDKs share a surface, so the switch is where you build the client and nowhere else.

import Groq from "groq-sdk";
import OpenAI from "openai";

function buildClient() {
  if (process.env.USE_GROQ) {
    return [new Groq({ apiKey: process.env.GROQ_API_KEY }), "openai/gpt-oss-20b"];
  }
  return [new OpenAI(), "gpt-5.4-mini"];
}

const [client, model] = buildClient();

const response = await client.chat.completions.create({
  model,
  messages: [{ role: "user", content: "Explain a database index." }],
});
console.log(response.choices[0].message.content);

QUESTIONS

Which languages are the Groq examples in?

Python and JavaScript. Every sample runs as pasted and reads its credentials from an environment variable — no key is ever written into the code.

Which package do I install for Groq?

The samples use groq. The install step on each language tab gives the exact command.

Which Groq model do these examples use?

openai/gpt-oss-20b, checked against the official documentation on 25 September 2026. Model ids change often; if a call returns a model-not-found error, check the current list in the official docs.

Where is the official Groq documentation?

https://console.groq.com/docs — it is linked at the bottom of this page, and it is the authority whenever it disagrees with a sample here.

ALSO IN LANGUAGE MODELS

The authority

Official Groq documentation

Model names, pricing and rate limits change faster than any page here can. When this page and the official docs disagree, the official docs are right.

Read the docs ↗

https://console.groq.com/docs

New drops, every week

Free tools & Claude templates, landing regularly.

We keep shipping new free tools, code recipes and Claude Code templates. Follow @freeyantra on Instagram or join the newsletter so you never miss one.

Follow @freeyantra

or get them by email

Double opt-in. Unsubscribe any time. Privacy