Calling AI model APIs
Now connect Python to an AI model. This module shows how to call an API securely and turn its output into data your code can use.
Learning objectives
- Explain the request–response pattern of AI APIs
- Store and use API keys securely
- Make an API call with an official SDK
- Request and validate structured JSON output
- Handle errors, rate limits and cost
5.1 How AI APIs work
An AI model API is a web service: your code sends a request (your prompt, settings and an API key) and receives a response (the model's output, plus information such as token usage). Providers publish official Python SDKs that handle the details.
5.2 Keep your API key secret
- Never put keys in your code, notebooks you share, or prompts
- Store them as environment variables — or in Colab, use the Secrets panel (key icon)
- Set spending limits in your provider account
- If a key leaks, revoke it immediately
Python · reading a key safelyimport os
# In Colab: from google.colab import userdata; os.environ["ANTHROPIC_API_KEY"] = userdata.get("ANTHROPIC_API_KEY")
assert "ANTHROPIC_API_KEY" in os.environ, "Set your API key as an environment variable first"
5.3 Your first API call
Python · calling a model (Anthropic SDK; OpenAI and Gemini SDKs are very similar)# !pip install anthropic
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
response = client.messages.create(
model="YOUR-MODEL-NAME", # copy a current model name from the provider's docs
max_tokens=300,
system="You are a concise assistant for a UK kitchenware shop.",
messages=[{"role": "user", "content": "Suggest three names for a bamboo utensil set."}],
)
print(response.content[0].text)
print(response.usage) # input and output tokens — this is what you pay for
5.4 A reusable helper function
def ask(prompt, system="You are a helpful assistant.", max_tokens=500):
r = client.messages.create(model="YOUR-MODEL-NAME", max_tokens=max_tokens, system=system,
messages=[{"role": "user", "content": prompt}])
return r.content[0].text
print(ask("Explain a Python dictionary in two sentences."))
5.5 Getting structured output
To use AI output in code, ask for JSON — then validate it (Module 3).
import json
def classify_review(text):
prompt = f"""Classify this customer review.
Return JSON only, with keys: "sentiment" (positive/neutral/negative),
"topic" (product/delivery/price/other) and "summary" (max 10 words).
Review: {text}"""
raw = ask(prompt, system="You return only valid JSON.")
try:
return json.loads(raw)
except json.JSONDecodeError:
return {"sentiment": None, "topic": None, "summary": None, "error": raw[:80]}
print(classify_review("Lovely board but it took two weeks to arrive."))
Many providers also offer structured-output or tool-use features that guarantee valid JSON matching a schema — worth using in production.
5.6 Handling errors and limits
import time
def ask_with_retry(prompt, attempts=3):
for i in range(attempts):
try:
return ask(prompt)
except anthropic.RateLimitError:
time.sleep(2 ** i) # wait 1s, 2s, 4s
except anthropic.APIError as e:
print("API error:", e)
break
return None
Mind the cost
You pay per token, for input and output. Test on a handful of rows before running thousands, set max_tokens, and check response.usage.
Key takeaways
- Your code sends a prompt and key; the API returns output and token usage.
- Keep keys in environment variables or Colab Secrets — never in code.
- Wrap calls in a helper function.
- Ask for JSON, then validate it.
- Retry rate-limit errors with back-off; test small to control cost.