Installation and setup
The setup for this course is three Python packages and one free API key: groq to call the model, tiktoken to count tokens, pydantic to check answers, and a Groq key.
Last updated: 30 Sep, 2026 · groq 1.7 · gpt-oss-120b on Groq
You need Python 3.10 or later. Check yours with python --version. Nothing runs a model on your computer: the model runs on Groq, and your code sends it text and gets text back.
Installing groq, tiktoken and pydantic
pip install "groq==1.7.0" "tiktoken==0.14.0" "pydantic==2.13.5"- groq is Groq's Python client. It sends a list of messages to a model and returns the reply.
- tiktoken is OpenAI's tokenizer library. The first time you use a tokenizer it downloads that tokenizer's vocabulary, a file of a few megabytes, and keeps it in a cache.
- pydantic checks that data has the shape you expect. Structured output uses it on the model's answers.
Getting a Groq API key
Sign in at console.groq.com/keys with an email address or a Google account, choose Create API Key, give it a name and copy the key. It starts with gsk_. The free plan limits how many requests and tokens you can send per minute and per day; each example here sends a few short requests.
Setting GROQ_API_KEYRequired
export GROQ_API_KEY=gsk_...Groq() reads GROQ_API_KEY from the environment when you create the client, so the key never appears in your code. export and $env: last until you close the terminal.
Checking the install and the key with one call
Syntax: client.chat.completions.create(model=..., messages=[...]) sends one request and returns one response.
from importlib.metadata import version
from groq import Groq
print("groq", version("groq"))
client = Groq() # reads GROQ_API_KEY from the environment
response = client.chat.completions.create(
model="openai/gpt-oss-120b",
messages=[{"role": "user", "content": "Reply with the single word: ready"}],
)
print(response.choices[0].message.content)groq 1.7.0 ready
- groq 1.7.0 is the installed version of the client.
- ready is gpt-oss-120b's reply, so the key works and the model answered.
Running without the key
If the variable is not set in the terminal or notebook that runs the code, the client refuses to start:
from groq import Groq
client = Groq()Traceback (most recent call last):
File "main.py", line 3, in <module>
client = Groq()
groq.GroqError: The api_key client option must be set either by passing api_key to the client or by setting the GROQ_API_KEY environment variableThe error names both fixes: set GROQ_API_KEY, or pass api_key= to Groq(). Setting the variable is the safer one, because the key stays out of files you might share.
pip vs uv vs Colab
| pip | uv | Colab | |
|---|---|---|---|
| Installs into | The active Python environment | The project's own environment | The notebook's runtime |
| Key set with | export or $env: | export or $env: | Secrets and userdata.get |
| Lasts | Until you uninstall | In the project | Until the runtime restarts |
Related
- Previous: LLM Fundamentals overview
- Next: Choosing a model
- Reference: Groq quickstart
- Change
readyin the prompt to another word and run the check again. - Print
response.modelafter the call to see which model answered. - Open a new terminal without setting the key, run the check, and read the
GroqError.
Little by little, you're building something great.