DEV Community

Cover image for How to Use GPT-5.4 API
Preecha
Preecha

Posted on

How to Use GPT-5.4 API

TL;DR

To use the GPT-5.4 API, install the OpenAI SDK, initialize a client with an API key, and call chat.completions.create() with model="gpt-5.4". The model supports computer use, tool search, a 1M-token context window, and vision capabilities. Listed pricing is $2.50 per million input tokens and $15 per million output tokens.

Try Apidog today

This guide walks through setup, chat completions, computer-use workflows, tool integration, image processing, long-context code analysis, streaming, retries, and cost optimization.

Introduction

GPT-5.4 is a general-purpose model with native computer-use capabilities, efficient tool search, and context windows of up to 1M tokens. To use it effectively, start with a basic request and add capabilities incrementally.

You will learn how to:

  • Send requests from Python and Node.js
  • Automate browser and desktop workflows
  • Integrate large tool ecosystems and MCP servers
  • Process high-resolution images and documents
  • Analyze large codebases and multiple documents
  • Stream responses and handle transient failures
  • Control token usage and production costs

When integrating GPT-5.4 into an application, use Apidog to design, test, and document your API endpoints. Its unified workflow can help you debug requests, create automated test suites, mock responses during development, and generate documentation for your team.

Quick Start: Your First GPT-5.4 Request

You can test the API manually before writing application code.

Create a POST request to:

https://api.openai.com/v1/chat/completions
Enter fullscreen mode Exit fullscreen mode

Configure the request with:

  • Authorization: Bearer YOUR_API_KEY
  • Content-Type: application/json
  • Body: a model name and messages
  • Environment variables: store API keys separately for development, staging, and production

Save the request to a collection so you can rerun it while developing.

Testing a GPT-5.4 request in an API client

This workflow helps you validate the request shape and inspect responses before adding SDK code.

Prerequisites

Python

Install the SDK:

pip install openai
Enter fullscreen mode Exit fullscreen mode

Set your API key:

export OPENAI_API_KEY="your-api-key"
Enter fullscreen mode Exit fullscreen mode

Send a request:

import os
from openai import OpenAI

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "system",
            "content": "You are a helpful coding assistant.",
        },
        {
            "role": "user",
            "content": (
                "Write a Python function to sort a list of dictionaries by a key."
            ),
        },
    ],
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Node.js

Install the SDK:

npm install openai
Enter fullscreen mode Exit fullscreen mode

Set your API key:

export OPENAI_API_KEY="your-api-key"
Enter fullscreen mode Exit fullscreen mode

Then make the request:

const OpenAI = require("openai");

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

async function main() {
  const response = await client.chat.completions.create({
    model: "gpt-5.4",
    messages: [
      {
        role: "system",
        content: "You are a helpful coding assistant.",
      },
      {
        role: "user",
        content:
          "Write a Python function to sort a list of dictionaries by a key.",
      },
    ],
  });

  console.log(response.choices[0].message.content);
}

main();
Enter fullscreen mode Exit fullscreen mode

Example Output

def sort_dicts_by_key(dict_list, key, reverse=False):
    """
    Sort a list of dictionaries by a specified key.

    Args:
        dict_list: List of dictionaries to sort
        key: The dictionary key to sort by
        reverse: If True, sort in descending order

    Returns:
        Sorted list of dictionaries
    """
    return sorted(dict_list, key=lambda item: item.get(key, ""), reverse=reverse)

data = [
    {"name": "Alice", "age": 30},
    {"name": "Bob", "age": 25},
    {"name": "Charlie", "age": 35},
]

sorted_by_age = sort_dicts_by_key(data, "age")
print(sorted_by_age)
# [
#   {'name': 'Bob', 'age': 25},
#   {'name': 'Alice', 'age': 30},
#   {'name': 'Charlie', 'age': 35}
# ]
Enter fullscreen mode Exit fullscreen mode

Understanding GPT-5.4 Capabilities

GPT-5.4 is described in the source material across four primary areas. Choose the capability based on the work your application needs to perform.

1. Knowledge Work

The listed GDPval win rate is 83%.

