OpenAI API Getting Started Guide 2026

The OpenAI API gives you programmatic access to GPT-4o, GPT-4o-mini, o3, o4-mini, and other OpenAI models from your own applications. In 2026, the API has changed significantly from its 2023 origins — the Assistants API was deprecated and shut down in August 2026, the Responses API is now the recommended interface for new projects, and several new model families have launched. This guide reflects the current state of the API for developers starting or updating projects today.

Update (November 2026): The models named in this guide (GPT-4o, GPT-4o mini, o3 and o4-mini) were common choices earlier in the year. OpenAI’s current lineup is led by the GPT-6 family, with GPT-5.x mini and nano models alongside it, and older models are retired over time. The API patterns here (Responses API, streaming, structured outputs, function calling) still apply, but pick your model from OpenAI’s current models and pricing pages.

What Changed in 2026: The Responses API

The most important architectural change for developers in 2026 is the deprecation of the Assistants API. OpenAI shut down the Assistants API on August 26, 2026. If you are starting a new project, use the Responses API (released in early 2025) rather than the Chat Completions API or the now-defunct Assistants API. The Responses API provides a stateful conversation interface with built-in tool support (web search, code interpreter, file search) and is OpenAI’s current recommended path for building AI-powered applications. The Chat Completions API continues to work for stateless single-turn or manually managed multi-turn conversations and is not being deprecated.

Getting an API Key

Create an account at platform.openai.com and navigate to the API keys section to generate a key. Store the key as the environment variable OPENAI_API_KEY — never hardcode it in source files. Add billing information to enable API access; the ChatGPT Plus subscription does not include API credits. OpenAI offers pay-as-you-go billing charged per token used. Set a monthly spending limit in the billing settings to prevent unexpected charges during development.

Installation and First Call

Install the official Python SDK: pip install openai. Import OpenAI, create a client (it reads OPENAI_API_KEY from the environment automatically), and make your first Responses API call. The response object’s output field contains the model’s text output. For Chat Completions format, call client.chat.completions.create() — the response structure differs slightly but the pattern is the same.

Figure 1 — OpenAI API: which interface to use

Use case Interface Status New projects 2026Responses APICurrent — recommended Stateless chatChat CompletionsMaintained, not deprecated Existing Assistants codeAssistants APIShut down Aug 26 2026

Model Selection in 2026

OpenAI’s model lineup has expanded significantly. For most applications, GPT-4o-mini is the recommended starting point — it is fast, cheap, and capable enough for the majority of text generation, classification, summarisation, and extraction tasks. GPT-4o provides higher reasoning quality for complex tasks. The o3 and o4-mini models (reasoning models) are optimised for mathematics, coding, and multi-step logical problems — they think before responding and produce significantly better results on difficult reasoning tasks than standard GPT models, at higher latency and cost. Use reasoning models specifically for tasks where standard GPT-4o produces unsatisfactory results — do not default to them for all tasks as the cost-to-quality ratio favours GPT-4o-mini for most practical applications.

Streaming

For interactive applications, enable streaming by passing stream=True to the API call. The SDK returns an iterator that yields event objects as tokens generate. For Responses API calls, iterate over client.responses.stream() context manager. For Chat Completions, set stream=True and iterate over the chunks, checking chunk.choices[0].delta.content for each token. Streaming is the standard pattern for any user-facing chat application — displaying text as it generates rather than waiting for the full response feels dramatically more responsive to users even when the total time to completion is the same.

Structured Outputs

Structured outputs guarantee that the model returns valid JSON matching a schema you define. Use the response_format parameter with a JSON schema object, or in the Python SDK use client.beta.chat.completions.parse() with a Pydantic model as the schema. Structured outputs are more reliable than asking the model to “respond in JSON” without a schema — the model is constrained at the generation level to produce valid output matching the schema rather than just instructed to try. This is the recommended approach for any application that parses model output programmatically. Combine with function calling for tool-use patterns where the model needs to both reason about which function to call and return structured parameters.

Rate Limits and Cost Management

