SandboxCodex on your ChatGPT plan

Run Codex on your ChatGPT plan

Codex in a sandbox can sign in with your ChatGPT plan, so usage counts against that plan, or use your own OpenAI API key. Tangle bills only the sandbox’s compute. For Claude Code on a Claude plan, see Claude Code on your Claude plan.

With your ChatGPT plan

1. Sign in locally

codex login

Codex writes your login to ~/.codex/auth.json. Anyone holding that file can use your ChatGPT plan.

2. Store the login as a secret

import { readFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
 
await client.secrets.create(
  "CODEX_AUTH",
  readFileSync(join(homedir(), ".codex/auth.json"), "utf8"),
);

3. Start Codex

import { Sandbox } from "@tangle-network/sandbox";
 
const client = new Sandbox({
  apiKey: process.env.TANGLE_API_KEY!,
  baseUrl: "https://sandbox.tangle.tools",
});
 
const box = await client.create({
  backend: {
    type: "codex",
    model: {
      cliAuth: { account: "me", secretEnv: "CODEX_AUTH", format: "bundle" },
    },
  },
  secrets: ["CODEX_AUTH"],
  git: { url: "https://github.com/tangle-network/sandbox-demo" },
});
try {
  const run = await box.prompt("Add input validation to the signup handler.", {
    sessionId: "main",
  });
  console.log(run.success ? run.response : run.error);
} finally {
  await box.delete();
}

cliAuth.format says what the secret holds:

FormatThe secret holds
bundleThe contents of ~/.codex/auth.json.
filesA JSON array with one auth file whose path is .codex/auth.json and whose content is that file.

The sandbox writes the login to ~/.codex/auth.json before Codex starts. The create request is refused when cliAuth names a secret that is not in secrets; the CLI’s --cli-auth adds it for you. cliAuth cannot be combined with apiKey, apiKeyEnv, baseUrl or authFiles.

With an OpenAI API key

Store the key as a secret and name it with apiKeyEnv. The key stays out of the create request, and the platform mints no Router key for the sandbox.

await client.secrets.create("OPENAI_KEY", process.env.OPENAI_API_KEY!);
 
const box = await client.create({
  backend: {
    type: "codex",
    model: {
      apiKeyEnv: "OPENAI_KEY",
      baseUrl: "https://api.openai.com/v1",
    },
  },
  secrets: ["OPENAI_KEY"],
});

apiKeyEnv must name a secret listed in secrets. Otherwise the request is refused with backend.model.apiKeyEnv names OPENAI_KEY, which is not a selected secret or proxy alias.

Rotate the login

After codex login writes a new file, replace the stored value. client.secrets.update() replaces it and re-keys running sandboxes that use it:

await client.secrets.update("CODEX_AUTH", readFileSync(join(homedir(), ".codex/auth.json"), "utf8"));

To cut access everywhere, sign out of the session in your ChatGPT account settings.

In a workflow

A workflow’s agent.run step runs on the same login when its profile sets model.metadata.credentialSource: subscription on a codex profile and your account stored the login as CODEX_AUTH. The run creates its sandbox with cliAuth (format bundle) and CODEX_AUTH in secrets. See Workflows.

From HTTP

curl -X POST https://sandbox.tangle.tools/v1/sandboxes \
  -H "Authorization: Bearer $TANGLE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "backend": {
      "type": "codex",
      "model": {
        "cliAuth": { "account": "me", "secretEnv": "CODEX_AUTH", "format": "bundle" }
      }
    },
    "secrets": ["CODEX_AUTH"]
  }'

Next