Use GPT-5.4 for:

  • Spreadsheet creation and analysis
  • Presentation generation
  • Document drafting and editing
  • Financial modeling
  • Data analysis and reporting

GPT-5.4 knowledge-work capabilities

2. Computer Use

The listed OSWorld-Verified result is 75%.

Use computer use for:

  • Browser automation
  • Data entry across applications
  • Interactive web scraping
  • Testing workflows
  • Cross-application task automation

GPT-5.4 computer-use capabilities

3. Coding

The listed SWE-Bench Pro result is 57.7%.

Use GPT-5.4 for:

  • Full-stack development
  • Frontend UI generation
  • Debugging complex issues
  • Refactoring
  • Test generation

GPT-5.4 coding capabilities

4. Tool Integration

The listed Toolathlon result is 54.6%.

Use tool integration for:

  • MCP server integrations
  • Multi-step API workflows
  • External tool orchestration
  • Agentic applications

GPT-5.4 tool-integration capabilities

Computer Use API

GPT-5.4 can operate a computer through screenshots, mouse commands, and keyboard input.

GPT-5.4 computer-use API workflow

Before deploying computer-use workflows, test every action in isolation. For example:

  • Validate screenshot upload endpoints
  • Test click, type, scroll, and keypress commands
  • Mock responses for each computer action
  • Automate multi-turn workflow tests
  • Document the computer-use API contract

How Computer Use Works

A typical loop looks like this:

  1. Capture the current screen.
  2. Send the screenshot and task to GPT-5.4.
  3. Receive a computer command.
  4. Execute the command in your application.
  5. Capture a new screenshot.
  6. Send the new state back to the model.
  7. Continue until the task is complete or a turn limit is reached.

Basic Computer-Use Setup

The following example uses pyautogui for screen capture and command execution. Replace these functions with the desktop or browser automation library used by your application.

import base64
import io

import pyautogui
from openai import OpenAI

client = OpenAI()

def take_screenshot():
    """Capture the current screen as a base64-encoded PNG."""
    screenshot = pyautogui.screenshot()

    buffer = io.BytesIO()
    screenshot.save(buffer, format="PNG")

    return base64.b64encode(buffer.getvalue()).decode("utf-8")

def execute_computer_command(command):
    """Execute a model-generated computer command."""
    action = command.get("action")

    if action == "click":
        x, y = command.get("coordinate", [0, 0])
        pyautogui.click(x, y)

    elif action == "type":
        pyautogui.write(command.get("text", ""), interval=0.05)

    elif action == "scroll":
        pyautogui.scroll(command.get("scroll_amount", 0))

    elif action == "keypress":
        pyautogui.press(command.get("key", ""))

    return take_screenshot()

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": (
                    "Navigate to gmail.com and log in with the credentials "
                    "I provided."
                ),
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/png;base64,{take_screenshot()}",
                },
            },
        ],
    }
]

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=messages,
    tools=[
        {
            "type": "computer",
            "display_width": 1920,
            "display_height": 1080,
            "display_number": 1,
        }
    ],
    tool_choice="required",
)

for tool_call in response.choices[0].message.tool_calls:
    if tool_call.type == "computer":
        command = tool_call.function.arguments
        new_screenshot = execute_computer_command(command)

        messages.append(
            {
                "role": "assistant",
                "content": response.choices[0].message.content,
            }
        )
        messages.append(
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": (
                                "data:image/png;base64,"
                                f"{new_screenshot}"
                            ),
                        },
                    }
                ],
            }
        )
Enter fullscreen mode Exit fullscreen mode

In production, validate the tool-call payload before executing it. Do not allow arbitrary coordinates, keystrokes, or text entry without applying your own authorization and safety rules.

Safety Policies

Use confirmation policies for actions that could expose data or create irreversible side effects:

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=messages,
    tools=[
        {
            "type": "computer",
            "display_width": 1920,
            "display_height": 1080,
            "confirmation_policy": "always",
            # Other values: "never" or "selective"
        }
    ],
    system_message="""
You are operating a computer. Follow these rules:
1. Never enter credentials without explicit user confirmation.
2. Ask before deleting files or data.
3. Confirm before sending emails or messages.
4. Report errors or unexpected states immediately.
""",
)
Enter fullscreen mode Exit fullscreen mode

