Short answer
Create an OpenAI API key, store it in an environment variable, install the openai Python library and send gpt-3.5-turbo a list of messages that starts with a system message; the reply comes back with the answer and a count of the tokens you paid for. A short exchange costs a fraction of a cent at the current price of $0.0015 per 1,000 input tokens and $0.002 per 1,000 output tokens.
Follow-up question Who should use the API instead of the chat app?
The ChatGPT website is a finished app. The API, short for application programming interface, is the same family of models offered as a service your own code can call. Instead of typing into a chat box, your program sends a request over the internet and gets a reply back as data. That small shift is what lets you build ChatGPT into a spreadsheet tool, a help desk or a script that processes a hundred documents while you get coffee.
This guide takes you from nothing to a working call in Python. You need a computer with Python 3 installed, an OpenAI account, and about twenty minutes.
Step 1: get a key and keep it safe
Sign in at platform.openai.com, open the API Keys page and create a new secret key. An API key is a password for your code: it identifies your requests and is billed to your account, so treat it that way. OpenAI's own safety advice is clear on three points. Never paste it into code you share or commit to a repository, give each person on a team their own key, and if a key leaks, delete it and make a new one right away.
The recommended home for the key is an environment variable, a named value your operating system keeps for programs to read. On a Mac or Linux machine, add this line to your ~/.zshrc (or ~/.bash_profile) file and open a new terminal window:
export OPENAI_API_KEY="paste-your-key-here"
Then install the library:
pip install openai
Step 2: send your first request
Save this as first_call.py:
import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a friendly cooking coach. Answer in under 100 words."},
{"role": "user", "content": "I have eggs, spinach and feta. What can I make for lunch?"},
],
temperature=0,
)
print(response["choices"][0]["message"]["content"])
print(response["usage"])
Run it with python3 first_call.py. Three parts of the request matter. model picks which model answers. messages is the conversation so far, where each message has a role: system for your standing instructions, user for the person's words and assistant for earlier replies. temperature controls how much the wording varies between runs; OpenAI's own examples use 0 to keep answers steady while you test.
Step 3: read what comes back
The response is a bundle of fields. The answer sits in choices[0].message.content. Next to it, finish_reason says stop when the model ended naturally, or length when it ran out of room, which is your cue to allow a longer reply or shorten the request. The usage field reports three numbers: prompt tokens, completion tokens and the total. Tokens are the small chunks of text models read and write; OpenAI's rule of thumb is that 1,000 tokens is about 750 English words.
The API does not remember anything between calls. To continue the conversation, add the model's reply as an assistant message, add the next user message, and send the whole list again.
What it costs: a worked estimate
Prices are per 1,000 tokens, and input and output are priced separately. Suppose the usage field for the lunch question shows the following (illustrative numbers):
| Part | Tokens | Price per 1,000 | Cost |
|---|---|---|---|
| Prompt (system + user) | 50 | $0.0015 | $0.000075 |
| Reply | 120 | $0.002 | $0.00024 |
| Total | 170 | $0.000315 |
That is about three hundredths of a cent. A thousand similar questions would cost roughly 32 cents. For comparison, GPT-4 with an 8K context costs $0.03 per 1,000 input tokens and $0.06 per 1,000 output tokens, twenty times or more the gpt-3.5-turbo rate, so start with gpt-3.5-turbo and switch only where you need the extra quality. New accounts get $5 of free credit to use within three months, and every account has a spending limit that OpenAI raises over time, or on request.
Try it: Change the system message in first_call.py three times (a cooking coach, a strict editor, a children's science teacher), keep the same user question, and compare the replies and the usage numbers. Then work out the cost of each run with the table above.
Who should use the API instead of the chat app?
For most everyday questions, the chat app is the better tool: it is ready to use, keeps your history and needs no code. The API makes sense when you need something the app cannot give you.
The first case is repetition. If you want to summarize 300 customer reviews or label 1,000 product descriptions, a short script that loops over a file beats pasting each one into a chat window. The second is building something for other people, such as a help bot on your website or a writing feature inside your own app, where users never see ChatGPT at all. The third is control: a fixed system message, a set temperature and a predictable format that you can test and repeat.
The API also asks more of you. You pay for every token, you must guard your key, and you are responsible for what your app shows its users. If that trade looks worth it, your first call is the right place to start: small, cheap and easy to change.
Sources
- Pricing — OpenAI, June 28, 2023
- How to format inputs to ChatGPT models — OpenAI Cookbook on GitHub (commit of 2023-03-02), March 2, 2023
- Best Practices for API Key Safety — OpenAI Help Center