Complete OpenAI API guide

Everything about the OpenAI API

Models, pricing, setup guides, coding agent integration, and alternatives. Your complete reference for building with OpenAI's API.

What is the OpenAI API?

The OpenAI API is a cloud service that gives developers programmatic access to OpenAI's family of large language models. Instead of interacting with GPT through a chat interface like ChatGPT, you make HTTP requests from your own code — sending a conversation history and receiving a generated response. The standard endpoint is POST /v1/chat/completions, and it has become the de facto protocol for AI APIs across the industry.

At its core, the API works like this: you send an array of messages (system instructions, user queries, assistant responses from prior turns), choose a model, and the API returns the next message in the conversation. You control the system prompt, temperature, token limits, and output format. Beyond basic text generation, the API supports streaming (tokens arrive as they are generated), function calling (the model generates structured JSON to call external tools), vision (sending images for analysis), and structured output (enforcing a specific JSON schema on the response).

The OpenAI API is used for far more than chatbots. Developers use it to build coding assistants, content generation pipelines, data extraction tools, customer support automation, research applications, and autonomous agents. The same underlying API powers all of these — the difference lies in the system prompt, the tools you attach, the model you select, and the application layer you build around it.

Because OpenAI's chat completions protocol has become the industry standard, most alternative providers — Anthropic, Google, Mistral, DeepSeek, and others — offer OpenAI-compatible endpoints. This means a single codebase can work across multiple providers by changing just the base URL and API key. For developers, this is one of the most important practical facts about the OpenAI API: learning it once gives you access to a wide ecosystem of models and providers.

OpenAI API models

OpenAI's model family spans from budget options for high-volume tasks to frontier models for the most demanding applications. Here is the full lineup as of September 2026.

Model Input (per 1M tokens) Output (per 1M tokens) Context Window Best for
GPT-5.4 Mini $0.75 $4.50 128K High-volume tasks, classification, formatting
GPT-5.4 $2.50 $15.00 128K Balanced production workloads, general assistant
GPT-5.5 $5.00 $30.00 1.05M Advanced reasoning, long-context analysis
GPT-5.6 Sol $5.00 $30.00 1.05M Coding, agent workflows, complex reasoning
GPT-5.3 Codex Spark $1.75 $14.00 128K Code generation, software engineering tasks
GPT-6 Astra $10.00 $50.00 1.05M Frontier intelligence, multimodal, vision

Pricing as of September 2026. All models support the OpenAI-compatible chat completions protocol. Prompt caching reduces cached input costs to 10% of the standard rate.

GPT-5.6 Sol and Terra/Luna — The GPT-5.6 family introduced a split architecture: Sol handles general reasoning and coding, while Terra and Luna are specialized variants optimized for different workload profiles. For most production use cases, GPT-5.6 Sol is the default choice. Read the full breakdown in our GPT-5.6 Sol, Terra & Luna guide.

GPT-6 Astra — The latest frontier model from OpenAI. Astra brings multimodal understanding, enhanced reasoning, and a broader knowledge base. At $10/$50 per million tokens, it is the most expensive option but justified for tasks that genuinely require frontier-level intelligence. See the full details in our GPT-6 Astra pricing and access guide.

OpenAI API pricing

OpenAI charges per token — roughly 4 characters of English text per token. You pay separately for input tokens (the prompt and conversation history you send) and output tokens (the model's response). Pricing varies by model, with budget options starting at fractions of a cent per request and frontier models costing significantly more.

Key pricing details:

For the full comparison — including cost-per-request estimates and model-by-model breakdowns — see our OpenAI API pricing comparison for startups. If you are evaluating whether to use OpenAI direct or a reseller, our OpenAI API vs official: why use a reseller article covers the tradeoffs in detail.

Getting started with the OpenAI API

Setting up the OpenAI API takes about five minutes. Here is the minimum path from zero to your first API call.

Step 1: Get an API key

Sign up at apitokendeal.com (2.5M free tokens, no credit card) or at platform.openai.com. Copy the API key and store it in a password manager or environment variable — it is only shown once.

Step 2: Install the SDK

pip install openai          # Python
npm install openai          # Node.js

Step 3: Make your first request

from openai import OpenAI

client = OpenAI(
    api_key="your_key_here",
    base_url="https://api.apitokendeal.com/v1"  # or api.openai.com/v1
)

response = client.chat.completions.create(
    model="gpt-5.4-mini",
    messages=[{"role": "user", "content": "Explain tokens in 2 sentences"}]
)
print(response.choices[0].message.content)

The only difference between using OpenAI direct and a provider like APItokendeal is the base_url and api_key. Your code, prompts, and model names stay the same. For a walkthrough of the full setup — including environment variables, error handling, and streaming — read our OpenAI API key setup guide.

Using the OpenAI API with coding agents

Coding agents — tools that write, edit, and debug code autonomously — have become one of the most popular use cases for the OpenAI API. These agents use the chat completions protocol with tool use: the agent sends your codebase context to the model, the model proposes file edits or terminal commands, and the agent applies them. The loop continues until the task is complete.

OpenAI's own Codex is a terminal-based coding agent built on GPT models. It operates in a sandboxed environment, reads your codebase, and makes changes based on natural language instructions. Codex uses GPT-5.4 and GPT-5.6 Sol by default, optimized for code generation and multi-file refactoring.

ChatGPT Work is a desktop agent that extends ChatGPT's capabilities to your local applications — creating documents, spreadsheets, presentations, and scheduled automations. It represents a different model of AI agent: not just code-focused, but a general-purpose assistant that operates across your desktop apps.

Beyond OpenAI's own agents, the OpenAI API powers a growing ecosystem of third-party coding tools. All of them work with OpenAI-compatible providers through a simple base URL change. For a comparison of the best options, see our OpenAI-compatible API for coding agents in 2026 guide and our best API for coding agents comparison.

Codex

OpenAI's official agent

Claude Code

Anthropic terminal agent

Cline

VS Code extension

OpenCode

Multi-model CLI agent

OpenAI API alternatives

The OpenAI API is the most widely supported, but it is not the only option. Several providers offer comparable or better value depending on your priorities:

For most developers, the practical choice is not OpenAI or an alternative — it is a platform that gives access to all of them through one key. That is what APItokendeal provides: OpenAI-compatible access to GPT, Claude, DeepSeek, Gemini, Mistral, and 30+ other models. If you are weighing the decision between going direct to OpenAI or using a multi-model provider, our OpenAI API vs official: why use a reseller guide covers the key considerations — cost, billing model, model access, and setup complexity.

Frequently asked questions

What is the OpenAI API?

The OpenAI API is a cloud service that gives developers programmatic access to GPT language models. You send an array of messages to a chat completions endpoint and receive a generated response. The API supports streaming, function calling, vision, and structured output. Most AI providers now offer OpenAI-compatible endpoints, so the same codebase works across multiple providers.

How much does the OpenAI API cost?

Official pricing (September 2026) ranges from $0.75/$4.50 per million tokens for GPT-5.4 Mini to $10/$50 for GPT-6 Astra. GPT-5.6 Sol at $5/$30 is the most popular production model. Prompt caching reduces cached input to 10% of standard rates. Via APItokendeal, prepaid token packs offer up to 70% off these prices.

How do I get started with the OpenAI API?

Get an API key, install the OpenAI SDK, and make a chat completions request. The full setup takes about five minutes. APItokendeal provides 2.5M free tokens on signup so you can start without adding a payment method. See our setup guide for a complete walkthrough.

Which OpenAI API model should I use?

GPT-5.4 Mini for high-volume, low-complexity tasks. GPT-5.4 for balanced production workloads. GPT-5.6 Sol for coding, agents, and complex reasoning. GPT-6 Astra for frontier-level intelligence. For most production applications, model routing — cheap models for simple queries, premium models for complex ones — delivers the best cost-to-quality ratio.

Can I use the OpenAI API with coding agents?

Yes. OpenAI's Codex, Claude Code, Cline, OpenCode, and most other coding agents support OpenAI-compatible APIs. Set the base URL to your provider's endpoint, enter your API key, and select a model. Through APItokendeal, one key gives access to GPT, Claude, DeepSeek, and more — letting you switch models per task without reconfiguring.

Ready to build with OpenAI API?

Get one API key for GPT, Claude, DeepSeek, and 30+ models. Start with 2.5M free tokens.