CoreMesh

Prewarm OpenAI Hosted Sandbox: Reduce Turn Latency

Learn how to pre-warm an OpenAI-hosted sandbox in the Agents API to eliminate cold-start latency, manage active turn billing, and optimize agent turns.

Prewarm OpenAI Hosted Sandbox: Reduce Turn Latency

Pre-warming an OpenAI-hosted sandbox means initializing and configuring the execution environment before a user finishes submitting their prompt. Instead of making the user wait through container provisioning, environment setup, and tool initialization during the live turn, your application prepares the container ahead of time and attaches it to the session the moment the prompt arrives.

According to the OpenAI Platform Changelog as of October 9, 2026, you can pre-warm an OpenAI-hosted sandbox before attaching it to an Agents API (v1/agents) session. Crucially, that same release changed how sandbox compute is charged: compute is billed strictly during active turns, eliminating charges for idle time between turns. Here is how to structure your backend to handle pre-warming, avoid latency spikes, and keep execution reliable.

Why hosted sandbox cold starts happen#

When an agent executes code, manipulates spreadsheets, or inspects files in an OpenAI-hosted sandbox, it runs inside a dedicated container harness. The Agents API, released in public beta on September 10, 2026, manages session orchestration, context compaction, and recovery automatically. However, booting an isolated container from scratch incurs noticeable initialization latency.

That delay consists of three main stages:

  1. Compute allocation: Provisioning the isolated container runtime.
  2. Tool and harness bootstrapping: Loading the base agent runtime, tool bridges, and built-in capabilities (such as the Artifact SDK for spreadsheets, presentations, and documents added on October 16, 2026).
  3. Session attachment: Binding the container's stdin, stdout, and filesystem state to the durable agent session.

If you defer this sequence until the user clicks "Submit", every millisecond of that setup directly inflates Time to First Token (TTFT) and stalls execution. Pre-warming shifts those three stages into the dead time that occurs while the user is drafting their message or navigating your interface.

Understanding the active turn billing model#

Before October 9, 2026, allocating a container early carried a cost penalty because idle sandboxes continued to tick against compute budgets. You had to choose between user experience and infrastructure cost.

Under the updated model documented on the OpenAI Pricing page and changelog:

  • Sandbox compute is billed strictly during active turns.
  • You no longer pay for idle time between turns while the sandbox waits for input.
  • Container pricing for Hosted Shell and Code Interpreter sessions is tiered by size (1 GB at $0.03, 4 GB at $0.12, 16 GB at $0.48, and 64 GB at $1.92 per 20-minute session per container, billed by the minute with a 5-minute minimum per session for eligible runs).

Because idle time between turns is no longer billed against active compute, pre-warming a sandbox 5 to 15 seconds before message submission does not burn compute credits while it sits empty. If you want to compare isolation architectures or evaluate self-hosted alternatives, check out /run-untrusted-python-tests-sandbox/ for approaches that isolate untrusted code without heavy container overhead.

Triggering the pre-warm lifecycle#

Pre-warming requires a trigger event that precedes the final completion call. The most effective triggers depend on your user interface:

Trigger Event Timing Window Trade-off
User opens chat window 5 to 60+ seconds before turn Guarantees container is ready; higher rate of unused warm sandboxes if user drops off.
First character typed / input focus 2 to 10 seconds before turn High intent signal; gives enough runway to hide container setup.
Background task detected Immediate upon workflow enqueue Ideal for asynchronous agent graphs or multi-step execution pipelines.

In standard chat clients, binding the pre-warm call to input focus or typing activity yields the best balance between resource readiness and intent. When the user selects an input field or types the first few characters, the client signals your backend to pre-warm the sandbox. By the time the user finishes their sentence and hits enter, the sandbox is warm and waiting to be attached to the agent session.

Managing pre-warmed sandbox pools in Python#

To manage pre-warmed instances cleanly without leaking resources, maintain a short-lived registry on your application server. The registry tracks warm containers, attaches them upon turn dispatch, and evicts stale instances if a user navigates away.

The following runnable example demonstrates the state machine for pre-warming, claiming, and reaping expired sandboxes using standard Python:

python
import time

class SandboxPool:
    def __init__(self, ttl_seconds: float = 30.0):
        self.ttl_seconds = ttl_seconds
        self._pool = {}

    def prewarm(self, user_id: str, sandbox_config: dict) -> str:
        sandbox_id = f"sbx_{user_id}_{int(time.time() * 1000)}"
        self._pool[user_id] = {
            "sandbox_id": sandbox_id,
            "created_at": time.time(),
            "config": sandbox_config,
            "status": "ready"
        }
        return sandbox_id

    def claim(self, user_id: str) -> dict:
        self.evict_expired()
        entry = self._pool.pop(user_id, None)
        if entry:
            entry["status"] = "attached"
            return entry
        return {"sandbox_id": None, "status": "cold_start_required"}

    def evict_expired(self):
        now = time.time()
        expired = [uid for uid, data in self._pool.items() if now - data["created_at"] > self.ttl_seconds]
        for uid in expired:
            del self._pool[uid]

pool = SandboxPool(ttl_seconds=10.0)

# User begins typing
sbx_id = pool.prewarm("usr_42", {"memory_limit": "4GB"})
print(f"Pre-warmed sandbox: {sbx_id}")

# User submits turn within TTL
claimed = pool.claim("usr_42")
print(f"Claimed status: {claimed['status']}, ID: {claimed['sandbox_id']}")

# Second check verifies sandbox was detached from pool
retry = pool.claim("usr_42")
print(f"Subsequent claim status: {retry['status']}")

When building client-side bridges or API handlers, review /openai-api-python/ for examples of structured request patterns using the standard SDK.

