// What's Next · Done · The Associate Track
Working with the OpenAI API
Completed Nov 03, 2026 · 3 hrs
An introduction to the OpenAI API: API keys, endpoints, cost, and calling chat models from Python, then controlling output with tokens and temperature. Covers shot prompting, chat roles, system messages, assistant examples, and building multi-turn conversations.
Getting started with the API
- API key: A unique secret key used to authenticate requests to the OpenAI API.
- endpoints: The URLs for different model capabilities; the chat completions endpoint sends a conversation and returns a response.
- API Usage cost: The price of a request, which depends on the model requested and the size of input and output.
- tokens: Units of one or more characters that language models use to understand and interpret text.
Calling the API with Python
- OpenAI's python library: The package used to interact with the OpenAI API from Python.
- OpenAI API client: Created with the API key, it configures the environment for communicating with the API.
- chat completions endpoint: Called with client.chat.completions.create() to send messages and get a model response.
- ChatCompletion object: The response object with attributes like id, choices, created, model, object, and usage; the message text is under choices[0].message.content.
Controlling the response
- max_completion_tokens: An upper limit on the number of tokens the model will output, used to shorten or lengthen responses.
- temperature: A control on determinism, set to 1 by default and ranging from 0 (highly deterministic) to 2 (very random).
Prompting and roles
- shot prompting: Including examples in a prompt to guide the model's response, as zero-shot, one-shot, or few-shot.
- system message: A prompt that outlines the role of the model and can add guardrails restricting what it may generate.
- user role: Provides an instruction or prompt to the assistant.
- assistant role: Attached to model responses, and also used by developers to provide example answers that steer the desired style and tone.
Multi-turn conversations
- messages list: A list of dictionaries with role and content, often starting with the system role, that represents the conversation.
- single-turn tasks: Tasks with one input and one output, where the model uses only the system message and user prompt.
- conversation history: Stored assistant responses appended back into messages so each new user message carries the full context.
// Module notes
Getting started with the API
Requests reach models through one unique secret API key, which authenticates every call. Endpoints are the URLs for different model capabilities, and the chat completions endpoint is the one used here. Usage cost depends on the model requested and the size of input and output, billed per million tokens. To get started: create an account, open the API keys page, and create a new secret key. Get the key in place first, since every later step depends on it.
Calling the API from Python
Three lines get a Python call working: install OpenAI's Python library, import the OpenAI class, and instantiate a client with the API key to configure the environment. Then client.chat.completions.create() sends the request, taking a model and a messages list of dictionaries with role and content. The reply comes back as a ChatCompletion object exposing id, choices, created, model, object, and usage. The text itself sits at response.choices[0].message.content, so reach it one attribute at a time.
Controlling output and estimating cost
Two dials decide what comes back, and both are measured in tokens, the units of one or more characters models use to interpret text. max_completion_tokens caps how much the model outputs, while temperature (0 to 2, default 1) runs from highly deterministic to very random. Estimate cost as input_tokens times input_token_price plus output_tokens times output_token_price, assuming output tokens equal max_completion_tokens and dividing the per million prices down. Estimate before deploying, since longer outputs add up fast.
Transforming and generating text
Chat models do two jobs here: transform text through editing and summarization, and generate brand-new content. For editing, triple quotes define a multi-line prompt, and f-strings insert variables like a chat transcript into a prompt asking for a concise summary. For new content, write a clear and detailed prompt: a tagline for an electric car, or a product description built from features, tone, and target audience. Then tweak temperature and max_completion_tokens to iterate.
Roles, prompting, and multi-turn conversations
Examples guide the model before you ask anything: shot prompting comes as zero-shot, one-shot, and few-shot. Messages then carry three roles, system, user, and assistant. The system role sets behavior and adds guardrails against misuse, while assistant messages supply structured examples that steer style and tone.
- Single-turn tasks have one input and one output, drawing on the system message and user prompt.
- For multi-turn conversations, append each assistant response to messages so the model keeps the full history.
In plain terms
Talking to the OpenAI API is like ordering at a very literal restaurant: your API key is the reservation, the chat completions endpoint is the kitchen, and the bill arrives per million tokens. A system message sets the house rules, example messages set the recipe, and temperature plus max_completion_tokens decide how adventurous and how large the dish comes out.
Worth sitting with
- When a system message and an assistant example pull in different directions, which one does the model follow, and how would you test it?
- Temperature and max_completion_tokens both shape the reply, so when an answer feels wrong, which dial should you turn first and how do you know?
- Conversation history lets the model remember earlier turns, so where does that approach start to strain as the exchange grows longer?
// Where this fits
The concepts above are the ones this module turns on, and the notes below explain each in turn. The course page carries the whole track, and the certificate page carries the plan it belongs to.