> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lab Walkthrough Your First API Call

> Hands-on Python lab showing how to make your first OpenAI API chat request, run a minimal script, set API key securely, and experiment with temperature

You've reviewed the code — now run it yourself against a live model. This short hands-on lesson walks through executing a minimal Python program that calls an LLM via the OpenAI SDK using a real API key from your terminal. No simulations: your code, your credentials, and a real model responding.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/z7NmHsFQN9LCEiD0/images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/Lab-Walkthrough-Your-First-API-Call/retro-neon-your-first-api-call.jpg?fit=max&auto=format&n=z7NmHsFQN9LCEiD0&q=85&s=952533f118a605079839015f5534da73" alt="A retro-style black banner with neon text that reads &#x22;YOUR FIRST API CALL&#x22; and the subtitle &#x22;Run it yourself — for real.&#x22; A small green button below says &#x22;FOR REAL&#x22; and there's a &#x22;HANDS-ON LAB&#x22; label above." width="1920" height="1080" data-path="images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/Lab-Walkthrough-Your-First-API-Call/retro-neon-your-first-api-call.jpg" />
</Frame>

Environment and prerequisites

* Python 3.11 is already installed and your project's virtual environment is active.
* Install the official SDK:

```bash theme={null}
pip install openai
```

<Callout icon="lightbulb" color="#1CB2FE">
  It's safer to keep your API key out of source files. Use an environment variable such as `OPENAI_API_KEY` and load it from `os.environ` instead of pasting the key in code.
</Callout>

Create and run the example

1. Create a file named `hello_llm.py`.
2. Paste the short program below, save the file, and run it with `python3 hello_llm.py`.

This example demonstrates the minimal flow:

* Create an SDK client
* Send a chat completion request
* Print the returned message

```python theme={null}
from openai import OpenAI

# Step 1: Create a client (replace the string with your key or use an environment variable)
client = OpenAI(api_key="YOUR_API_KEY")

# Step 2: Send a message
response = client.chat.completions.create(
    model="gpt-4o",
    temperature=0.7,
    messages=[
        {"role": "system", "content": "You are a helpful assistant. Be concise."},
        {"role": "user", "content": "What is an LLM?"}
    ]
)

# Step 3: Print the response
print(response.choices[0].message.content)
```

Explanation of key steps

* client creation: `OpenAI(api_key="...")` — creates an authenticated client instance.
* `model`: selects which model to use (here `"gpt-4o"`).
* `temperature`: controls randomness (0.0 = deterministic, higher = more diverse).
* `messages`: the chat-style conversation (system -> assistant behavior, user -> request).
* `response.choices[0].message.content`: the assistant's returned text.

Run it from your terminal:

```bash theme={null}
root@controlplane ~/project via 🐍 v3.12.3 (venv) $ python3 hello_llm.py
An LLM (Large Language Model) is a neural network trained on massive text data to understand and generate human language.
root@controlplane ~/project via 🐍 v3.12.3 (venv) $
```

Parameters reference

| Parameter | Purpose | Example |
| - | - | - |
| `model` | Which model to invoke | `gpt-4o` |
| `temperature` | Controls output randomness; `0.0` is deterministic | `0.7` |
| `messages` | Chat-style input array (system, user, assistant) | `[{ "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "What is an LLM?" }]` |
| `response` path | Where generated text appears | `response.choices[0].message.content` |

Change the prompt and experiment with temperature

1. Modify the `user` message to:
   * `"Write a one-sentence story about a robot."`
2. Set `temperature` to `0.0` and run the script twice. With `0.0` the model is deterministic (the same prompt returns the same output):

```bash theme={null}
# After setting temperature=0.0
root@controlplane ~/project via 🐍 v3.12.3 (venv) $ python3 hello_llm.py
A lonely robot discovered a forgotten garden and learned to grow flowers.
root@controlplane ~/project via 🐍 v3.12.3 (venv) $ python3 hello_llm.py
A lonely robot discovered a forgotten garden and learned to grow flowers.
```

3. Set `temperature` to `1.0` and run multiple times to see more diverse outputs:

```bash theme={null}
# After setting temperature=1.0
root@controlplane ~/project via 🐍 v3.12.3 (venv) $ python3 hello_llm.py
A lonely robot discovered a forgotten garden and learned to grow flowers.
root@controlplane ~/project via 🐍 v3.12.3 (venv) $ python3 hello_llm.py
A robot baked a soufflé on Neptune's rings while dreaming of electric sheep.
```

Summary: temperature controls randomness — `0.0` = deterministic (repeatable), higher values = more varied creativity.

Security reminder

<Callout icon="warning" color="#FF6B6B">
  Do not commit API keys to source control. Use environment variables (for example, `OPENAI_API_KEY`) or a secrets manager and never paste keys into public repositories.
</Callout>

Use an environment variable (recommended)

If you prefer to load the API key from the environment instead of hardcoding it, update your client creation like this:

```python theme={null}
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
```

Set the environment variable in your shell before running:

```bash theme={null}
export OPENAI_API_KEY="sk-..."
python3 hello_llm.py
```

Further reading and references

* OpenAI Python SDK documentation: [https://platform.openai.com/docs/api-reference](https://platform.openai.com/docs/api-reference)
* Learn more about temperatures and sampling: search for "temperature sampling in language models"

You've now written and executed your first LLM program locally. Concepts demonstrated here — tokens, prompts, temperature, and context windows — are the foundations you'll build on for more advanced use cases.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/ai-agents-for-beginner-openclaw-case-study/module/13d4f7ad-29e5-4bc0-b026-47c4ae43c31c/lesson/42e75e6b-d463-4664-906a-c6c9307a2807" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.