Skip to content

A Codex cloud environment defines the isolated hosted container where unattended Codex cloud tasks execute: repository checkout, setup and maintenance phases, setup-only secrets, container caching, and a default-off agent internet policy. Developers and tech leads architect these boundaries so background tasks stay fast, reproducible, safe against prompt injection, and verifiable through CI.

Your first cloud task burns most of its runtime reinstalling packages, the second fails offline against a private registry, and the third opens a pull request whose tests never ran because browser binaries were missing. Every one of those failures stems from environment architecture rather than model quality.

Understand the six-phase cloud task lifecycle

Section titled “Understand the six-phase cloud task lifecycle”

Every cloud task—whether triggered from the web, CLI, GitHub, or workspace chat integrations—moves through strict phase boundaries that explain almost every runtime failure:

  1. Container start: Codex launches a container from its base image (or resumes a cached snapshot) and checks out your pushed remote branch.
  2. Setup phase: Setup runs with internet access and is the only phase where secrets exist.
  3. Maintenance phase: On cached resumes, Codex skips full setup and runs the fast maintenance script to catch up with recent branch changes.
  4. Lockdown: Secrets are purged from the container and the agent-phase internet policy engages (default: completely offline).
  5. Agent loop: Codex reads committed AGENTS.md instructions, edits files, and runs build, lint, and test commands inside the prepared filesystem.
  6. Result: The task returns a summary and one diff per attempt, ready for inspection, local application, or pull-request review.

OpenAI offers both published reusable cloud environments (prepared, tested, and shared across a workspace for interactive and chat delegation) and script-configured cloud environments (used for custom setup/maintenance pipelines and repository integrations). Follow the official OpenAI Codex documentation for current UI workflows, codex cloud CLI subcommands, and plan availability.

Design setup, caching, and secret boundaries deliberately

Section titled “Design setup, caching, and secret boundaries deliberately”

Split expensive setup from fast maintenance

Section titled “Split expensive setup from fast maintenance”

Start with automatic dependency installation for standard ecosystems, and write an explicit setup script once tasks need system packages, code generators, browser binaries, or shared workspace builds. Keep three invariants in every setup script:

  • Fail loudly (set -euo pipefail): Silent install failures let the agent waste turns diagnosing “command not found” errors.
  • Pin lockfile installs (--frozen-lockfile, npm ci): Guarantee every container resolves the exact dependency graph CI tests.
  • End with a smoke gate (such as typecheck): A broken environment should fail during setup with a clear log rather than tempting the agent to edit application code around missing tools.

Because container snapshots stay warm across tasks for hours, split work by change frequency:

  • Setup script (expensive, rare changes): OS packages, Playwright/browser binaries, base toolchain, initial lockfile install.
  • Maintenance script (cheap, runs on every cached resume): Frozen lockfile sync and codegen so cached containers never test today’s branch against yesterday’s schemas or dependencies.

Remember two practical traps: setup runs in a separate shell from the agent (persist required runtime variables through environment settings rather than transient export), and resetting a shared environment’s cache forces every teammate’s next task to pay full setup time. Rehearse setup scripts locally using OpenAI’s codex-universal reference container before saving changes to a shared environment.

Enforce the secrets vs environment variables split

Section titled “Enforce the secrets vs environment variables split”
StoreAvailable duringAppropriate contentsNever store here
Environment variableSetup and agent phasesNODE_ENV=test, local test DB URLs, test flagsAnything unsafe to expose in a pull request or log
SecretSetup phase only (removed before agent loop)Private npm/PyPI registry tokens, container pull tokensCredentials required during runtime test execution

Removing secrets at lockdown ensures a prompt-injected dependency or web page cannot exfiltrate registry tokens during the agent loop. Whenever tests require a runtime credential, supply an isolated test-only dummy key or stub the external service.

Setup always has network access; the agent loop has none by default. That default blocks three major risks at once: prompt injection from external pages, code exfiltration, and unreviewed package installs.

