Skip to main content

GPT-4o Python quickstart using the OpenAI API

Getting set up and running GPT-4o on your machine in Python using the OpenAI API.
Created on March 18|Last edited on May 5
Launching GPT-4o through the OpenAI API gives you programmatic access to one of the most advanced multimodal models available. In this guide, you’ll see how to configure your environment, install required packages, and run a simple Python script to generate content—all in under five minutes. We’ll also show you how to capture inputs and outputs using Weights & Biases Weave.
Jump to the tutorial

You can find our quickstart on the newer GPT-4.1 here.
💡

Table Of Contents


If you're just getting started and don't yet have your machine set up to run Python, I've created a quick tutorial here that will have you up-and-running in just a few minutes.
💡
Once you're set-up, you're going to need an OpenAI API. So let's start there:

Getting your OpenAI API

Before you can call GPT-4o, you need a valid API key. Head to OpenAI’s product page and click "Start building" to begin the signup flow.

After you verify your email and phone number, you’ll land in the dashboard where project-level keys have replaced user keys—this lets OpenAI map usage back to your projects and billing.

Creating your OpenAI API key

If this is your first key, create a new project in the dashboard. Click Default project in the top-left, then choose Create project and give it a name like Scripts.


Once you have a project, select API keys, then Create key.


A modal will appear. Choose the scopes you need, copy the key, and store it securely (not in plain text).

This will open a lightbox:


You'll want to decide what permissions you'd like it to have. You can read more about these here.
Don't save your key unless you have someone highly secure (a notepad doc on your desktop is not). Once you're used it momentarily, close the notepad doc without saving.
💡

OpenAI pricing

You get a $5 credit upon signing up, which will get you surprisingly far if you're just experimenting!
As of this writing (May 5, 2025) the pricing for GPT-4o is:



W&B Weave

W&B Weave provides an automatic way to log and visualize model inputs and outputs. By decorating your functions with @weave.op(), every call to that function—including its arguments and return values—will be recorded. You can then explore traces in the Weave UI to debug, compare runs, and share results with your team.

Getting started with GPT-4o

You can follow along in Jupyter Notebook, Colab, or any Python REPL. If you need a quick Python setup, see this tutorial. Otherwise, let’s jump into the code.
One of the great things about Jupyter Notebooks is that, like Colabs, you can add your comments and context in markdown fields:


Step 1: The OpenAI GPT-4o API key

Begin by exporting your OpenAI API key as an environment variable:
%env OPENAI_API_KEY=KEY
Replace KEY with the value yours. When you run this in Jupyter, it will echo back the key, confirming it’s active.


Step 2: Installing OpenAI and W&B Weave

Install both the OpenAI client and W&B Weave:
This is a good time to sign up for Weights & Biases, to save you having to interrupt your workflow in a couple minutes, when you're further along.
Fun Fact: OpenAI was the first paying customer of Weights & Biases!
💡
!pip install openai weave
and run the cell.
Wait for the cell to finish—the [*] indicator will turn into a number.
💡

Now you've installed it, we still need to import it for use
If you're new to Python, basically when we install a library we simply grab the code. When we import it, we make that library available for use.

I read a great explanation once. Installing is like uploading an image to a server. Importing is like embedding that image on a page once it's available.
💡

Step 3: Import libraries and pass the OpenAI API key

Next, load your packages and set up both Weave and the OpenAI client:
import os
import weave
from openai import OpenAI

# Initialize Weave
weave.init('gpt-4o')

# Initialize OpenAI client
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))

Step 4: Setting your GPT-4o prompt

To shape GPT-4o’s responses, you can specify an assistant role and user task interactively:
gpt_assistant_prompt = "You are a " + input ("Who should I be, as I answer your prompt?")
gpt_user_prompt = input ("What do you want me to do?")


Step 5: Generating content with GPT-4o

Here we break content creation into two phases: outlining reasoning steps, then drafting the final output.
The functions below use the @weave.op() decorator so Weave will log everything.
@weave.op()
def generate_content(gpt_assistant_prompt: str, gpt_user_prompt: str) -> dict:
gpt_prompt = f"{gpt_assistant_prompt} {gpt_user_prompt}"
messages = [
{"role": "assistant", "content": gpt_assistant_prompt},
{"role": "user", "content": gpt_user_prompt}
]
response = client.chat.completions.create(
model="gpt-4.1", # Ensure correct model name is used
messages=messages,
temperature=0.2,
max_tokens=1000,
frequency_penalty=0.0
)
response_text = response.choices[0].message.content
tokens_used = response.usage.total_tokens
return {"response": response_text, "tokens_used": tokens_used}

# Call the function
result = generate_content(gpt_assistant_prompt, gpt_user_prompt)
print(result)
You'll notice that we're sending both the assistant and user roles as messages.
Additionally, I'm defining the following in my response criteria:
  • temperature: A value between 0 and 1.0 that controls the randomness or creativity of the model's output. Lower values like 0.2 make the output more focused and deterministic, leading to responses that are less creative but more precise.
  • max_tokens: Limits the maximum number of tokens (words and parts of words) in the response. The model will generate up to 1000 tokens in its reply, balancing between verbosity and performance.
  • frequency_penalty: A value between -2.0 and 2.0 that adjusts the likelihood of the model repeating tokens. A value of 0.0 means there is no penalty, allowing repetition to occur naturally. Higher values would reduce repetition in the generated text.
and
When run, these appear as:

You'll notice I passed some additional parameters with the request. You can change the numbers, and the full set can be found on the OpenAI site here.
The ones I'm using are:
  • model - the model you want to use.
  • message - the message being sent (mostly the prompt).
  • temperature - a number between 0 and 1 with higher numbers being more random. Higher numbers may be more creative, but they also make the results less predictable.
  • frequency_penalty - a number between -2 and 2, where higher numbers penalize new tokens based on their frequency to that point in the response. The higher the number, the lower the probability of repetition.

Viewing GPT-4o inputs and outputs in W&B Weave

After running the final cell, your notebook output will include links to each Trace. Clicking a link opens the Weave dashboard, where you can inspect inputs, outputs, code versions, execution time, and peer comments—all in rich visual form.

You'll see something like the following:


You can also click the tabs in the top right to get additional details including:
The code that generated that Trace:

Feedback on the Trace from your peers or other.
And a summary of the environment that was running:



Conclusion

You now have GPT-4o integrated with the OpenAI API and Weights & Biases Weave in Python. This setup makes it simple to prototype prompts, log results automatically, and collaborate on model-driven workflows. Continue to tweak parameters, explore different prompt patterns, and unlock new use cases.

Dave Davies
Dave Davies •  
Sorry for the delayed reply to all. I changed my account. :) I've now updated it, so all errors should be sorted out.
Reply
Bijan Nikouravan
Bijan Nikouravan •  
More over, I was not able to past API key for logging in to the wandb library from Anaconda Prompt. Thank you if guide me.
Reply
Bijan Nikouravan
Bijan Nikouravan •  
Thank you for present nice blog. I was trying to follow all of your comments but finally I hade some problem. I appreciate if you can guide me in this regard. the error is a s follow (this is only part of error): InvalidRequestError: The model `gpt-4` does not exist or you do not have access to it. Learn more: https://help.openai.com/en/articles/7102672-how-can-i-access-gpt-4.
1 reply
Zzavk
Zzavk •  
nice blog :)
Reply
Iterate on AI agents and models faster. Try Weights & Biases today.