Skip to main content

GPT-4.1 Python quickstart using the OpenAI API

Getting set up and running GPT-4.1 on your machine in Python using the OpenAI API.
Created on May 5|Last edited on May 5
Like everyone else, I was excited to jump right in and start playing around with GPT-4.1 as soon as I heard about it in the announcement from OpenAI on April 14th. I figured you might be too, and so I put together this quickstart - a replacement for GPT-4.5 which we heard will be deprecated in the same announcement.
💡
Getting GPT-4.1 up and running via the OpenAI API opens up a world of possibilities beyond what you’ve experienced with ChatGPT or other interfaces. In this quickstart guide, we'll walk you through the setup in just a few steps so you can start experimenting in about 5 minutes.
If you already have your OpenAI API key, you can ...
Jump to the tutorial


I've also created a handy Colab for those who want to jump right to seeing it in action.

Here's what we're covering:

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 begin using GPT-4.1, you'll need an API key to access it.
To get one, simply head over to OpenAI's product page, and click "Start building".

Choose your signup method (I chose Google) and run through the verification process. You'll need access to a phone that can receive texts for this step.
Once that's complete, you'll head over to create an API Project key (which have replaced user keys). This is so OpenAI can connect your scripts to your account. It's important that they know who's doing what and determine if and how much they should charge you for what you're doing.

Creating your OpenAI API key

If you haven't yet created a project for your API keys, you'll want to do that now. For our purposed here I just have a single project "Scripts" for these applications.
To create a project just "Default project" in the top left, and then "Create project".


To create your key, You'll just head to your Dashboard, and click "API keys".

...and then you'll create your key.

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 (April 14, 2025) the pricing for GPT-4.1 is:
Source

W&B Weave

W&B Weave enhances our project by offering a streamlined way to track and evaluate model outputs. To use Weave, you start by importing it and initializing it with your project name.
The key feature is the @weave.op() decorator, which you add above any function you want to track. A decorator in Python is a special function that adds functionality to another function. By adding @weave.op() above your function definition, you instruct Weave to automatically log the inputs and outputs of that function. This makes it easy to monitor what data goes into the function and what results come out.
After running your code, you can view these logs in the Weave dashboard, which provides detailed visualizations and traces of the function calls. This helps in debugging and organizing your experimental data, making the development process with GPT-4 more efficient and insightful.

Getting started with GPT-4.1

The screenshots in this tutorial will show me using Jupyter Notebooks.
If you've never used Colabs or Jupyter before there's a nice quickstart here. And a refresher on key markdown here.
One of the great things about Jupyter Notebooks is that, like Colabs, you can add your comments and context in markdown fields:

For the content and context, I'm simply going to post what I am writing here as a description of what the code does. If you're working along, obviously exclude that or add your own version.

Step 1: The OpenAI GPT-4.1 API key

The first thing we need to do is define our GPT-4.1 API key.
The code to do this is:
%env OPENAI_API_KEY=KEY
You'll want to replace "KEY" with your OpenAI API.
When you run the cell you'll get your key back as the output. This confirms that it's been successfully entered as an environment variable.


Step 2: Installing OpenAI and W&B Weave

To make GPT-4.1 work, you only need OpenAI. But below we'll be showing you how you can effortlessly track your inputs and outputs using 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.
This isn't as important in the context of this quickstart, but as you start using GPT-4.1 and other models for more advanced functions, you'll be glad you started off on the right foot.
Fun Fact: OpenAI was the first paying customer of Weights & Biases!
💡
The code to do this is:
!pip install openai weave
and then run the cell.
When you run the Jupyter cell you'll notice that the space between the [ ] gets filled with an asterix ([*]). This means the cell is running, and you need to wait for the * to turn into a number before continuing.
💡

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

The next step is to import the libraries we'll need to make magic, as well as pass the API key across to OpenAI so we have access to GPT-4.1.
In this block, we'll import the OpenAI and W&B libraries we've already installed.
import os
import weave
from openai import OpenAI

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

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

Step 4: Setting your GPT-4.1 prompt

If you're used to using ChatGPT and don't regularly define your assistant, it's a great technique. Starting your prompts with something like, "You are an expert SEO who only uses whitehat tactics ..." helps the model frame their output.
This is a necessary step when using the OpenAI API for GPT-4.1.
💡
The following code will generate inputs you can fill in. They will ask for:
  • Define the assistant's role. This sets up the model's identity by specifying how it should behave. For example, the input to "Who should I be, as I answer your prompt?" could be something like "a machine learning engineer learning about LLMs," guiding the model to respond accordingly.
  • Set the user’s task. This defines what you want the model to do. For instance, the input to "What do you want me to do?" could be a specific request like "Explain how to use Weights & Biases in my code to work more effectively with my team." which informs the model of the action it needs to take.
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-4.1

And now all that's left is to generate the content.
First we'll define the functions to do this, and run it.
@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-4.1 inputs and outputs in W&B Weave

You'll notice I added the @weave.op() above the generate_content function, which adds W&B Weave which logs result inputs and outputs for the function which it is added to. After running the script, we can navigate to our project page or the link at the top of the output this will take you right to the latest Trace.

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

In just a few steps, you now have GPT-4.1 running via the OpenAI API in Python and integrated it with W&B Weave for seamless tracking and visualization. Whether you're fine-tuning prompts or managing inputs or outputs, this setup empowers you to take full control of your generative AI projects.
Keep experimenting, adjusting parameters, and exploring new possibilities with GPT-4.1. For further insights and optimizations, check out the related resources below, and feel free to dive deeper into the world of AI-driven content creation.

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.