I listened to all of the episodes of the Data Skeptic podcast. Yeah. That was a lot.
One recent highlight: Kyle suggested that we—the listeners—get programmatic access to ChatGPT.
He meant API access. He strongly implied that if you are serious about using this powerful new tool, you shouldn’t dick around—pardon the expression—typing at the prompt.
I am taking this to heart. I have been doing more and more prompting, but it is time to take it to the next level.
Just the “Hello World from ChatGPT” of API access will do for now. Not a bad way to spend a Saturday night. Well—ok—it is probably a terrible way to spend a Saturday night. But I don’t need to impress anyone.
So I typed into my ChatGPT window the sloppy question, “How to connect to chatgpt with an api”
It boiled down to just knowing where to go to get started. You have to go to openai.com and get to their API area and generate a key. Then you can select some connect code. I selected python.
You have to install openai in your python environment. They said to “pip install openai” but I wanted to try “conda install openai” first.
I usually feel better doing conda installs. I don’t know if that is justified or not, but it’s how I feel.
But before I do a “conda install” and mess with my carefully curated python environment of choice, I’d like to make a copy of my conda env.
So I cloned my conda environment. I habitually clone my conda environment before installing a package. I just do something like:
conda create --name myclone_todays_date --clone myenv
Ok. Great. Now conda install openai worked.
Now just test the python code they generated for me with my API key hardcoded right in there.
python connect_chatgpt_api.py
…openai.RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}
Well. I’m not surprised.
Let’s go back to the page where I got my key.
I guess I should click “Continue”
Well. That takes me to a page where they want me to buy credits. Ok. I guess I’ll buy $5 worth of credits. Enter credit card… done!
Now it should work.
Nope. Insufficient quota. Ugh.
So I typed into a ChatGPT prompt my question, “I bought $5 worth of credits but I don’t seem to be able to use it on the ChatGPT API”
The first two steps it gave me back helped:
1. Check Your API Billing Status
Go to Billing Settings and confirm:
- Your $5 credits appear under “Current balance”.
- Your usage is being deducted from prepaid credits (and not a separate expired trial balance).
I clicked the “Billing Settings” link and saw my $5. So far-so good.
2. Verify Your Usage & Quota
Check your usage and quota limits at Usage Dashboard:
- Ensure you haven’t hit any limits.
- Some accounts have spending limits (e.g., $0/day if not adjusted).
Ah. My plan says $0. That’s gotta be it.

Let’s hit that green “Increase limit” button and see if that does it.
Hmmm. I’m taken to a kind of an annoying page with all sorts of rate limits and warning and such… Looks like I’m in “Usage Tier 1”.
And the enabled budget limit is defaulting to $120. I guess that’s ok. I’ll just accept that.
Let’s do it. Click “Save”
Now let’s run the code, which BTW, looks mostly like this:
from openai import OpenAI
client = OpenAI(
api_key="wouldnt-you-like-to-know"
)
completion = client.chat.completions.create(
model="gpt-4o-mini",
store=True,
messages=[
{"role": "user", "content": "write a haiku about ai"}
]
)
print(completion.choices[0].message);
Yay. I got a response with a stupid little haiku about ai in the response:
ChatCompletionMessage(content='Silent thoughts emerge, \nWoven dreams of circuits sing, \nMind of light and code.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None)
Leave a Reply