A practical policy is to require confirmation for:

  • Credential entry
  • File deletion
  • Email or message submission
  • Purchases or financial actions
  • Changes to production systems

Browser Automation with Playwright

Playwright can execute the commands against a browser instead of the desktop.

import base64
import json

from playwright.sync_api import sync_playwright
from openai import OpenAI

client = OpenAI()

def browser_automation_workflow():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        page = browser.new_page()

        page.goto("https://example.com")

        screenshot = page.screenshot()
        screenshot_b64 = base64.b64encode(screenshot).decode("utf-8")

        messages = [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "Find the login form and fill it out.",
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": (
                                "data:image/png;base64,"
                                f"{screenshot_b64}"
                            ),
                        },
                    },
                ],
            }
        ]

        response = client.chat.completions.create(
            model="gpt-5.4",
            messages=messages,
            tools=[{"type": "computer"}],
            tool_choice="required",
        )

        for tool_call in response.choices[0].message.tool_calls:
            if tool_call.type != "computer":
                continue

            command = json.loads(tool_call.function.arguments)

            if command.get("action") == "click":
                x, y = command.get("coordinate", [0, 0])
                page.mouse.click(x, y)

            elif command.get("action") == "type":
                page.keyboard.type(command.get("text", ""))

            new_screenshot = page.screenshot()

            # Append the command result and continue the loop.
            # messages.append(...)
Enter fullscreen mode Exit fullscreen mode

For reliable browser automation, combine screenshots with DOM-based checks where possible. Use a maximum number of turns and stop when the page reaches a known success or failure state.

Email and Calendar Automation

A multi-step workflow can be represented as a single task, but your application should still enforce limits and confirmations.

def process_email_and_schedule_meeting():
    workflow_prompt = """
Complete this workflow:
1. Open Gmail and find unread emails from the last 24 hours.
2. Identify meeting requests or scheduling questions.
3. Extract proposed dates, times, attendees, and meeting purpose.
4. Open Google Calendar and check availability.
5. Send calendar invites only for confirmed meetings.
6. Reply to emails confirming the scheduled time.

Report what was completed and identify anything requiring approval.
"""

    screenshot = take_screenshot()

    messages = [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": workflow_prompt},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/png;base64,{screenshot}",
                    },
                },
            ],
        }
    ]

    for turn in range(10):
        response = client.chat.completions.create(
            model="gpt-5.4",
            messages=messages,
            tools=[{"type": "computer"}],
            tool_choice="required",
        )

        content = response.choices[0].message.content or ""
        if "complete" in content.lower():
            print(f"Workflow completed in {turn + 1} turns")
            break

        # Execute the returned command, capture a new screenshot,
        # and append the next user message here.
Enter fullscreen mode Exit fullscreen mode

Performance Practices

The source material reports the following results for Mainstay's processing of 30K property-tax portals:

  • 95% first-attempt success rate
  • 3x faster than previous models
  • 70% fewer tokens per session

Regardless of the benchmark, apply these implementation practices:

  • Use screenshots with sufficient resolution for the task.
  • Describe the goal and success condition precisely.
  • Set a turn limit to prevent infinite loops.
  • Avoid sending duplicate screenshots.
  • Use selective confirmation for trusted workflows.
  • Log each action and its resulting screen state.

Tool Search and Integration

Tool search lets the model find tool definitions on demand instead of loading every full schema into the initial request. The source material reports a 47% token reduction for large tool ecosystems.

GPT-5.4 tool search workflow

How Tool Search Works

The application provides:

  1. A lightweight list of available tools
  2. A mechanism for retrieving the full definition of a selected tool
  3. An execution layer for running the selected tool
  4. The tool result sent back to the model

This avoids placing hundreds of complete schemas in every request.

Basic Tool Search Pattern

