---
name: codex-pool
description: Lease shared ChatGPT/Codex subscription accounts from codex-pool instead of using paid API billing. Use whenever running codex CLI tasks, spawning sub-researchers, or integrating a service with the shared Codex account pool.
---

# codex-pool (Shared Codex Subscription Lease Broker)

Server URL: `https://codex.tbbs.io`

`codex-pool` runs on Cloudflare Workers + SQLite Durable Objects to manage shared ChatGPT subscription accounts, serialize single-flight OAuth token refresh, track quota windows, and attribute per-project / per-agent token consumption.

## Required Environment Variables

- `CODEX_POOL_URL=https://codex.tbbs.io`
- `CODEX_POOL_TOKEN=ck_...` (scoped client token issued by the admin dashboard or CLI)

## Hard Rules (MUST FOLLOW)

1. **NEVER fall back to `OPENAI_API_KEY` or `CODEX_API_KEY`** when the pool is exhausted or unavailable. This infrastructure exists specifically to use ChatGPT subscription accounts, not pay-as-you-go API billing.
2. **NEVER retry in a tight loop on `429 codex_quota_exhausted`**. Read `reset_at` from the JSON error response and surface it to the user or schedule work after `reset_at`.
3. **Keep thread affinity when resuming a Codex thread**: pass `pinAccountId` (or `--pin <account-id>`) for resumed `thread_id` turns. If you get `409 pinned_account_unavailable`, wait until `retry_at` or start a fresh thread on another account.
4. **Always pass a short `purpose` tag** (e.g. `review-pr-42`, `maturity:discover`) so the Dashboard attributes usage accurately.

## Recommended Usage for Coding Agents (CLI Wrapper)

Prefer `codex-pool exec` over manual HTTP lease calls. It automatically leases an account, creates a `0700` temporary `CODEX_HOME`, strips `OPENAI_API_KEY`/`CODEX_API_KEY`, sends heartbeats, writes back rotated `auth.json`, reports usage, and cleans up on exit or `SIGINT`/`SIGTERM`.

```bash
# 1. Check pool capacity before spawning concurrent jobs
codex-pool status

# 2. Run any Codex command under an automatically managed lease
codex-pool exec --purpose "review-pr-42" -- codex exec "Review the diff for bugs"
```

## HTTP Lease API (for Services / Custom Integrations)

All `/v1/*` endpoints require `Authorization: Bearer $CODEX_POOL_TOKEN`.

### 1. Check Pool Status
```http
GET https://codex.tbbs.io/v1/status
```

### 2. Acquire a Lease
```http
POST https://codex.tbbs.io/v1/leases
Content-Type: application/json

{
  "ttlMs": 1200000,
  "pinAccountId": null,
  "excludeAccountIds": [],
  "client": {
    "repo": "signals-mono",
    "harness": "cursor",
    "host": "dev-box",
    "purpose": "maturity:discover"
  }
}
```
Returns `200 OK` with `{ leaseId, expiresAt, account, authJson }`.
Write `JSON.stringify(authJson)` to a private `0600` file `$TMP_CODEX_HOME/auth.json` and run Codex with `CODEX_HOME=$TMP_CODEX_HOME`.

### 3. Heartbeat & Save Rotated Auth
```http
POST https://codex.tbbs.io/v1/leases/:leaseId/heartbeat
Content-Type: application/json

{ "ttlMs": 1200000, "authJson": { ... } }
```

### 4. Report Per-Turn Usage Telemetry
```http
POST https://codex.tbbs.io/v1/leases/:leaseId/usage
Content-Type: application/json

{
  "purpose": "maturity:discover",
  "model": "codex-default",
  "status": "completed",
  "inputTokens": 1200,
  "cachedInputTokens": 400,
  "outputTokens": 350,
  "searches": 2,
  "durationMs": 4800
}
```

### 5. Release Lease
```http
POST https://codex.tbbs.io/v1/leases/:leaseId/release
Content-Type: application/json

{
  "authJson": { ... },
  "failure": null,
  "usage": { ... }
}
```
