Skip to content

Latest commit

 

History

History

codex

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Codex

Run the Codex CLI on a GitHub repository in a named Container. Each sandbox clones one repository, runs one task at a time, and returns Codex's changes as a diff. For a step-by-step guide, see Run Codex in a sandbox.

Done when GET .../diff shows the change you asked for.

Deploy

npm create cloudflare@latest -- codex-agent --template=cloudflare/sandbox-sdk/examples/coding-agents/codex
cd codex-agent

In wrangler.jsonc, set AI_GATEWAY_ACCOUNT_ID and AI_GATEWAY_ID. MODEL is an OpenAI model ID your gateway can serve, for example gpt-6-sol. To attach custom metadata to each model request, set AI_GATEWAY_METADATA to a JSON object, for example {"project": "my-app"}.

Create a Cloudflare API token with the AI Gateway Run permission. The first deploy reads the required secret from a file:

printf '{"AI_GATEWAY_TOKEN":"%s"}\n' "$AI_GATEWAY_TOKEN" > .secrets.json
npm run deploy -- --secrets-file .secrets.json
rm .secrets.json

Later deploys use npm run deploy.

To clone private repositories, add a GITHUB_TOKEN secret with npx wrangler secret put GITHUB_TOKEN. Use a fine-grained token limited to the repositories the agent works on. The agent can use the token for anything that token allows on github.com, including pushes if it has write access.

Run a task

Clone a repository into agent-1:

curl --request POST "$WORKER_URL/sandboxes/agent-1/repository" \
  --header "content-type: application/json" \
  --data '{"url": "https://github.com/OWNER/REPOSITORY"}'

Start a task. The body is the prompt:

curl --request POST "$WORKER_URL/sandboxes/agent-1/task" \
  --data 'Add a test for the parser.'

The response is 202. A second task while one runs returns 409. The prompt is one argument of the agent's command, so a prompt of 128 KiB or more returns 413.

Poll the task:

curl "$WORKER_URL/sandboxes/agent-1/task"

state is one of:

  • running
  • succeeded, with the agent's final reply
  • failed, with the error
  • lost, when the agent or its Container stopped before the agent finished
  • none, when no task has run in this Container

Stream the agent's events with GET .../events.

You do not need to poll to keep the task alive. While the agent runs, an alarm checks the task every minute. Each check keeps the Container awake.

Read the changes:

curl "$WORKER_URL/sandboxes/agent-1/diff"

Reset the Container:

curl --request DELETE "$WORKER_URL/sandboxes/agent-1/execution"

The next clone starts on a fresh disk.

Network access

The Container has no internet access. An Outbound entrypoint in the Worker receives every HTTP request on port 80 and HTTPS request on port 443 from the Container, and allows two hosts:

  • gateway.ai.cloudflare.com, only under your account and gateway. The Worker adds the gateway token, so the Container never holds it.
  • github.com. The Worker adds GITHUB_TOKEN when it is set.

Every other host gets 403. Connections to other ports time out.

The agent runs without its own permission prompts or sandbox. The Container is the sandbox: the agent can run any command in it, but it reaches only the two hosts above.

Authenticate in production. Anyone who can reach this Worker can spend your AI Gateway budget.

How Codex runs

Codex reaches OpenAI models through AI Gateway as a custom provider with no API key. It runs with its analytics, update check, and plugin sync turned off, so it only calls the gateway. Plugin sync would otherwise clone a repository from github.com on every run, with GITHUB_TOKEN attached.

Codex exits 1 when the turn fails, so the exit code decides the outcome. A successful task returns the message Codex writes with --output-last-message.

Codex runs with --dangerously-bypass-approvals-and-sandbox.

Code

src/index.ts holds Codex's command line and how to read its outcome. src/runner is a copy of the runner that every coding agent template in cloudflare/sandbox-sdk shares: the HTTP routes, the outbound policy, and the Durable Object that clones, runs, and tracks tasks.