available_tools = [
    {
        "name": "get_weather",
        "description": "Get current weather for a location",
    },
    {
        "name": "send_email",
        "description": "Send an email to a recipient",
    },
    {
        "name": "calendar_search",
        "description": "Search the calendar for events",
    },
]

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "user",
            "content": "What's the weather in Tokyo and send it to my team?",
        }
    ],
    tools=available_tools,
    tool_choice="auto",
)

# If the model selects a tool, retrieve its complete definition
# and execute it in your application.
Enter fullscreen mode Exit fullscreen mode

In a real implementation, the lightweight tool list and full tool schemas must match the lookup logic in your application.

MCP Server Integration

The source material references a 47% token reduction in the MCP Atlas benchmark.

mcp_servers = [
    {
        "name": "filesystem",
        "description": "File system operations",
        "tool_count": 12,
    },
    {
        "name": "database",
        "description": "Database query operations",
        "tool_count": 8,
    },
    {
        "name": "web-search",
        "description": "Web search and scraping",
        "tool_count": 15,
    },
]

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "user",
            "content": (
                "Find all Python files modified today and search for "
                "TODO comments."
            ),
        }
    ],
    tools=mcp_servers,
    parallel_tool_calls=True,
)
Enter fullscreen mode Exit fullscreen mode

The model can request tool definitions as needed. Compare this approach with loading every MCP tool definition upfront to measure token savings in your own workload.

Multi-Step Tool Workflows

For workflows that involve several independent tools, define each operation explicitly:

workflow_steps = """
1. Read emails from students with assignment attachments.
2. Download each attachment.
3. Upload each file to the grading portal.
4. Grade each assignment using the rubric.
5. Record grades in a spreadsheet.
6. Send confirmation emails to students.
"""

tools = [
    {"name": "email_read", "description": "Read emails from the inbox"},
    {"name": "email_send", "description": "Send emails"},
    {"name": "file_download", "description": "Download file attachments"},
    {"name": "file_upload", "description": "Upload files to a web portal"},
    {"name": "web_form_fill", "description": "Fill and submit web forms"},
    {"name": "spreadsheet_write", "description": "Write data to a spreadsheet"},
    {
        "name": "rubric_evaluate",
        "description": "Evaluate work against a rubric",
    },
]

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[{"role": "user", "content": workflow_steps}],
    tools=tools,
    parallel_tool_calls=True,
)
Enter fullscreen mode Exit fullscreen mode

The Toolathlon result listed in the source is 54.6% for GPT-5.4 versus 45.7% for GPT-5.2.

Vision and Image Processing

GPT-5.4 supports visual processing with original image detail up to 10.24 million pixels.

Image Detail Levels

Use the detail level based on the accuracy and latency your task requires:

  • original: highest fidelity, up to 10.24M pixels and a 6000-pixel maximum dimension
  • high: up to 2.56M pixels and a 2048-pixel maximum dimension
  • low: fastest processing with lower visual detail
response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://example.com/high-res-image.jpg",
                        "detail": "original",
                    },
                },
                {
                    "type": "text",
                    "text": "Analyze this technical diagram.",
                },
            ],
        }
    ],
)
Enter fullscreen mode Exit fullscreen mode

Use high or original for small text, diagrams, and screenshots. Use low when speed is more important than fine-grained visual accuracy.

Document Parsing

The source material reports an OmniDocBench error rate of 0.109, compared with 0.140 for GPT-5.2.

The following example converts PDF pages to images and sends the first five pages in one request:

import base64
import io

from pdf2image import convert_from_path
from openai import OpenAI

client = OpenAI()

def parse_complex_document(pdf_path):
    pages = convert_from_path(pdf_path, dpi=300)
    content = []

    for page in pages[:5]:
        buffer = io.BytesIO()
        page.save(buffer, format="PNG")

        image_b64 = base64.b64encode(buffer.getvalue()).decode("utf-8")

        content.append(
            {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/png;base64,{image_b64}",
                    "detail": "high",
                },
            }
        )

    content.append(
        {
            "type": "text",
            "text": """
Extract all data from this document:
1. Tables with row and column headers.
2. Key figures and their captions.
3. Summary statistics mentioned in the text.

Return the result as structured JSON.
""",
        }
    )

    response = client.chat.completions.create(
        model="gpt-5.4",
        messages=[{"role": "user", "content": content}],
    )

    return response.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode

