GenAIHub
Back to Technical
Foundation Models

OpenAI GPT Models

Comprehensive guide to OpenAI's GPT family of models. Learn about GPT-4o, GPT-4, GPT-3.5, their capabilities, API usage, pricing, and best practices for integration.

Current GPT Models

4o

GPT-4o

Recommended

The flagship multimodal model. "o" stands for "omni" - handles text, images, and audio with state-of-the-art performance. 2x faster and 50% cheaper than GPT-4 Turbo.

Context Window 128K tokens
Training Data Oct 2023
Input Price $2.50/1M tokens
Output Price $10/1M tokens
4o-m

GPT-4o-mini

Cost-Effective

Small, affordable model ideal for lightweight tasks. Replaces GPT-3.5 Turbo as the go-to option for simple completions, chat, and classification at minimal cost.

Context Window 128K tokens
Training Data Oct 2023
Input Price $0.15/1M tokens
Output Price $0.60/1M tokens
o1

o1 / o1-mini

Reasoning

Advanced reasoning models with chain-of-thought capabilities. Designed for complex problems in math, science, coding, and multi-step logical reasoning.

Context Window 200K tokens
Max Output 100K tokens
Input Price $15/1M tokens
Output Price $60/1M tokens
4T

GPT-4 Turbo

Legacy

Previous flagship model. Still available but GPT-4o is recommended for most use cases due to better performance at lower cost.

Context Window 128K tokens
Training Data Dec 2023
Input Price $10/1M tokens
Output Price $30/1M tokens

Which Model to Use?

4o
GPT-4o — General Purpose

  • Complex reasoning and analysis
  • Image understanding and vision tasks
  • Code generation and debugging
  • Long document processing

mini
GPT-4o-mini — Cost Optimization

  • High-volume, simple tasks
  • Classification and extraction
  • Basic chat assistants
  • Text summarization

o1
o1 — Deep Reasoning

  • Complex math problems
  • Scientific research questions
  • Multi-step logic puzzles
  • Advanced coding challenges

Pro Tip

Start with GPT-4o-mini for cost efficiency, then upgrade to GPT-4o for quality-critical paths. Use o1 only when you need explicit step-by-step reasoning.

API Usage

Basic Chat 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 quantum computing in simple terms."}
    ],
    temperature=0.7,
    max_tokens=500
)

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

Structured Outputs (JSON Mode)

from openai import OpenAI
from pydantic import BaseModel

class MovieReview(BaseModel):
    title: str
    rating: int
    summary: str
    pros: list[str]
    cons: list[str]

client = OpenAI()

response = client.beta.chat.completions.parse(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "Analyze movie reviews and extract structured data."},
        {"role": "user", "content": "Review: The Godfather is a masterpiece of cinema..."}
    ],
    response_format=MovieReview
)

review = response.choices[0].message.parsed
print(f"Rating: {review.rating}/10")

Vision (Image Analysis)

from openai import OpenAI
import base64

client = OpenAI()

# From URL
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": "https://example.com/image.jpg"}
                }
            ]
        }
    ]
)

# From Base64
with open("image.jpg", "rb") as f:
    image_data = base64.b64encode(f.read()).decode()

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this image in detail."},
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}
                }
            ]
        }
    ]
)

Function Calling (Tools)

from openai import OpenAI
import json

client = OpenAI()

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City name"},
                    "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,
    tool_choice="auto"
)

# Check if model wants to call a function
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    args = json.loads(tool_call.function.arguments)
    print(f"Function: {tool_call.function.name}")
    print(f"Arguments: {args}")

Streaming Responses

from openai import OpenAI

client = OpenAI()

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a short poem about AI."}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Key Parameters

Parameter Type Default Description
temperature float 1.0 Randomness (0-2). Lower = more focused, higher = more creative.
max_tokens int model max Maximum tokens in response. Set to control output length and cost.
top_p float 1.0 Nucleus sampling. Alternative to temperature (use one, not both).
frequency_penalty float 0 Reduce repetition based on frequency (-2 to 2).
presence_penalty float 0 Encourage new topics (-2 to 2).
seed int null Reproducible outputs (beta). Same seed + input = same output.
response_format object text Force JSON output or use Structured Outputs.

Best Practices

System Prompt First

Always include a clear system message defining role, task, and constraints. This significantly improves output quality.

Use Structured Outputs

For data extraction and APIs, use response_format with JSON schema for guaranteed valid output.

Temperature by Task

Use 0-0.3 for factual/code, 0.5-0.7 for balanced, 0.8-1.2 for creative writing.

Control Costs

Set max_tokens, use gpt-4o-mini for simple tasks, cache repeated prompts and responses.

Handle Errors

Implement retry logic with exponential backoff for rate limits (429) and server errors (500+).

Stream for UX

Use streaming for user-facing apps. Users perceive faster responses when text appears progressively.

Related Topics