Live
Auto Router Beta Introduces Per‑Request Model Selection and Cost Trade‑offsSecure Agent Execution with Kagent and Agent Substrate: Isolation, Policy, and AvailabilityAgentic AI Harnesses: From Prompt Chains to Executable CodeNative PyTorch TPU Support Arrives via Open‑Source TorchTPUML Drift simplifies on-device GPU inference across heterogeneous hardwareRethinking Kubernetes for AI Agents: Sandbox Isolation and Warm‑Pool ExecutionCloudflare WAF adds default‑log rules for Brotli encoding and Next.js image endpointGPU Capacity Planning: Validate Outcomes Before Adding HardwareAuto Router Beta Introduces Per‑Request Model Selection and Cost Trade‑offsSecure Agent Execution with Kagent and Agent Substrate: Isolation, Policy, and AvailabilityAgentic AI Harnesses: From Prompt Chains to Executable CodeNative PyTorch TPU Support Arrives via Open‑Source TorchTPUML Drift simplifies on-device GPU inference across heterogeneous hardwareRethinking Kubernetes for AI Agents: Sandbox Isolation and Warm‑Pool ExecutionCloudflare WAF adds default‑log rules for Brotli encoding and Next.js image endpointGPU Capacity Planning: Validate Outcomes Before Adding Hardware
Cloudflare

Running Codex Sessions in Cloudflare Containers via the OpenAI Agents API

AI SummaryPowered by AI

Cloudflare Containers can now host OpenAI Agents API Codex sessions, moving execution from a fully OpenAI‑managed sandbox to a self‑hosted container per session. This gives engineers control over runtime environment, data access, and integration points while retaining OpenAI’s session orchestration.

Cloudflare Containers are now usable as the execution host for the OpenAI Agents API, letting each Codex session run inside a dedicated, self‑hosted container. This shifts the runtime boundary from a purely OpenAI‑managed environment to a Cloudflare‑controlled one, while OpenAI continues to handle session orchestration, context compaction, and recovery.

Architectural Shift

The new pattern introduces a Cloudflare Worker that spawns a separate Container for every Codex session. The Worker maintains the container’s lifecycle: it keeps the container alive while work is in progress, reconnects on subsequent inputs, and automatically shuts the container down after a period of inactivity. The OpenAI Agents API remains the entry point, but the actual code execution now occurs inside the Cloudflare Container.

Implementation Guidance

Developers can start with the open‑source Workers template located in the cloudflare/sandbox-sdk/openai/agents-api repository. The template demonstrates how to:

  • Instantiate a Container per session.
  • Maintain an active connection for follow‑up prompts.
  • Detect idle state and trigger automatic shutdown.

Practitioners can extend the container definition to restrict network access, mount data sources, or integrate with other Cloudflare services such as KV storage or R2. Because the container runs under the developer’s control, any additional tooling or data‑access policies must be explicitly added.

Operational Considerations

Running Codex in self‑hosted containers introduces new operational signals to monitor:

  • Container start‑up latency, which adds to the overall request time.
  • Resource consumption per session, especially if many concurrent containers are active.
  • Idle‑shutdown thresholds, which affect cost and availability.

Operators should instrument the Worker to emit metrics on container lifecycle events and set alerts for unexpected spikes in container count or prolonged idle periods.

Security Implications

Isolation is now provided by the Cloudflare Container runtime rather than by OpenAI’s internal sandbox. This gives engineers the ability to enforce controlled data and network access inside the container, but also places responsibility on them to configure those controls correctly. Misconfiguration could expose internal resources or allow unintended outbound traffic.

Related CloudNinjas coverage: AI engineering.

What This Means For Practitioners

Adopting the Cloudflare Container model lets you keep execution of Codex prompts within your own trust boundary while still leveraging OpenAI’s session management. Evaluate the template, define the minimal network and data permissions required for your use case, and instrument lifecycle metrics to ensure the new containers behave predictably at scale.

Originally published atCloudflare Developer Platform