For longer documents, process pages in batches and preserve page numbers in your prompt or output schema.

UI Screenshot Analysis

import base64

from openai import OpenAI

client = OpenAI()

def analyze_ui_screenshot(screenshot_path):
    with open(screenshot_path, "rb") as image_file:
        image_b64 = base64.b64encode(image_file.read()).decode("utf-8")

    response = client.chat.completions.create(
        model="gpt-5.4",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/png;base64,{image_b64}",
                            "detail": "original",
                        },
                    },
                    {
                        "type": "text",
                        "text": """
Review this UI screenshot for accessibility issues:
1. Color contrast problems.
2. Missing labels or alt-text indicators.
3. Visible focus-state or keyboard-navigation issues.
4. Text size and readability.
5. Screen-reader compatibility concerns.

List each issue with its location and severity.
""",
                    },
                ],
            }
        ],
    )

    return response.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode

Long-Context Workflows

GPT-5.4 supports context windows of up to 1M tokens experimentally.

Standard Context

For a code review, load the relevant files and provide a focused task:

from openai import OpenAI

client = OpenAI()

with open("large_codebase.py", "r", encoding="utf-8") as code_file:
    code = code_file.read()

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "system",
            "content": "You are a code review assistant.",
        },
        {
            "role": "user",
            "content": f"""
Review this codebase for:
1. Security vulnerabilities.
2. Performance issues.
3. Code-style inconsistencies.
4. Missing error handling.

Code:
{code}
""",
        },
    ],
    max_tokens=4000,
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Extended Context

The source material configures the extended context through extra_body:

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "user",
            "content": large_document,
        }
    ],
    extra_body={
        "model_context_window": 1048576,
        "model_auto_compact_token_limit": 272000,
    },
)
Enter fullscreen mode Exit fullscreen mode

Requests exceeding 272K tokens are described as counting at a 2x usage rate. Extended context is listed as experimental and available in Codex.

Multi-Document Analysis

When sending multiple documents, label each document and define the output format:

def analyze_multiple_documents(documents):
    content_parts = []

    for index, document in enumerate(documents, start=1):
        content_parts.append(
            f"=== Document {index}: {document['title']} ===\n"
        )
        content_parts.append(document["content"][:50000])
        content_parts.append("\n\n")

    combined_content = "".join(content_parts)

    response = client.chat.completions.create(
        model="gpt-5.4",
        messages=[
            {
                "role": "user",
                "content": f"""
Analyze these documents and provide:
1. Key themes across all documents.
2. Contradictions or inconsistencies.
3. Action items.
4. A timeline of events, if applicable.

{combined_content}
""",
            }
        ],
        max_tokens=8000,
    )

    return response.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode

For production workloads, measure prompt size before sending requests and truncate or summarize documents when the full context is unnecessary.

Coding and Development Workflows

The source material lists GPT-5.4 at 57.7% on SWE-Bench Pro, matching GPT-5.3-Codex, with added computer-use capabilities.

Generate a Frontend Component

from openai import OpenAI

client = OpenAI()

def generate_frontend_component(spec):
    prompt = f"""
Create a complete React component based on this specification:

{spec}

Requirements:
1. Use a functional component with hooks.
2. Add TypeScript types for all props and state.
3. Use Tailwind CSS for styling.
4. Support mobile, tablet, and desktop layouts.
5. Include ARIA labels and keyboard navigation.
6. Add Jest and React Testing Library tests.

Return:
- Component file (.tsx)
- Styles, if not using Tailwind
- Test file (.test.tsx)
"""

    response = client.chat.completions.create(
        model="gpt-5.4",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=6000,
    )

    return response.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode

Example specification:

Create an interactive isometric theme park simulation game with:

- Tile-based path placement
- Ride and scenery construction
- Guest pathfinding and queueing
- Park metrics for money, guests, happiness, and cleanliness
- Browser playability
- Playwright testing
- Generated isometric assets
Enter fullscreen mode Exit fullscreen mode