OpenAI rate limits vary by model and usage tier. New accounts start at tier 1 with lower limits; limits increase automatically as you spend more over time. Check your current limits in the API dashboard under Usage. For applications expecting sudden traffic spikes, request limit increases proactively before launch. The most effective cost management strategies: use GPT-4o-mini for tasks that do not require GPT-4o quality, enable prompt caching for requests with shared prefixes (cached tokens cost 50% of standard input token price), and use the Batch API for non-latency-sensitive jobs (50% cost reduction). Monitor your token usage in the dashboard and set spending limits to prevent runaway costs during development.

Frequently Asked Questions

Does ChatGPT Plus give me API access?
No. ChatGPT Plus is a subscription for the ChatGPT web and mobile app. API access is a separate pay-as-you-go service billed per token. Add a payment method at platform.openai.com to get API access — no subscription required.
Which model should I use for production?
GPT-4o-mini for high-volume, cost-sensitive applications. GPT-4o for tasks requiring better reasoning quality. o4-mini for mathematics, coding, and complex logical problems where standard models produce unsatisfactory results. Always benchmark your specific use case — model rankings change with each release.
How do I migrate from the Assistants API?
OpenAI provides a migration guide at developers.openai.com. The Responses API is the primary target — it provides stateful conversation with built-in tools (file search, web search, code interpreter) that replaces most Assistants functionality.

Last updated: October 2026. Model names and pricing change frequently — check platform.openai.com for current information.

Function Calling

Function calling (tool use) lets the model call functions you define and return their results as part of a multi-turn conversation. Define functions as a list of JSON schema objects passed as the tools parameter. When the model decides to call a function, the response includes a tool_calls field with the function name and arguments. Execute the function in your code, then add the result as a tool message in the conversation and call the API again. The model incorporates the function result into its next response. Function calling is the foundation of AI agent patterns — it enables the model to search databases, call APIs, run calculations, and take other actions with real-world effects based on the user’s request. Use structured outputs with function calling by defining the function return schema in the response_format, ensuring the model’s JSON output for function results is always valid and parseable.

Embeddings

The OpenAI Embeddings API converts text to high-dimensional vector representations suitable for semantic search, similarity comparison, clustering, and classification. Call client.embeddings.create() with a model (text-embedding-3-small for most use cases, text-embedding-3-large for higher accuracy at more cost) and your text input. The response contains the embedding vector as a list of floats. Store embeddings in a vector database (Pinecone, Qdrant, pgvector) and retrieve semantically similar content by computing cosine similarity between query embeddings and stored embeddings. Embeddings are the foundation of RAG pipelines and are significantly cheaper than model inference — embedding a document costs a fraction of the cost of generating a response about it. Use text-embedding-3-small for applications processing large document sets where cost matters; the quality difference from text-embedding-3-large is only meaningful for very fine-grained semantic distinctions.

The OpenAI API in 2026 is more capable and more complex than its 2023 origins. The Responses API replaces the Assistants API with a cleaner stateful interface. Reasoning models add a new tier for hard problems. Structured outputs make JSON extraction reliable. The core pattern — create a client, send messages, parse the response — remains simple to start with, and the additional features layer in as your application’s requirements grow. Start with Chat Completions for stateless use cases and the Responses API for anything requiring conversation state or built-in tools, and add streaming, function calling, and structured outputs as your application needs them.

Setting Up a Safe Development Environment

Good habits at the start save painful incidents later. Create a separate virtual environment for each project, store your API key in a local .env file that is listed in .gitignore, and load it with python-dotenv so the key never appears in source code or shell history. Use a distinct API key for each project or environment, because that lets you rotate one key without breaking everything and makes it easy to see which application is responsible for usage.

Set a monthly spending limit and a notification threshold in the billing settings before you write any loops, since a runaway script can burn through credit quickly. Log the request ID returned with each response, because it is the first thing support will ask for if something goes wrong. During development, start with the cheapest model that works and a low max token limit so mistakes are inexpensive. If you ever accidentally commit a key to a repository, revoke it immediately in the dashboard rather than trying to rewrite history, as automated scanners find exposed keys within minutes. These small precautions take ten minutes and protect both your budget and your account.

Leave a Comment