Module 2: OpenAI API - Introduction
Module 2: OpenAI API - Introduction
What will you learn in this module?
In Module 1 you learned to DECIDE which provider to use. Now you're going to learn to IMPLEMENT with the most popular provider: OpenAI API.
This module covers everything you need to integrate GPT-3.5/GPT-4 into your application:
- Account setup and API keys
- The Python SDK (v1.x)
- The Chat Completions API
- Pricing and rate limits
- Error handling and retries
- Best practices
By the end, you'll have a functional chatbot connected to the OpenAI API with robust error handling.
🎯 Module goal
Master the OpenAI API: From scratch to production
Deliverables:
- Complete setup (account, API key, SDK installed)
- First successful request (GPT-3.5)
- Conversational chatbot with history
- Robust error handling (retries, fallbacks)
Final project: Technical support chatbot with conversational context
📋 Module structure
Capsule progression:
Phase 1: Setup and First Request (capsules 1-3)
- 01: Module introduction
- 02: Account setup and API keys
- 03: First request with the Python SDK
Phase 2: Chat Completions API (capsules 4-5)
- 04: Conversations with context
- 05: Advanced parameters (temperature, max_tokens)
Phase 3: Production (capsules 6-7)
- 06: Pricing, rate limits, and optimization
- 07: Error handling and retries
Phase 4: Project (capsule 8)
- 08: Mini-project - Technical support chatbot
🔗 Connections with other modules
Prerequisite:
- Module 1 (Decision Framework): You decided that the OpenAI API is the best option for your project
Lateral connections:
- Module 3 (LM Studio): You'll learn a local interface compatible with OpenAI
- Module 5 (OpenRouter): A compatible API, you'll be able to replicate the code
Next step:
- Module 7 (Technical Comparison): You'll compare OpenAI vs the other implemented providers
⏱️ Estimated time
Total module: 3-4 hours
Breakdown per capsule:
- Capsules 1-3 (Setup): 45 minutes
- Capsules 4-5 (Chat API): 60 minutes
- Capsules 6-7 (Production): 45 minutes
- Capsule 8 (Project): 60-90 minutes
Recommended pace: 2 sessions of 2 hours
🛠️ Skills you'll develop
Technical:
-
OpenAI Python SDK (v1.x):
- Installation and configuration
- OpenAI client
- Chat Completions API
- Streaming responses
-
REST API fundamentals:
- Authentication (Bearer token)
- Request/response format (JSON)
- Rate limiting
- Error codes (429, 500, 503)
-
Error handling:
- Try/except patterns
- Exponential backoff
- Retry strategies
- Timeout handling
Conceptual:
-
LLM parameters:
- Temperature (creativity)
- Max tokens (length)
- Top-p (sampling)
-
Conversational context:
- System message (instructions)
- User/assistant messages (history)
- Context window management
-
Cost optimization:
- Token counting
- Model selection (GPT-3.5 vs GPT-4)
- Caching strategies
📦 Prerequisites
Mandatory skills:
- ✅ Basic Python (variables, functions, classes)
- ✅ Pip/virtualenv (dependency management)
- ✅ Basic terminal (cd, ls, python)
Recommended skills (not mandatory):
- ⚠️ Requests library (HTTP calls)
- ⚠️ JSON (reading/writing)
- ⚠️ Environment variables (.env)
Necessary software:
- Python 3.8+ (check:
python --version) - Pip (check:
pip --version) - Code editor (VS Code, PyCharm, etc.)
- OpenAI account (capsule 02 guides you through setup)
💰 Module cost
OpenAI account:
- Signup: Free
- Initial credits: $5 (new users, valid 3 months)
- Real module cost: ~$0.50 ($5 is enough)
Calculation:
- Exercises: ~100 requests × 500 tokens = 50k tokens
- GPT-3.5: 50k tokens × $0.002/1k = $0.10
- Project: ~500 requests × 500 tokens = 250k tokens = $0.50
- Total: $0.60 (well within the $5 credits)
Note: If you already spent your $5 credits, you'll need to add a payment method. The real module cost is <$1.
🎯 Why start with the OpenAI API?
Strategic reasons:
-
De facto standard:
- The OpenAI SDK is the reference others imitate
- LM Studio, Ollama, OpenRouter are "OpenAI-compatible"
- Learn OpenAI → 80% of the knowledge transfers
-
Best developer experience:
- Excellent docs (best in the industry)
- Official SDK maintained (Python, Node, etc.)
- Massive community (Stack Overflow, Discord)
-
Production-ready out of the box:
- 99.9% uptime SLA
- Autoscaling (you don't manage infra)
- Intelligent rate limiting
-
Maximum quality (GPT-4):
- If your project requires maximum accuracy
- OpenAI has the best models (2024-2026)
When NOT to use the OpenAI API:
Review Module 1 - Capsule 04 (Decision Matrix) if:
- Critical privacy (HIPAA, mandatory on-premise)
- Prohibitive cost (millions of queries/month)
- Specific requirements that others meet better
If Module 1 led you to OpenAI, this module is your next step.
📊 What you'll build
Final mini-project (capsule 08):
Technical Support Chatbot
Features:
- Answers frequently asked questions (FAQs)
- Maintains conversational context
- Handles errors gracefully
- Conversation logs
- Cost tracking
Technologies:
- Python 3.8+
- OpenAI SDK v1.x
- Python-dotenv (env vars)
- JSON (persistence)
Interaction example:
User: How do I reset my password?
Bot: To reset your password:
1. Go to Settings > Security
2. Click "Reset Password"
3. You'll receive an email with a link
Do you need help with any step?
User: I'm not getting the email
Bot: I understand, the reset email didn't arrive. Check:
- Spam/Junk folder
- Correct registered email: user@example.com
If it still doesn't arrive within 5 minutes, I can escalate
to human support. Would you like me to do that?
Success metrics:
- ✅ Answers coherently (not random responses)
- ✅ Remembers context (references to previous messages)
- ✅ Handles errors without crashing (rate limits, timeouts)
- ✅ Cost <$0.10 for 100 conversations (efficient)
🔄 Module methodology
Incremental learning:
Pattern: Minimal theory → Immediate code → Iteration
-
I explain the concept (5 minutes):
- What it is
- Why it matters
- When to use it
-
You write minimal code (10 minutes):
- The simplest possible example
- You run it and see the output
- It works on YOUR machine
-
You iterate with exercises (15 minutes):
- Modify parameters
- Observe changes
- Solidify your understanding
No theory without code. No code without running it.
✅ Module success criteria
You've successfully completed it when:
Technical skills:
- You installed the OpenAI SDK without errors
- You made a successful request to GPT-3.5
- You created a conversation with context (3+ messages)
- You implemented a retry with exponential backoff
- You calculated the cost of requests manually
Deliverable:
- A working chatbot with 5+ consecutive exchanges
- Error handling for 3 cases (timeout, rate limit, API error)
- Logs saved to a JSON file
Conceptual understanding:
- You explain the difference GPT-3.5 vs GPT-4 (cost/quality)
- You understand how temperature affects responses
- You know how to count tokens and estimate cost
If you meet all the criteria: ✅ Module 2 passed
You'll be ready for:
- Modules 3-6 (other providers)
- Module 7 (technical comparison)
- Module 8 (unified client)
🔗 Module resources
Official documentation:
- OpenAI Platform Docs - Complete reference
- Python SDK GitHub - Source code
- API Reference - Endpoints
- Pricing - Up-to-date costs
Tools:
- Tokenizer - Count tokens
- Playground - Test without code
- Status Page - Uptime monitoring
Community:
- OpenAI Community Forum
- r/OpenAI - Subreddit
- Discord - Real-time chat
🚀 Getting started
Next step: 02-account-setup-and-api-keys.md
In the next capsule:
- You'll create an OpenAI account
- You'll get an API key
- You'll configure environment variables
- You'll install the Python SDK
Time: 15 minutes
Difficulty: Low
📝 Important notes
⚠️ Warnings:
-
API keys are secrets:
- NEVER commit them to Git
- Use .env + .gitignore
- If you leak one: Regenerate it immediately
-
Billing can surprise you:
- Configure spending limits in the dashboard
- Monitor usage daily
- Stop execution if you reach the limit
-
Rate limits exist:
- Free tier: 3 requests/min (GPT-4)
- Paid tier: 500 requests/min (GPT-3.5)
- Implement retries (capsule 07)
✅ Tips for success:
-
Run ALL the code:
- Don't just read the examples
- Copy, run, modify
- Debugging is part of learning
-
Experiment with parameters:
- Temperature 0 vs 1 (determinism vs creativity)
- Max tokens (cost vs completeness)
- System message (tone and behavior)
-
Use the module as a reference:
- After completing it, come back when you need it
- The code is reusable (copy-paste friendly)
- The patterns apply to real projects
Total estimated time: 3-4 hours
Next: 02-account-setup-and-api-keys.md