Treat generated code as a starting point. Run the tests, inspect dependencies, and review the output before merging it.

Debug Complex Issues

Include logs, stack traces, and only the relevant source files:

def debug_with_full_context(error_logs, codebase_files, stack_trace):
    context = f"""
ERROR LOGS:
{error_logs}

STACK TRACE:
{stack_trace}

RELEVANT CODE FILES:
{codebase_files}

Identify the root cause and provide a fix.

Consider:
1. Race conditions or timing issues.
2. Memory leaks or resource exhaustion.
3. Incorrect data-flow assumptions.
4. Unhandled edge cases.
5. External dependency issues.

Return:
1. Root-cause analysis.
2. Specific code changes.
3. Regression tests.
"""

    response = client.chat.completions.create(
        model="gpt-5.4",
        messages=[{"role": "user", "content": context}],
        max_tokens=4000,
    )

    return response.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode

Playwright Interactive Testing

The source material describes an experimental Playwright skill for testing an application while building it:

def playwright_interactive_debug():
    prompt = """
Build a todo web application and test it as you build:

1. Create the HTML structure.
2. Add CSS styling.
3. Implement JavaScript functionality.
4. After each feature, use Playwright to:
   - Verify element visibility.
   - Test user interactions.
   - Check state persistence.
   - Validate edge cases.
5. Report and fix issues found during testing.
"""

    response = client.chat.completions.create(
        model="gpt-5.4",
        messages=[{"role": "user", "content": prompt}],
        tools=[{"type": "playwright_interactive"}],
        max_tokens=8000,
    )

    return response.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode

Streaming Responses

Streaming reduces perceived latency for long responses by returning output incrementally.

Python

from openai import OpenAI

client = OpenAI()

stream = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "user",
            "content": "Write a detailed explanation of quantum computing.",
        }
    ],
    stream=True,
)

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

Node.js

const stream = await client.chat.completions.create({
  model: "gpt-5.4",
  messages: [
    {
      role: "user",
      content: "Write a detailed explanation of quantum computing.",
    },
  ],
  stream: true,
});

for await (const chunk of stream) {
  if (chunk.choices[0].delta.content) {
    process.stdout.write(chunk.choices[0].delta.content);
  }
}
Enter fullscreen mode Exit fullscreen mode

Track Usage During Streaming

def stream_with_usage(stream):
    total_tokens = 0

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

            # Approximate only; use API usage data for billing.
            total_tokens += len(content) // 4

        if chunk.usage:
            print(f"\n\nUsage: {chunk.usage.total_tokens} tokens")

    return total_tokens
Enter fullscreen mode Exit fullscreen mode

The character-based estimate is useful for rough progress reporting, not accounting. Use the usage information returned by the API for cost tracking.

Error Handling and Retry Logic

Production clients should distinguish between errors that are safe to retry and errors that require code or configuration changes.

import time

from openai import (
    APIError,
    AuthenticationError,
    OpenAI,
    RateLimitError,
)

client = OpenAI()

