Stop 13 · Choose a Path: Web, Data or AIData & AI · 6/6

Calling AI Model APIs (OpenAI)

Part of the Python Roadmap

In short

Most AI features you'll build start with one HTTP call to a hosted model. The OpenAI API is the one most tutorials use, so learning it well makes every other provider easier.

Hosted AI models are used from Python through an SDK or HTTP API: you send a prompt as messages and get text back. Learning one provider's API, like OpenAI's, makes it easy to work with others.

The blob talks happily on a phone inside a small roadside phone booth while a meter ticks beside it.

Think of it like this

The old PCO booth

At a PCO booth you didn't own the phone line. You walked in, made your call and paid by the minute on the meter. The OpenAI API works the same way: you don't own the model, you send a request and pay for the tokens you use.

Watch it happen

One request to the Responses API

Your app
OpenAI API

POST /v1/responses

1/4Your app sends the model name, instructions and input, with your API key in the Authorization header.

The big ideas

What you need to know

API keys are secrets

Keep the key in an environment variable on the server. Never ship it inside a frontend or mobile app.

Instructions plus input

Instructions set the model's behaviour. Input is the user's request. Keep them separate so users can't easily override your rules.

You pay per token

Both input and output tokens cost money, and output usually costs more. Short prompts and length limits save real money.

Pick the smallest model that works

Smaller models are cheaper and faster. Test on your real task before reaching for the biggest one.

See it in code

Walk through the code

python
1from openai import OpenAI2 3client = OpenAI()  # reads OPENAI_API_KEY from the environment4 5response = client.responses.create(6    model="gpt-5-mini",7    instructions="You are a friendly tutor for Indian college students.",8    input="Explain Big O notation with a chai stall example.",9)10print(response.output_text)11print(response.usage.input_tokens, response.usage.output_tokens)

1/3The client picks up your key from the environment. No key in the code.

A basic call with the official Python SDK. Model names change often, so check the docs for the current list.

Save yourself the pain

Common mistakes

Oops

Putting the API key in React code so the browser calls OpenAI directly.

Do this instead

Call the API from your own backend. Anyone can read a key shipped to a browser and spend your money.

Oops

Never looking at usage until the bill arrives.

Do this instead

Log input and output tokens for every call and set a monthly budget alert in the dashboard.

Your mission

Your first metered call

~20 min

Quick check

Where should your OpenAI API key live in a web app?

Go deeper

  1. 1Store your API key in an environment variable and make one simple request.
  2. 2Use a system message to control the tone and format of answers.
  3. 3Handle errors and rate limits, and keep an eye on token usage and cost.

More data & ai at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge