DS DevShelfHub Projects · AI tools
Pay-per-token LLM API Function Calling Multimodal

OpenAI API Review: The Reference Standard LLM API

The OpenAI API is not just a wrapper around GPT models — it is the architectural reference every other LLM provider tries to be compatible with. Chat Completions, function calling, structured outputs, Assistants, embeddings, DALL-E, Whisper, TTS — all in one platform. If you build with LLMs seriously, this API will be at the center of your stack.

OpenAI API — GPT-4o, function calling, structured outputs

What is the OpenAI API?

The OpenAI API is a REST endpoint that gives your application access to GPT models. You send a request with a prompt, system instructions, and optional tool definitions; OpenAI returns a completion. It is the de facto standard interface — so much so that other LLM providers (Anthropic, Google, Cohere, open-source alternatives) have built OpenAI-compatible endpoints to make switching seamless.

It is not one API but a family:

  • --
    Chat Completions API — the main interface; dialogue with models
  • --
    Assistants API — stateful conversations with persistent context
  • --
    Embeddings API — convert text to dense vectors (for search, similarity)
  • --
    DALL-E 3 — image generation
  • --
    Whisper — speech-to-text
  • --
    TTS (Text-to-Speech) — natural voice generation

Core Capabilities

1

Chat Completions API

POST to /v1/chat/completions. Pass messages (system, user, assistant), select a model (GPT-4o, GPT-4o mini, GPT-3.5 Turbo), and get back a completion. The most common API call — used in 90% of applications.

2

Function Calling / Tool Use

Define functions as JSON schema. When you ask the model a question, it can respond with a function call instead of text. Your app executes the function and feeds the result back. This powers AI agents, API integrations, and structured workflows.

3

Structured Outputs

Guarantee that GPT returns valid JSON matching a schema you define. No more parsing errors or hallucinated fields. Newer feature; production-grade reliability.

4

Assistants API

Stateful conversations with persistent threads, file uploads, retrieval (file search), and code execution. Higher-level abstraction than Chat Completions; useful for multi-turn complex workflows, but slower due to server-side state management.

5

Embeddings API

Convert text to a dense vector (1536 dimensions for text-embedding-3-small). Foundation for semantic search, RAG pipelines, and recommendation systems.

Chat Completions Request Flow

  1. 1

    Authenticate with your API key

    Set the Authorization: Bearer sk-... header. Keep this secret — never expose it client-side.

  2. 2

    Send messages and optional tools

    Include model, messages array, and any tools definitions in your POST body. Token count determines cost.

  3. 3

    Model runs inference

    OpenAI routes to the selected model, checks rate limits, processes system prompt and messages, and evaluates whether to call any defined tools.

  4. 4

    Handle the response

    The response contains message.content (text) or message.tool_calls, along with usage tokens for billing.

Your App
    │
    ▼
OpenAI API
    │
    ├─ Authenticate (API key in header)
    ├─ Route to model (GPT-4o, GPT-4o mini, etc.)
    ├─ Check rate limits
    ├─ Count tokens (input + output estimate)
    │
    ▼
Model inference
    │
    ├─ Process system prompt
    ├─ Process messages
    ├─ If tools defined: evaluate whether to call
    ├─ Generate response
    │
    ▼
Response object
    │
    ├─ message.content (text) OR message.tool_calls (function calls)
    ├─ usage.prompt_tokens, completion_tokens
    ├─ finish_reason (stop, tool_calls, length, etc.)
    │
    ▼
Your App (process response)

Cost is charged based on usage.prompt_tokens + usage.completion_tokens. Tool calls cost extra output tokens. Streaming changes the flow but pricing remains the same.

Python Example — Chat with Tool Use

python
from openai import OpenAI

client = OpenAI(api_key="sk-...")

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "What's the weather in NYC?"}
    ],
    tools=tools
)

# Check if model called a function
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    result = get_weather(location="NYC")

    # Feed result back to model
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "user", "content": "What's the weather in NYC?"},
            {"role": "assistant", "content": None,
              "tool_calls": [tool_call]},
            {"role": "tool", "content": result,
              "tool_call_id": tool_call.id}
        ]
    )

print(response.choices[0].message.content)

Available Models

Model Context Price (per 1M tokens) Best for
GPT-4o 128K $5 input / $15 output Complex reasoning, multimodal (vision)
GPT-4o mini 128K $0.15 input / $0.60 output Fast, cheap, simple tasks
GPT-3.5 Turbo 16K $0.50 input / $1.50 output Legacy; use GPT-4o mini instead

Prices are approximate; verify on openai.com/api/pricing.

Practical Gotchas

  • --
    Rate limit tiers: Starting accounts get strict limits. Tier 1 (new): 3 RPM. Tier 5 (spend $100K+): 200 RPM. Plan accordingly or request higher limits upfront.
  • --
    Context is not memory: 128K context means each request can include up to 128K tokens total. Without careful prompt management, old conversation history bloats requests. Implement message trimming for long conversations.
  • --
    Token costs compound fast: A 10K-word document x 100 API calls = millions of tokens quickly. Use GPT-4o mini for cheap inference; GPT-4o for quality when you need it.
  • --
    Function calls add output tokens: When the model decides to call a function, the entire tool_calls object is output tokens. Multiple tools = more tokens per function call.
  • --
    Assistants API is slower: Server-side state, file indexing, and code execution add latency. P50 response time ~2–5s vs ~500ms for Chat Completions. Use Assistants only when you need statefulness.
  • --
    No offline fallback: If OpenAI is down, your app is down unless you implement fallback to another provider. Most production apps use OpenRouter or similar to route to backup models.

