Skip to content
UseAgent

Engineering · Codex · Security

How to run Codex in the cloud with your ChatGPT plan

How we run Codex in the cloud with your ChatGPT plan without the login entering the sandbox, one login per shared thread, and warm turns from 12.6 s to 5 s.

Abhishek8 min read
On this page

Most people who want Codex working in a cloud sandbox already pay for ChatGPT. The obvious way to use that plan is to copy the login into the sandbox, and that is the one thing we would not do: the sandbox runs whatever code the agent decides to run, and a ChatGPT session token in there is a token the agent can read.

So we run Codex in the cloud with your ChatGPT plan in two halves. Codex's brain sits next to your login on our side. Its hands are in the sandbox. Most of this post is the wire between them. After that, a shared-thread decision we argued over, and how a warm turn got from 12.6 seconds to about 5.

To run Codex in the cloud with your ChatGPT plan, keep the login on our side

When you connect ChatGPT under Settings, the login is stored on the control plane. When a Codex run starts, the Codex app-server process that holds that login runs on our backend host, not in the sandbox.

That process is locked down hard, because it runs on a trusted machine and the model is steering it. Every default-on feature that could start a process, a browser or a plugin on the host is off. ChatGPT Apps are off. Code-mode JavaScript the model writes runs inside the sandbox, not on the host. Codex's stderr stays on the host too, since it can contain host paths. What's left is the model's back-and-forth, and shell and file tools that reach the sandbox through the run's remote environment.

From inside the sandbox, the agent can run commands and read files. It can't see the login, and it can't talk to the app-server except through the relay.

A relay that only opens while the run is live

The relay connects the agent runtime in the sandbox to the app-server on our side. Its rules are short:

  • A grant is minted for one run and lives in the backend's memory. When the run ends, the grant is gone.
  • Between turns the relay fails closed. A turn/start is refused unless a run is active, and each activation allows one turn plus the one continuation the platform itself may send. Without this, code left running in the sandbox could start turns on your subscription while nobody was watching.
  • The login is re-authorized on every frame. If the connection changes, or you reconnect your account, the session closes.
  • A login is only served while its owner is still a member of the org.

"Lives in the backend's memory" bit us early. An operator tool that drove release checks ran in its own process and minted its own grants. The sandbox dialed the public relay, landed on the real backend, which had never heard of those grants, and every check died with "no first activity within 45000ms". It looked like a broken release candidate. The fix was a loopback endpoint so the tool asks the backend to do the work. It's one of the reasons we run exactly one backend per database.

Reply turns had their own trap. The runtime in the sandbox loses its local resume cursor between runs, because each run's relay instance is torn down, so it asks for thread/start instead of thread/resume. Before we caught it, every reply in a Codex thread died silently. The relay now rewrites a start for a thread that is already bound into a resume of that bound thread. A resume request is the start request plus the thread id, and the two responses have the same shape, which is why the rewrite is safe.

One login per shared thread

Threads in UseAgent are shared inside an org. Codex conversations are not: a Codex conversation lives in the ChatGPT account that created it.

We found out when a newly invited member replied in a Codex thread someone else had started. Their first reply failed because they had no ChatGPT connected. They connected their own and replied again, and that turn ran on their account, which moved the Codex conversation there. The thread starter's next message tried to continue that conversation with the starter's login. Codex refused, correctly, with "Codex resume thread binding mismatch", five times, and the run failed with a label that hid all of it: "read-input-stream".

There were two real options.

Each sender's own login The thread starter's login
Whose plan a turn uses The sender's The starter's, for every turn
Codex conversation One per person, missed turns passed in from thread history One per thread
Cost Each person's Codex holds a different native context The starter's plan pays for everyone's turns

We shipped the second. Every turn in a Codex thread runs on the starter's ChatGPT login, with no fallback to the sender. Tools, attribution and spend stay with the sender, so the run record says who asked. If the starter disconnects, the run fails with a message naming that. If the starter has left the org, the run tells you to start a new thread. Every place that reads a Codex credential goes through one function that picks the thread's account, so the gate, the relay and recovery can't disagree about whose login a turn uses.

The cost is plain: a teammate's reply spends the starter's plan limits. A ChatGPT plan is a personal account under OpenAI's terms, so if several people need Codex on the same problem, give each of them their own thread.

