Home
Back to Overview

Understanding OpenAI APIs

Cloud-based AI models for production applications

Learn how to use cloud-based LLMs with the OpenAI API

Getting Started: Setting Up Your API Key

Step 1: Get Your API Key

  1. Visit https://platform.openai.com/api-keys
  2. Sign up or log in to your OpenAI account
  3. Click "Create new secret key"
  4. Copy the key immediately (you won't see it again!)

Step 2: Store It Securely

# Create a .env file in your project root
OPENAI_API_KEY=sk-your-key-here

⚠️ Never commit your .env file to Git! Add it to .gitignore

Step 3: Install Dependencies

npm install openai dotenv

What is the OpenAI API?

The OpenAI API provides programmatic access to powerful language models like GPT-4o. Instead of running models locally, you send requests to OpenAI's servers and receive responses.

Aspect OpenAI API Local LLMs
Setup API key only Download models, need GPU/RAM
Cost Pay per token Free after initial setup
Performance Consistent, high-quality Depends on your hardware
Privacy Data sent to OpenAI Completely local/private

Message Roles: The Conversation Structure

1. System Messages

Define the AI's behavior, personality, and capabilities

{ role: 'system', content: 'You are a helpful Python tutor.' }

Think of it as the AI's "job description" - invisible to users but shapes all responses

2. User Messages

Represent the human's input or questions

{ role: 'user', content: 'How do I use async/await?' }

3. Assistant Messages

Represent the AI's previous responses

{ role: 'assistant', content: 'Async/await is a way to handle promises...' }

Provides context for follow-up questions

Statelessness: A Critical Concept

⚠️ Most Important Principle: OpenAI's API is stateless

Each API call is independent. The model doesn't remember previous requests.

Request 1: "My name is Alice"
Response 1: "Hello Alice!"
Request 2: "What's my name?"
Response 2: "I don't know your name." ← No memory!

✅ How to Maintain Context

You must send the full conversation history with each request:

// First turn
messages.push({ role: 'user', content: 'My name is Alice' });
// Second turn - include full history!
messages.push({ role: 'user', content: "What's my name?" });
// Send complete conversation array

Temperature: Controlling Randomness

0.0 - 0.3

Focused Tasks

  • • Code generation
  • • Data extraction
  • • Translation

0.5 - 0.9

Balanced Tasks

  • • General conversation
  • • Customer support
  • • Content summarization

1.0 - 2.0

Creative Tasks

  • • Story writing
  • • Brainstorming
  • • Poetry

Streaming: Real-time Responses

❌ Non-Streaming

User: "Tell me a story"
[Wait...]
[Wait...]
[Wait...]
Response: "Once upon a time..."
(All at once)

Poor user experience, no feedback

✅ Streaming

User: "Tell me a story"
"Once"
"Once upon"
"Once upon a"
"Once upon a time"
...

Immediate feedback, better UX

💰 Understanding Costs

How Pricing Works

OpenAI charges per token for both input and output. Prices are per 1 million tokens. Cached input pricing applies when reusing context from previous requests (much cheaper). Training costs apply only when fine-tuning models.

Premium Models (GPT-5 Series)

Model Input Cached Input Output
GPT-5.2 $1.75 $0.175 $14.00
GPT-5.2 pro $21.00 - $168.00
GPT-5 mini $0.25 $0.025 $2.00

Fine-tuning Models (GPT-4.1 Series)

Model Input Cached Input Output Training
GPT-4.1 $3.00 $0.75 $12.00 $25.00/1M
GPT-4.1 mini $0.80 $0.20 $3.20 $5.00/1M
GPT-4.1 nano $0.20 $0.05 $0.80 $1.50/1M

* All prices are per 1 million tokens unless otherwise noted. Prices as of January 2026. Check OpenAI's website for current rates.

Tokens: The Currency of LLMs

What are tokens?

Tokens are the fundamental units that language models process. They're not exactly words, but pieces of text.

"Hello world" → ["Hello", " world"] = 2 tokens
"coding" → ["coding"] = 1 token
"uncoded" → ["un", "coded"] = 2 tokens

💰 Cost

You pay per token (input + output)

📏 Context Limits

Each model has a maximum token limit

⚡ Performance

More tokens = longer processing time

Model Selection: Choosing the Right Tool

GPT-5.2: The Premium Choice

Best for: Coding and agentic tasks across diverse industries

Premium model with highest capability. Input: $1,750/1M, Output: $14,000/1M tokens

GPT-5.2 pro: The Intelligence Leader

Best for: Most intelligent and precise tasks

Highest intelligence. Input: $21.00/1M, Output: $168.00/1M tokens

GPT-5 mini: Fast & Cost-Effective

Best for: Well-defined tasks, faster and cheaper than GPT-5.2

Balanced performance. Input: $0.250/1M, Output: $2.000/1M tokens

GPT-4.1: Fine-tuning Ready

Best for: Tasks requiring fine-tuning, complex reasoning

Supports fine-tuning. Input: $3.00/1M, Output: $12.00/1M tokens

GPT-4.1 mini: Budget Fine-tuning

Best for: Fine-tuning on a budget, general-purpose tasks

Cost-effective fine-tuning. Input: $0.80/1M, Output: $3.20/1M tokens

GPT-4.1 nano: Ultra Budget

Best for: Simple tasks, maximum cost savings

Lowest cost option. Input: $0.20/1M, Output: $0.80/1M tokens

o4-mini: Reinforcement Learning

Best for: Tasks requiring reinforcement learning fine-tuning

Specialized for RL. Input: $4.00/1M, Output: $16.00/1M tokens

⚠️ Common Errors & How to Handle Them

Error: Invalid API Key

Error: Invalid API key provided

Solution:

  • Check your .env file exists and has OPENAI_API_KEY
  • Verify the key starts with "sk-"
  • Make sure you're loading dotenv: import 'dotenv/config'

Error: Rate Limit Exceeded

Error: Rate limit exceeded

Solution:

  • Wait a few minutes before retrying
  • Implement exponential backoff in your code
  • Consider upgrading your OpenAI plan for higher limits

Error: Context Length Exceeded

Error: Maximum context length exceeded

Solution:

  • Reduce the length of your input messages
  • Use a model with a larger context window
  • Summarize or truncate conversation history

Key Insights

1️⃣

Statelessness is power and burden: You control context, but you must manage it

2️⃣

System prompts are your secret weapon: Same model → different behaviors

3️⃣

Temperature changes everything: Match it to your task type

4️⃣

Tokens are the real currency: Monitor and optimize usage

Next: System Prompts