OpenAI API vs Anthropic API

Both are reference-standard LLM APIs. Key differences:

Feature OpenAI API Anthropic API
Context window 128K (GPT-4o) 200K (Claude 3.5 Sonnet)
Function calling Yes Yes
Structured output Yes (guaranteed) Beta
Vision / multimodal Yes (GPT-4o) Yes (Claude 3.5)
Prompt caching No Yes (90% savings)
Price (input / output) $5 / $15 (GPT-4o) $3 / $15 (Claude 3.5)
OpenAI-compatible Native No (custom SDK)

Pros and Cons

Pros

  • +Industry-standard API; most tools compatible
  • +Excellent model quality (GPT-4o is top-tier)
  • +Full feature set: chat, function calling, structured output, embeddings, DALL-E, Whisper
  • +Great documentation and SDK
  • +Reliable and battle-tested at scale

Cons

  • -Expensive compared to alternatives
  • -Rate limit tiers restrict new accounts
  • -No prompt caching (Anthropic has it)
  • -Assistants API is slower and less flexible
  • -No offline or on-device option

Pricing

Pay-per-token

No monthly subscription. You pay for input and output tokens separately. GPT-4o: $5 per 1M input tokens, $15 per 1M output tokens. GPT-4o mini: $0.15 input / $0.60 output — much cheaper for simple tasks.

Free trial

$5 free credits on sign-up (expires after 3 months). Enough to test the API but not for production workloads.

Verify pricing at openai.com/api/pricing.

When NOT to Use It

  • --
    Very high-volume, cost-sensitive workloads: If you're processing millions of documents daily, OpenAI's pricing will dominate costs. Open-source alternatives (Llama on vLLM) or cheaper providers (Groq) may be better.
  • --
    You need long context + low cost: Claude 3.5 Sonnet has 200K context at lower price. If that's your bottleneck, Anthropic API wins.
  • --
    Offline or on-device inference required: OpenAI API requires internet. For edge devices or privacy-critical work, run local models instead.

Who Should Use the OpenAI API?

Best for any developer or team building with LLMs. Startups, enterprises, AI-first apps, chatbots, agents — the OpenAI API is the de facto standard. If you're unsure which API to start with, start here.

Caveat: evaluate cost vs alternatives for your specific use case. For high-volume inference or very long contexts, alternatives may be cheaper. Compare with the Anthropic API or Google AI Studio for specific use cases.

Tips for Getting the Most from the OpenAI API

  • 01.
    Start with GPT-4o mini: For most use cases, GPT-4o mini delivers 80% of GPT-4o quality at a fraction of the cost. Only upgrade to GPT-4o when you actually need the reasoning quality boost.
  • 02.
    Use Structured Outputs for data extraction: Instead of parsing free-form text, define a JSON schema and use response_format={"type": "json_schema", ...}. Guaranteed valid JSON — no post-processing needed.
  • 03.
    Implement exponential backoff for rate limits: New accounts hit rate limits easily. Wrap API calls with retry logic and exponential backoff (start at 1s, double each retry). Use the tenacity library for Python.
  • 04.
    Stream responses for better UX: Set stream=True to display tokens as they arrive. Reduces perceived latency significantly for user-facing apps. Pricing is identical.
  • 05.
    Trim conversation history to control costs: Long conversations grow token counts fast. Keep only the last N messages and always include the system prompt. Use a sliding window approach to maintain coherence.

Frequently Asked Questions

Is the OpenAI API free?
No. OpenAI charges per token. New accounts receive $5 in free credits that expire after three months. After that you pay per token — GPT-4o costs $5 per million input tokens and $15 per million output tokens.
What is the difference between the OpenAI API and ChatGPT?
ChatGPT is a consumer chat product. The OpenAI API gives developers programmatic access to the same GPT models so they can build AI-powered applications, automate workflows, and integrate language intelligence into their own products.
Does the OpenAI API support function calling?
Yes. You define functions as JSON schema and GPT models decide when to call them. Your application executes the function and feeds the result back — the foundation of AI agents and structured data extraction.
What models are available through the OpenAI API?
GPT-4o (multimodal flagship), GPT-4o mini (fast and cheap), DALL-E 3 (image generation), Whisper (speech-to-text), TTS (text-to-speech), and text-embedding models. GPT-3.5 Turbo is legacy — prefer GPT-4o mini instead.
What are the main alternatives to the OpenAI API?
The main alternatives are the Anthropic API (Claude with 200K context), Google AI Studio (Gemini with a free tier), and OpenRouter for multi-provider routing. For self-hosted inference, Meta Llama runs via Ollama or vLLM.
Does the OpenAI API work offline?
No. The OpenAI API requires an internet connection. For offline or on-device inference, use open-source models such as Meta Llama via Ollama or vLLM on your own hardware.