From 12.6 seconds to about 5

A warm follow-up in an existing Codex thread used to take 12.6 seconds to first text. The target was under 5.

Warm follow-up Before After the first pass
First text 12.6 s 5.2 s
Our dispatch 4.5 s 1.53 s
Dispatch to first text about 8.1 s about 3.4 s

What it took, in the order we learned it:

The hidden first model call wasn't one. Traces showed what looked like an extra model call at the start of each turn. It was Codex re-sending the previous turn's token usage on every resume, and we were storing it as part of the new run. Dropping it fixed nothing about speed, and fixed fleet token totals, which had been double counting.

ChatGPT Apps held every turn. Codex starts its Apps tool server and the runtime waited for it on each turn. Apps aren't part of our tool surface, so they're off.

A new Codex session every turn. Now there is one kept relay session per thread. Each kept session is about 150 MB on the host, measured in production, so 100 warm threads would be around 15 GB. They're capped at 40 globally and 4 per user, evicted after 5 minutes idle, and deactivated at the end of each run. One environment flag turns reuse off and goes back to a session per run.

That got the median to 5.2 seconds. The second pass issued the sandbox warm checks (credential marker, install identity, services probe) together instead of one after another, with the credential check still gating the rest, and had the warm pool start Codex's sandbox services ahead of time. The median dropped to 4.75 seconds, with our dispatch at 1.06. A 51-run soak with a backend restart in the middle came back green.

What's left is mostly the model: about 3.7 seconds, part of which is the runtime reloading its tool servers each turn. A brand new thread still takes around 15 seconds.

The helper that started in a directory that didn't exist

Kept sessions created a new problem. Codex's tool server needs a bearer for our tool gateway, the bearer used to change per run, and a long-lived Codex child's environment is fixed when it spawns. So we used Codex's http_headers_helper, a small command Codex runs to fetch headers, to read the current run's bearer.

Codex spawns that helper with the thread's working directory. The thread's working directory is a path that exists in the sandbox. The helper runs on the backend host, as an unprivileged user, where that path is not accessible. EACCES, the required tool server never starts, and every subscription turn died at the runtime's wait: "no first activity within 45000ms". Every Codex-on-ChatGPT turn, all at once, right after a release.

Our local proof had passed because the test used a directory that existed. We rolled back, then fixed forward: the credential Codex uses to reach our gateway is now handed to it once at spawn instead of through a helper command, and a kept session only serves runs that hold the same credential. A test now fails if anyone adds a *_helper= or command= override to the app-server config. Any change to that config gets proved in a throwaway container of the live backend image, as the same unprivileged user, with the sandbox's working directory.

Two more of these, briefly, because they rhyme.

Our platform pauses an idle sandbox two minutes after its last turn, which cuts every socket. Kept relays showed the sandbox side disconnect 124 to 149 seconds after a turn completed. The runtime held on to its dead client, we reused the kept relay anyway, and every follow-up after about two minutes failed with "Codex App Server transport operation 'read-input-stream' failed." without even dialing. A kept relay is now reused only while it's connected, and a turn cut by a backend restart gets one automatic continuation.

And when we moved to a new runtime version, its end-to-end tests ran Codex on an OpenAI API key, which never crosses the relay. The ChatGPT path broke on production because the new client sent a turn/start shape the relay refused. Now every runtime change gets a real ChatGPT-login run on production before we call it done.

If you pay for ChatGPT already, connect it in Settings and start a Codex thread; the relay code is in the repo.

Frequently asked questions

Does my ChatGPT login go into the sandbox?

No. The login stays on the control plane with Codex's app-server. The sandbox only executes shell and file operations that come through a per-run relay, and the relay refuses to start a turn when no run is active.

Which Codex models can I use with my ChatGPT plan?

Any model your own login's catalog lists. Models that would spend our keys instead are limited to an allowlist.

What happens if a teammate replies in my Codex thread?

Their turn runs on your ChatGPT login, because the Codex conversation lives in your account. Tools, attribution and spend are recorded against them. If you disconnect your login, the thread fails with a message saying so instead of switching accounts.

All posts →

Hand your next task
to a coding agent.

UseAgent runs Claude Code, Codex and OpenCode in cloud sandboxes. The core is open source, so you can read the code or host it yourself.