OpenAI's GPT-4 family includes multiple models optimized for different use cases, from fast chat to complex reasoning.
Model Variants
| Model | Context | Best For |
|---|---|---|
| GPT-4o | 128K | Multimodal, fast responses |
| GPT-4 Turbo | 128K | Complex reasoning, long context |
| GPT-4o mini | 128K | Cost-effective, high volume |
Key Strengths
Multimodal Capabilities
GPT-4o can process:
- Text input and output
- Image analysis
- Audio (speech) input/output
- Document understanding
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": {"url": image_url}}
]
}]
)Function Calling
Native support for structured tool use:
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}]
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=tools
)JSON Mode
Guaranteed valid JSON output:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "List 3 programming languages as JSON"}],
response_format={"type": "json_object"}
)API Usage
Basic Completion
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain recursion in programming."}
],
temperature=0.7,
max_tokens=500
)
print(response.choices[0].message.content)Streaming
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a Python tutorial."}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")Vision Analysis
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "Describe this architecture diagram"},
{"type": "image_url", "image_url": {
"url": "https://example.com/diagram.png"
}}
]
}]
)Optimal Use Cases
Code Generation
Create a Python FastAPI application with:
- User authentication using JWT
- CRUD operations for a blog post model
- SQLAlchemy ORM with PostgreSQL
- Proper error handling
- Input validation with PydanticData Extraction
Extract structured data from this invoice image:
- Vendor name
- Invoice number
- Line items with prices
- Total amount
- Due date
Return as JSON.Content Creation
Write a technical blog post about microservices vs monoliths.
Target audience: Senior developers
Length: 1500 words
Include: code examples, pros/cons table, decision frameworkBest Practices
System Prompts
messages = [
{
"role": "system",
"content": """You are a senior software engineer.
Rules:
- Provide production-quality code
- Include error handling
- Add comments for complex logic
- Follow PEP 8 for Python
- Suggest tests for critical functions"""
},
{"role": "user", "content": "Build a rate limiter class."}
]Temperature Settings
| Temperature | Use Case |
|---|---|
| 0 | Deterministic, factual responses |
| 0.3-0.5 | Balanced creativity |
| 0.7 | Creative writing |
| 1.0+ | Highly creative, varied |
Token Management
import tiktoken
encoder = tiktoken.encoding_for_model("gpt-4o")
tokens = encoder.encode("Your text here")
print(f"Token count: {len(tokens)}")Pricing (as of 2024)
| Model | Input (1M tokens) | Output (1M tokens) |
|---|---|---|
| GPT-4o | $2.50 | $10.00 |
| GPT-4o mini | $0.15 | $0.60 |
| GPT-4 Turbo | $10.00 | $30.00 |
Limitations
- Knowledge cutoff date
- Token context limits
- Rate limits per tier
- Potential for hallucinations
- Cost at scale
GPT-4 vs Claude Comparison
| Feature | GPT-4o | Claude Sonnet |
|---|---|---|
| Context | 128K | 200K |
| Multimodal | Yes (native) | Yes (images) |
| Function calling | Native | Via tools |
| JSON mode | Yes | Via prompting |
| Speed | Fast | Fast |
| Code quality | Excellent | Excellent |
- gpt-4
- openai
- chatgpt
- gpt-4 turbo
- gpt-4o