def make_request_with_retry(messages, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(
                model="gpt-5.4",
                messages=messages,
                max_tokens=2000,
                temperature=0.7,
            )

        except RateLimitError:
            if attempt == max_retries - 1:
                raise

            wait_time = 2**attempt
            print(f"Rate limited. Waiting {wait_time}s...")
            time.sleep(wait_time)

        except APIError as error:
            if error.status_code < 500 or attempt == max_retries - 1:
                raise

            wait_time = 2**attempt
            time.sleep(wait_time)

        except AuthenticationError:
            print("Invalid API key. Check your credentials.")
            raise

    raise RuntimeError("Maximum retries exceeded")

try:
    response = make_request_with_retry(
        [{"role": "user", "content": "Hello, GPT-5.4!"}]
    )
    print(response.choices[0].message.content)
except Exception as error:
    print(f"Request failed: {error}")
Enter fullscreen mode Exit fullscreen mode

Recommended retry rules:

  • Retry rate-limit responses with exponential backoff.
  • Retry server-side errors such as 5xx responses.
  • Do not retry authentication or validation errors without changing the request.
  • Add a maximum retry count.
  • Log request IDs, latency, and failure categories.

Configure Timeouts

import httpx
from openai import OpenAI

client = OpenAI(
    timeout=httpx.Timeout(
        60.0,
        connect=10.0,
    )
)

try:
    response = client.chat.completions.create(
        model="gpt-5.4",
        messages=[
            {
                "role": "user",
                "content": "Long-running task...",
            }
        ],
    )
except httpx.TimeoutException:
    print(
        "Request timed out. Consider streaming or reducing "
        "task complexity."
    )
Enter fullscreen mode Exit fullscreen mode

Production Best Practices

API Testing and Documentation

Before deploying, create tests for both successful and failure paths:

  • Valid requests and expected response shapes
  • Missing or invalid API keys
  • Rate limits
  • Timeouts
  • Malformed tool calls
  • Empty or oversized inputs
  • Model errors and server failures

Use Apidog to:

  • Create comprehensive API test suites
  • Run API tests in CI/CD pipelines
  • Mock GPT-5.4 responses during integration testing
  • Generate API documentation from tested requests

Production API testing workflow

Team Collaboration

Keep development environments consistent:

  • Share API collections with team members.
  • Use environment variables for development, staging, and production.
  • Document expected behavior and edge cases.
  • Keep secrets out of collections and source control.
  • Record model, prompt, tool, and parameter changes.

The source material reports that teams using Apidog experienced 40–60% faster API integration cycles. The stated benefit comes from combining request debugging, automated tests, and documentation in one workflow.

Cost Optimization Strategies

The listed GPT-5.4 pricing is:

  • Input: $2.50 per million tokens
  • Output: $15 per million tokens

Use the following techniques to control usage.

Make Prompts Direct

Avoid unnecessary conversational text:

# Verbose prompt
bad_prompt = """
Hello! I hope you're doing well. I was wondering if you could possibly help me
with something. I have this code here and I'm not quite sure what it does.
Could you please explain it to me? Here's the code:
""" + code

# Direct prompt
good_prompt = f"Explain what this code does:\n{code}"
Enter fullscreen mode Exit fullscreen mode

The source estimates that removing approximately 50 tokens saves $0.000125 per request. At 1 million requests per month, that is approximately $125.

Limit Response Length

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "user",
            "content": "Summarize this article.",
        }
    ],
    max_tokens=200,
)
Enter fullscreen mode Exit fullscreen mode

For predictable formats, combine a clear output schema with an appropriate token limit.

The source also shows stop sequences:

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {
            "role": "user",
            "content": "List 5 items.",
        }
    ],
    stop=["\n\n", "6."],
)
Enter fullscreen mode Exit fullscreen mode

Use Batch Processing

The source material describes the Batch API as providing a 50% discount for non-real-time workloads.

import json

from openai import OpenAI

client = OpenAI()

batch_requests = []

for article in articles:
    batch_requests.append(
        {
            "custom_id": article["id"],
            "method": "POST",
            "url": "/v1/chat/completions",
            "body": {
                "model": "gpt-5.4",
                "messages": [
                    {
                        "role": "user",
                        "content": article["content"],
                    }
                ],
            },
        }
    )

batch_file = client.files.create(
    file=json.dumps(batch_requests),
    purpose="batch",
)

batch = client.batches.create(
    input_file_id=batch_file.id,
    endpoint="/v1/chat/completions",
    completion_window="24h",
)
Enter fullscreen mode Exit fullscreen mode

Use batch processing for asynchronous jobs such as document classification, bulk summarization, or offline evaluation.

Cache Repeated Requests

Cache only when identical inputs are expected to produce an acceptable reusable result.

import hashlib
import json

class ResponseCache:
    def __init__(self):
        self.cache = {}

    def _get_key(self, messages, kwargs):
        payload = {
            "messages": messages,
            "kwargs": kwargs,
        }

        return hashlib.md5(
            json.dumps(payload, sort_keys=True).encode("utf-8")
        ).hexdigest()

    def get_or_create(self, client, messages, **kwargs):
        key = self._get_key(messages, kwargs)

        if key in self.cache:
            return self.cache[key]

        response = client.chat.completions.create(
            model="gpt-5.4",
            messages=messages,
            **kwargs,
        )

        self.cache[key] = response
        return response

