Calling AI APIs from Python
Until now you have been using ChatGPT, Claude, and Gemini through their web interfaces. The web is fine for one-off help. But the moment you want to process 100 customer reviews automatically, summarize a folder of PDFs, or build a small chatbot of your own, you need to call the AI from inside your Python code.
This is the lesson where Python stops being just a data tool and becomes a way to build things that use AI.
What You'll Learn
- How to call the OpenAI and Anthropic APIs from Python
- How API keys, billing, and rate limits work
- A simple project: classify movie reviews as positive or negative using an LLM
- How to keep API costs under control while you experiment
API vs Web Chat
When you use ChatGPT in the browser, you are using OpenAI's hosted product. When you call the API, you are talking to the same models — gpt-4, gpt-4o, o3 — but as a service, with code you control.
The same is true for Anthropic (claude-opus-4-7, claude-sonnet-4-6, claude-haiku-4-5) and Google (gemini-2.5-pro, gemini-2.5-flash).
You pay per token (a token is roughly 0.75 words). Costs vary, but most beginner projects in this course will cost under one US dollar in total.
Setting Up an API Key
For OpenAI:
- Go to
platform.openai.com/api-keysand sign in - Click Create new secret key
- Copy the key (you will not be able to see it again)
- Add at least 5 USD of credit at
platform.openai.com/account/billing
For Anthropic, similar steps at console.anthropic.com.
Never paste your key into a Colab cell that you might share. Anyone who sees the cell can use your key on your billing account. The safe pattern is environment variables or Colab secrets.
In Colab: click the key icon in the left sidebar, add a secret named OPENAI_API_KEY, and paste your key. Then in code:
from google.colab import userdata
import os
os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_API_KEY")
The key never appears in the notebook itself.
Your First API Call (OpenAI)
Install and import the client:
!pip install openai
from openai import OpenAI
client = OpenAI() # picks up OPENAI_API_KEY from environment
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful Python tutor."},
{"role": "user", "content": "Explain what a list comprehension is in two sentences."},
],
)
print(response.choices[0].message.content)
That is the entire pattern. The messages list is a conversation: the system message sets the AI's role, and user messages are what you say. The response comes back as a string in response.choices[0].message.content.
Use gpt-4o-mini for learning. It is cheap and fast.
Your First API Call (Anthropic)
!pip install anthropic
import anthropic
client = anthropic.Anthropic() # picks up ANTHROPIC_API_KEY
response = client.messages.create(
model="claude-haiku-4-5",
max_tokens=400,
system="You are a helpful Python tutor.",
messages=[
{"role": "user", "content": "Explain what a list comprehension is in two sentences."},
],
)
print(response.content[0].text)
Slight differences: Anthropic puts system as a separate parameter (not inside messages), and the response is in response.content[0].text. Otherwise the pattern is the same.
A Real Mini-Project: Classify Movie Reviews
Build a classifier that decides whether a review is positive or negative. No machine learning training required — the LLM is the classifier.
from openai import OpenAI
client = OpenAI()
reviews = [
"Absolutely loved this film. Best of the year.",
"I want my two hours back. Avoid.",
"It was okay, not great, not terrible.",
"The cinematography was stunning, but the plot was thin.",
]
def classify(review):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Classify each review as POSITIVE, NEGATIVE, or MIXED. Reply with just the one word."},
{"role": "user", "content": review},
],
temperature=0,
)
return response.choices[0].message.content.strip()
for review in reviews:
label = classify(review)
print(f"{label:10} {review}")
You should see something like:
POSITIVE Absolutely loved this film. Best of the year.
NEGATIVE I want my two hours back. Avoid.
MIXED It was okay, not great, not terrible.
MIXED The cinematography was stunning, but the plot was thin.
The temperature=0 makes the output more deterministic — the same input gives the same output. Use that whenever you want consistent classifications instead of creative writing.
Cost Control While Learning
A few habits that keep your bill near zero:
- Use the cheapest model first.
gpt-4o-mini,claude-haiku-4-5, andgemini-2.5-flashcost a fraction of the flagship models. Move up only if you are sure quality is the bottleneck. - Set
max_tokens. Cap how long the response can be. A 200-token response cannot bankrupt you. - Print before processing in bulk. If you are about to loop over 1,000 inputs, run on 5 first and verify the output before running on all 1,000.
- Set a hard spending limit in your dashboard. Both OpenAI and Anthropic let you set a monthly cap. Set it to 5 or 10 USD until you trust your code.
A 1,000-review classification job with gpt-4o-mini typically costs under 50 cents.
Adding Memory: Multi-Turn Conversations
A single API call has no memory. To simulate a conversation, you keep the message history yourself and pass it back each time:
messages = [{"role": "system", "content": "You are a helpful Python tutor for beginners."}]
def chat(user_message):
messages.append({"role": "user", "content": user_message})
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply
print(chat("What is a Python list?"))
print(chat("Now show me a list comprehension that filters even numbers."))
The second turn knows about the first because you sent the whole history. This is how chatbots work — they keep adding to a messages list each turn.
Where to Go from Here
Once you can call an API and process its output, you can build:
- A spam-or-not classifier for your inbox (read emails with the Gmail API, classify with the LLM)
- A content tagger for a blog (read posts, suggest tags, write back to a CSV)
- A custom AI assistant tuned to your job (system prompt with your role, tools, and reference docs)
- A retrieval-augmented chat (load PDFs, answer questions about them — the topic of LangChain and similar libraries)
These build on the same messages pattern you learned above.
Caution: Sensitive Data
Do not paste customer data, employee data, or anything regulated into a third-party API without checking your company's policy. Most APIs let you opt out of training on your data, but the data still travels over the network. For sensitive workflows, run a local model with ollama or a private cloud deployment.
For learning and personal projects, public datasets and your own writing are perfectly safe.
Key Takeaways
- API calls give you programmatic access to the same models as the chat apps
- Always store keys in environment variables or Colab secrets — never in the notebook itself
- Use cheap models (
gpt-4o-mini,claude-haiku-4-5) for learning and prototyping temperature=0for consistent classification; higher for creative writing- For multi-turn conversations, you append to a
messageslist and resend it - Set a spending cap in the API dashboard before running anything in bulk