Attaching pre-warmed sandboxes in the Agents API#

In the Agents API (v1/agents), an agent run executes within durable sessions. Sandboxes are attached as part of the session environment configuration. When making the request via HTTP or the SDK, pass the pre-warmed sandbox identifier into the session parameters rather than requesting an on-demand container initialization.

Here is how that workflow looks in a backend dispatch route using requests:

python
import os
import requests

OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
BASE_URL = "https://api.openai.com/v1"

def execute_turn_with_prewarmed_sandbox(session_id: str, sandbox_id: str, prompt: str):
    url = f"{BASE_URL}/agents/sessions/{session_id}/turns"
    headers = {
        "Authorization": f"Bearer {OPENAI_API_KEY}",
        "Content-Type": "application/json"
    }
    payload = {
        "input": prompt,
        "environment": {
            "sandbox": {
                "id": sandbox_id,
                "attach_existing": True
            }
        }
    }
    response = requests.post(url, headers=headers, json=payload, timeout=30)
    response.raise_for_status()
    return response.json()

If the pre-warmed sandbox has already expired or failed health verification, fall back to letting the Agents API spawn a fresh container inline. This keeps the turn from failing if a network hiccup or TTL expiration catches the registry empty.

Pairing pre-warming with Ultrafast mode#

Pre-warming removes startup latency before the turn begins, but you also need to manage token generation latency during the turn. On October 16, 2026, OpenAI introduced Ultrafast mode for the Agents API.

By setting agent.service_tier: "ultrafast" alongside a supported model (such as gpt-6-astra or gpt-6.1-sol), the system reduces time between output tokens. Combining pre-warming with Ultrafast mode addresses both ends of the latency problem: pre-warming eliminates setup lag before execution, while Ultrafast accelerates token generation during execution.

For pricing context, standard vs. Ultrafast rates on flagship models (per 1M tokens) break down as follows:

  • GPT-6 Astra: Standard is $10.00 input / $50.00 output; Ultrafast is $60.00 input / $300.00 output.
  • GPT-6.1 Sol: Standard is $2.00 input / $10.00 output; Ultrafast is $12.00 input / $60.00 output.

To see how Ultrafast mode is configured across session manifests and model tiers, read /openai-agents-api-ultrafast-mode/.

Common gotchas and traps#

Avoid these operational issues when maintaining pre-warmed sandboxes:

  • Unbounded TTLs: A user might click an input box, trigger a pre-warm call, and walk away. While idle compute between turns is not billed, lingering idle sandboxes consume allocation slots. Set an aggressive client-side TTL (15 to 45 seconds) to discard unattached containers.
  • Environment drift: Sandboxes configured with pre-installed packages or custom filesystem mounts must match the agent's target spec. If an agent turn requires specific dependencies or the Artifact SDK (which includes spreadsheet, presentation, and document editing skills), confirm that the pre-warmed container shares that identical base configuration.
  • Ignoring rate limit spikes: Launching hundreds of pre-warm calls simultaneously when user traffic spikes can trigger 429 rate limit errors with the slow_down error code. Check for Retry-After headers on initial allocation calls and apply exponential backoff when provisioning pools.
  • Assuming zero-cost abandonment: While compute is billed during active turns, underlying container instances that remain allocated indefinitely may still incur container reservation limits or base minimums depending on session rules. Always clean up abandoned pre-warm instances.

Implementation checklist#

Use this sequence to implement pre-warming safely:

  • [ ] Configure client-side events (such as text input focus or draft message creation) to emit a prewarm request to your backend.
  • [ ] Ensure pre-warmed sandboxes are tagged with the same runtime configuration, dependencies, and tools required by the agent.
  • [ ] Track pre-warmed instances in a local cache with a maximum TTL of 30 seconds.
  • [ ] On prompt submission, pop the pre-warmed sandbox ID and attach it directly to the v1/agents session turn.
  • [ ] Include a fallback path in your turn runner so that if the pre-warmed instance has expired, the session falls back to inline sandbox allocation.
  • [ ] Pair your low-latency pre-warm architecture with agent.service_tier: "ultrafast" if your application requires minimum generation delay.

Quick answers

What is sandbox pre-warming in the OpenAI Agents API?

Sandbox pre-warming is the practice of initializing and configuring an OpenAI-hosted sandbox before a user finishes sending their prompt. This eliminates startup latency by completing container provisioning ahead of time.

Do I pay for pre-warmed sandboxes while they sit idle?

As of October 9, 2026, OpenAI-hosted sandbox compute is billed during active turns. You no longer pay for idle compute time between turns while the sandbox waits for input.

Which models support the Agents API and sandbox execution?

The Agents API supports flagship models including GPT-6 Astra (gpt-6-astra) and GPT-6.1 Sol (gpt-6.1-sol), both of which support function calling, code execution tools, and computer use.

Did this solve it?

One tap. No account.

Saved you an hour? Send a tip.

USDC or ETH on Base, Ethereum, Arbitrum and more. Keeps the notes free and the examples running.

Base

Network

0xc783bc5c3fa14cEC3f09A22284bC91C5AF0d59e1
Open in wallet · 5 USDC

On a computer? Scan the code with your phone wallet, or copy the address.

Pick the network your wallet or exchange uses. USDC works on all six; ETH on Base, Ethereum, Arbitrum and Optimism. Don't send on Tron or Solana: this address doesn't exist there. Tips are optional and unlock nothing; every guide stays free.

Discussion

Start the discussion

Indra Wijaya

Builds and runs AI agents, bots and content pipelines, and writes down what works and what broke.

About · Email · RSS

Keep reading

New notes, no spam.

One post at a time, only when there's something worth reading. Follow in any feed reader.

Subscribe via RSS