cache = ResponseCache()
response = cache.get_or_create(client, messages)
Enter fullscreen mode Exit fullscreen mode

For production, replace the in-memory dictionary with a shared cache and define an expiration policy. Include all output-affecting parameters in the cache key.

Conclusion

GPT-5.4 supports several workflows beyond basic chat completions:

  • Computer use for browser and desktop automation
  • Tool search for large tool ecosystems
  • Vision for screenshots, diagrams, and documents
  • Long context for large codebases and multi-document analysis
  • Streaming for lower perceived latency

Production integrations still require API testing, retries, timeouts, monitoring, and cost controls. Apidog provides a unified workflow for designing, testing, debugging, and documenting API integrations.

Start with a basic chat request. Add vision, tools, or computer use only when your use case requires them. Then measure latency, token usage, error rates, and task completion before expanding the workflow.

FAQ

How do I use the GPT-5.4 computer-use feature?

Include the computer tool in an API request, send the current screen as an image, and process the returned computer commands. Execute each command with a library such as pyautogui or Playwright, capture a new screenshot, and continue the conversation until completion.

Add turn limits and confirmation steps for sensitive actions.

What is tool search, and how do I enable it?

Tool search loads tool definitions on demand instead of sending every full schema in the initial request. Provide a lightweight list of available tools and return the full definition when the model selects one. The source material reports a 47% token reduction for large tool ecosystems.

How do I use the 1M-token context window?

The source material shows this configuration:

extra_body={
    "model_context_window": 1048576,
    "model_auto_compact_token_limit": 272000,
}
Enter fullscreen mode Exit fullscreen mode

Requests exceeding 272K tokens are described as counting at a 2x usage rate. The feature is listed as experimental and available in Codex.

What is the difference between GPT-5.4 and GPT-5.4 Pro?

According to the source material, GPT-5.4 Pro provides higher accuracy on complex reasoning tasks, with an 89.3% versus 82.7% result on BrowseComp. The listed pricing is $30/$180 for Pro compared with $2.50/$15 for standard GPT-5.4.

Use standard GPT-5.4 for most workloads and consider Pro when maximum accuracy justifies the higher cost.

How do I reduce GPT-5.4 API costs?

  • Use cached inputs, listed as providing 90% savings.
  • Remove unnecessary prompt text.
  • Set max_tokens based on the required output.
  • Use the Batch API for non-real-time jobs.
  • Cache repeated responses.
  • Select the appropriate image detail level.
  • Monitor input and output tokens separately.

Can GPT-5.4 process multiple images in one request?

Yes. Add multiple image_url content parts to a single message. This is useful for multi-page documents, image comparisons, and sequential screenshots.

How do I handle rate limits in production?

Use exponential backoff, such as 1, 2, and 4-second delays. Also:

  • Cap the number of retries.
  • Use batch processing for bulk work.
  • Spread high-volume requests over time.
  • Monitor rate-limit responses.
  • Request higher limits when necessary.

What programming languages does GPT-5.4 support best?

The source material identifies Python, JavaScript/TypeScript, React, Node.js, Java, Go, Rust, SQL, and common web technologies as strong use cases. It also lists GPT-5.4 at 57.7% on SWE-Bench Pro.

How do I stream GPT-5.4 responses?

Set stream=True in Python or stream: true in Node.js. Iterate over the returned chunks and process each content delta as it arrives.

Is GPT-5.4 suitable for production workloads?

The source material describes GPT-5.4 as suitable for production, with fewer factual errors than GPT-5.2 and more efficient token usage. Before deployment, add:

  • Retry and timeout handling
  • Authentication and secret management
  • Tool-call validation
  • Usage and cost tracking
  • Latency and error monitoring
  • Automated API tests
  • Human confirmation for sensitive computer-use actions

Top comments (0)