Turn agent internet on only when tasks genuinely cannot run offline, combining the tightest domain allowlist with read-only HTTP methods:

Egress posturePolicy shapeWhen to use
Off (default)Zero agent internetAll packages and fixtures install during setup. Start here.
Locked downCommon dependencies preset + explicit named domains · GET, HEAD, OPTIONS onlyAgent must query public registries or read-only test APIs
Hand-built allowlistEmpty preset + manually justified domains · read-only methodsRegulated codebases requiring written justification per host
UnrestrictedAll domains allowedDisposable sandboxes holding zero proprietary code or secrets. Avoid on real repos.

Restricting methods to GET, HEAD, and OPTIONS prevents POST/PUT exfiltration, yet query parameters in GET URLs can still leak data to an allowlisted domain—keep the domain list minimal.

Use best-of-N and monorepo fan-out selectively

Section titled “Use best-of-N and monorepo fan-out selectively”

Codex cloud supports running multiple independent attempts (--attempts, best-of-N) on the same prompt and fanning identical scoped prompts across monorepo packages. Because each attempt consumes a full agent run, spend extra runs only where ambiguity makes a single first pass expensive:

  • Good fits for best-of-N: Race conditions, subtle caching bugs, refactors with genuinely competing designs, or performance trade-offs. Name distinct candidate approaches explicitly if attempts keep converging on identical diffs.
  • Skip best-of-N for mechanical edits: Three identical diffs waste compute without improving confidence.
  • Monorepo fan-out discipline: Submit one narrow task per package against a pushed branch using an identical prompt template so diffs remain directly comparable, and forbid cross-package edits in each task.

Verify cloud task diffs without reading every line

Section titled “Verify cloud task diffs without reading every line”

Treat every cloud diff as an untrusted proposal filtered through five objective gates:

  1. Require CI-equivalent command output. Your setup script makes every AGENTS.md check runnable; reject any cloud task whose summary omits the exact lint, typecheck, and test commands and exit statuses.

  2. Check blast radius from task metadata first. Inspect changed-file and line counts before reading diffs—a single-endpoint fix touching 40 files goes back with tighter scope immediately.

  3. Re-run verification outside the agent. Apply the diff inside a clean local Git worktree or open a pull request and let CI run on a clean runner. CI is the authority, never the agent’s summary.

  4. Run automated review and protect the oracle. Trigger @codex review (code review with Codex) and explicitly inspect any edits to test assertions, snapshots, CI files, or lockfiles (protecting the oracle).

  5. Keep named human merge ownership. Best-of-N and fan-out scale proposal generation while the submitting engineer retains full merge accountability.

Before enabling team-wide cloud delegation, run npx @wondelai/harness-audit (Harness Audit) to confirm your repository’s AGENTS.md and CI checks align cleanly.

SymptomRoot causeRecovery
“Command not found” during agent loopSetup failed silently or exported PATH only inside setup shellAdd set -euo pipefail, persist variables via environment settings, and rehearse in codex-universal
Tests pass in cloud but fail in CIWarm container cache holds stale dependencies or codegenMove frozen lockfile sync and codegen into maintenance script; reset cache once
Every task pays full cold-start timeFrequent edits to setup script, vars, or secrets invalidate cacheBatch environment changes and keep volatile refresh steps in maintenance
Agent cannot fetch a package mid-taskAgent internet is disabled by defaultInstall the dependency during setup, or allowlist its registry read-only
Private registry fails during agent loopSecret token was purged at lockdownMove private package installs into setup; never copy secrets into env vars
Task ran against outdated codeLocal commits were not pushed or wrong branch targetedPush the branch first and pass branch identifiers explicitly in scripts
Applying a cloud diff fails locallyLocal working tree diverged from task baseApply into a clean worktree checked out at the task’s base branch

Refer to developers.openai.com/codex for current published-environment flows, codex cloud CLI commands, and Slack, Linear, and GitHub integration setup.

Did this guide help you?

One tap, no sign-in.

not at all a lot