Cloud-based AI models for production applications
Learn how to use cloud-based LLMs with the OpenAI API
⚠️ Never commit your .env file to Git! Add it to .gitignore
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 |
Define the AI's behavior, personality, and capabilities
Think of it as the AI's "job description" - invisible to users but shapes all responses
Represent the human's input or questions
Represent the AI's previous responses
Provides context for follow-up questions
⚠️ Most Important Principle: OpenAI's API is stateless
Each API call is independent. The model doesn't remember previous requests.
You must send the full conversation history with each request:
Focused Tasks
Balanced Tasks
Creative Tasks
Poor user experience, no feedback
Immediate feedback, better UX
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.
| 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 |
| 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 are the fundamental units that language models process. They're not exactly words, but pieces of text.
You pay per token (input + output)
Each model has a maximum token limit
More tokens = longer processing time
Best for: Coding and agentic tasks across diverse industries
Premium model with highest capability. Input: $1,750/1M, Output: $14,000/1M tokens
Best for: Most intelligent and precise tasks
Highest intelligence. Input: $21.00/1M, Output: $168.00/1M tokens
Best for: Well-defined tasks, faster and cheaper than GPT-5.2
Balanced performance. Input: $0.250/1M, Output: $2.000/1M tokens
Best for: Tasks requiring fine-tuning, complex reasoning
Supports fine-tuning. Input: $3.00/1M, Output: $12.00/1M tokens
Best for: Fine-tuning on a budget, general-purpose tasks
Cost-effective fine-tuning. Input: $0.80/1M, Output: $3.20/1M tokens
Best for: Simple tasks, maximum cost savings
Lowest cost option. Input: $0.20/1M, Output: $0.80/1M tokens
Best for: Tasks requiring reinforcement learning fine-tuning
Specialized for RL. Input: $4.00/1M, Output: $16.00/1M tokens
Solution:
import
'dotenv/config'Solution:
Solution:
Statelessness is power and burden: You control context, but you must manage it
System prompts are your secret weapon: Same model → different behaviors
Temperature changes everything: Match it to your task type
Tokens are the real currency: Monitor and optimize usage